-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from mohamedsalem401/auth
Add login/registration forms
- Loading branch information
Showing
36 changed files
with
797 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { z } from 'zod'; | ||
import { registerSchema } from '../register/register-schema'; | ||
|
||
export const loginSchema = registerSchema.pick({ | ||
email: true, | ||
password: true, | ||
}); | ||
|
||
export type LoginSchemaType = z.infer<typeof loginSchema>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { useForm } from 'react-hook-form'; | ||
import { Link, useNavigate } from 'react-router-dom'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
|
||
import { Input } from '@/components/ui/input'; | ||
import { Button } from '@/components/ui/button'; | ||
import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'; | ||
import { LoadingSpinner } from '@/components/connections/components/LoadingSpinner'; | ||
|
||
import useLoginMutation from '@/hooks/mutations/useLoginMutation'; | ||
import { type LoginSchemaType, loginSchema } from './login-schema'; | ||
import { toast } from 'sonner'; | ||
|
||
export const LoginUserForm = () => { | ||
const navigate = useNavigate(); | ||
const { mutate: login, isPending: loginPending } = useLoginMutation(); | ||
|
||
const form = useForm<LoginSchemaType>({ | ||
resolver: zodResolver(loginSchema), | ||
}); | ||
|
||
const onSubmit = async (data: LoginSchemaType) => { | ||
login(data, { | ||
onSuccess: () => { | ||
navigate('/'); | ||
}, | ||
onError: () => { | ||
toast.error("Failed to log in. Please verify your email and password and try again."); | ||
}, | ||
}); | ||
}; | ||
|
||
return ( | ||
<div className='mx-auto w-full max-w-sm lg:w-96'> | ||
<div className='text-center'> | ||
<Link to='/'> | ||
<img src='/logo.png' className='w-14 mx-auto' /> | ||
</Link> | ||
<h2 className='mt-6 text-3xl font-extrabold'>Login</h2> | ||
<p className='mt-2 text-sm'> | ||
Don't have an account?{' '} | ||
<Link to='/auth/register' className='font-medium text-primary hover:text-primary/80'> | ||
Register | ||
</Link> | ||
</p> | ||
</div> | ||
|
||
<Form {...form}> | ||
<form className='space-y-6 mt-8' onSubmit={form.handleSubmit(onSubmit)}> | ||
<FormField | ||
control={form.control} | ||
name='email' | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel htmlFor='email'>Email</FormLabel> | ||
<FormControl> | ||
<Input placeholder='[email protected]' autoComplete='email' {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
|
||
<FormField | ||
control={form.control} | ||
name='password' | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel htmlFor='password'>Password</FormLabel> | ||
<FormControl> | ||
<Input placeholder='********' type='password' autoComplete='current-password' {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
|
||
<Link to='/auth/forgot-password' className='font-medium block text-sm text-primary hover:text-primary/80'> | ||
Forgot your password? | ||
</Link> | ||
|
||
<Button type='submit' className='w-full' disabled={loginPending}> | ||
{loginPending ? <LoadingSpinner className='' /> : 'Login'} | ||
</Button> | ||
</form> | ||
</Form> | ||
</div> | ||
); | ||
}; |
81 changes: 81 additions & 0 deletions
81
apps/webapp/src/components/auth/register/create-organization-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { useForm } from "react-hook-form"; | ||
import { Link, useNavigate } from "react-router-dom"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
|
||
import { | ||
Form, | ||
FormControl, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from "@/components/ui/form"; | ||
import { Input } from "@/components/ui/input"; | ||
import { Button } from "@/components/ui/button"; | ||
import { LoadingSpinner } from "@/components/connections/components/LoadingSpinner"; | ||
|
||
import useOrganisationMutation from "@/hooks/mutations/useOrganisationMutation"; | ||
import { | ||
type OrganizationFormSchemaType, | ||
organizationFormSchema, | ||
} from "./create-organization-schema"; | ||
|
||
export const CreateOrganizationForm = () => { | ||
const navigate = useNavigate(); | ||
const { mutate, isPending } = useOrganisationMutation(); | ||
|
||
const form = useForm<OrganizationFormSchemaType>({ | ||
resolver: zodResolver(organizationFormSchema), | ||
}); | ||
|
||
const onSubmit = (data: OrganizationFormSchemaType) => { | ||
mutate( | ||
{ ...data, stripe_customer_id: "stripe-customer-76" }, | ||
{ | ||
onSuccess: () => navigate("/"), | ||
} | ||
); | ||
}; | ||
|
||
return ( | ||
<div className="mx-auto w-full max-w-sm lg:w-96"> | ||
<div className="text-center"> | ||
<Link to="/"> | ||
<img src="/logo.png" className="w-14 mx-auto" /> | ||
</Link> | ||
<h2 className="mt-6 text-3xl font-extrabold">Create Organization</h2> | ||
</div> | ||
|
||
<Form {...form}> | ||
<form className="space-y-6 mt-8" onSubmit={form.handleSubmit(onSubmit)}> | ||
<FormField | ||
control={form.control} | ||
name="name" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel htmlFor="name">Name</FormLabel> | ||
<FormControl> | ||
<Input | ||
placeholder="organization" | ||
autoComplete="given-name" | ||
{...field} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<Button className="w-full" type="submit"> | ||
{isPending ? <LoadingSpinner className="" /> : "Create"} | ||
</Button> | ||
</form> | ||
</Form> | ||
<Link | ||
to="/" | ||
className="mt-4 text-sm font-medium text-primary hover:text-primary/80 text-center block mx-auto" | ||
> | ||
Skip For Now | ||
</Link> | ||
</div> | ||
); | ||
}; |
8 changes: 8 additions & 0 deletions
8
apps/webapp/src/components/auth/register/create-organization-schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { z } from 'zod'; | ||
|
||
export const organizationFormSchema = z.object({ | ||
name: z.string().min(1, 'Name is required').max(100, 'Name must be less than 100 characters'), | ||
stripe_customer_id: z.string().min(1, 'stripe customer id is required'), | ||
}); | ||
|
||
export type OrganizationFormSchemaType = z.infer<typeof organizationFormSchema>; |
12 changes: 12 additions & 0 deletions
12
apps/webapp/src/components/auth/register/register-form.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useState } from 'react'; | ||
|
||
import { RegisterUserForm } from './register-user-form'; | ||
import { CreateOrganizationForm } from './create-organization-form'; | ||
|
||
export const RegisterForm = () => { | ||
const [registered, setIsRegistered] = useState(false); | ||
|
||
if (!registered) return <RegisterUserForm onSuccess={() => setIsRegistered(true)} />; | ||
|
||
return <CreateOrganizationForm />; | ||
}; |
10 changes: 10 additions & 0 deletions
10
apps/webapp/src/components/auth/register/register-schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { z } from 'zod'; | ||
|
||
export const registerSchema = z.object({ | ||
firstName: z.string().min(1, 'First name is required'), | ||
lastName: z.string().min(1, 'Last name is required'), | ||
email: z.string().min(1, 'Email is required').email('Email is invalid'), | ||
password: z.string().min(8, 'Password must be at least 8 characters'), | ||
}); | ||
|
||
export type RegisterSchemaType = z.infer<typeof registerSchema>; |
Oops, something went wrong.