-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): forget password and logout
- Loading branch information
1 parent
08cb7a8
commit 14e683d
Showing
7 changed files
with
144 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Link, useNavigate } from 'react-router'; | ||
import { Button } from '~/components/ui/button'; | ||
import { Input } from '~/components/ui/input'; | ||
import { Label } from '~/components/ui/label'; | ||
import { ChevronLeft } from 'lucide-react'; | ||
import { redirectIfLoggedIn } from '~/common/auth'; | ||
import type { Route } from './+types/register'; | ||
import * as zod from 'zod'; | ||
import { useForm } from 'react-hook-form'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { client } from '~/clients/client'; | ||
import toast from 'react-hot-toast'; | ||
|
||
export async function loader({ request }: Route.LoaderArgs) { | ||
await redirectIfLoggedIn(request, '/home'); | ||
} | ||
|
||
const ForgetPasswordSchema = zod.object({ | ||
email: zod.string().nonempty({ message: 'Please enter your email' }).email({ message: 'Please enter a valid email' }), | ||
}); | ||
type ForgetPasswordSchema = zod.infer<typeof ForgetPasswordSchema>; | ||
|
||
export default function ForgetPassword() { | ||
const navigate = useNavigate(); | ||
const { | ||
register, | ||
formState: { errors }, | ||
handleSubmit, | ||
} = useForm({ | ||
resolver: zodResolver(ForgetPasswordSchema), | ||
}); | ||
const { mutateAsync, isPending } = client.useMutation('post', '/account/v1/reset-password'); | ||
|
||
const handleRegister = async ({ email }: ForgetPasswordSchema) => { | ||
try { | ||
await mutateAsync({ body: { email } }); | ||
toast.dismiss(); | ||
toast.success('Email sent successfully'); | ||
navigate('/pending-verification'); | ||
} catch { | ||
toast.error('Something went wrong'); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="flex flex-col gap-12 p-8"> | ||
<Link to="/login" className="flex items-center gap-1.5 text-secondary"> | ||
<ChevronLeft /> Back | ||
</Link> | ||
<div className="flex flex-col gap-14 w-2/3 mx-auto"> | ||
<div className="flex flex-col gap-6"> | ||
<h1 className="text-4xl font-bold">Forget password</h1> | ||
<h2 className="text-lg text-secondary">Don't worry! Just a few steps away.</h2> | ||
</div> | ||
<div className="flex flex-col gap-10"> | ||
<form className="flex flex-col gap-5" onSubmit={handleSubmit(handleRegister)}> | ||
<div className="flex flex-col gap-2.5"> | ||
<Label htmlFor="email" className="text-secondary"> | ||
</Label> | ||
<Input id="email" placeholder="Enter your email" className="h-14" {...register('email')} /> | ||
{errors.email && <p className="text-destructive">{errors.email.message}</p>} | ||
</div> | ||
<Button className="h-14 text-md" disabled={isPending}> | ||
Continue | ||
</Button> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useNavigate } from 'react-router'; | ||
import { ChevronLeft } from 'lucide-react'; | ||
|
||
import { client } from '~/clients/client'; | ||
import { useEffect } from 'react'; | ||
import toast from 'react-hot-toast'; | ||
|
||
export default function Logout() { | ||
const navigate = useNavigate(); | ||
const { mutateAsync } = client.useMutation('post', '/auth/v1/logout'); | ||
|
||
useEffect(() => { | ||
const logout = async () => { | ||
try { | ||
await mutateAsync({}); | ||
toast.success('Logged out successfully'); | ||
navigate('/login'); | ||
} catch { | ||
toast.error('Something went wrong'); | ||
return; | ||
} | ||
}; | ||
logout(); | ||
}, []); | ||
|
||
return ( | ||
<div className="flex flex-col gap-12 p-8"> | ||
<button className="flex items-center gap-1.5 text-secondary" onClick={() => navigate(-1)}> | ||
<ChevronLeft /> Back | ||
</button> | ||
<div className="flex flex-col gap-14 w-2/3 mx-auto"> | ||
<div className="flex flex-col gap-6"> | ||
<h1 className="text-4xl font-bold">Logout</h1> | ||
<h2 className="text-lg text-secondary">Please wait a moment...</h2> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
import { requireAuth } from '~/common/auth'; | ||
import type { Route } from './+types/home'; | ||
import { Link } from 'react-router'; | ||
import { Button } from '~/components/ui/button'; | ||
|
||
export async function loader({ request }: Route.LoaderArgs) { | ||
await requireAuth(request); | ||
} | ||
|
||
export default function Home() { | ||
return <div>Home</div>; | ||
return ( | ||
<div className="flex justify-between p-8"> | ||
<h1 className="text-xl">Home</h1> | ||
<Button asChild variant="outline" className="w-fit"> | ||
<Link to="/logout">Logout</Link> | ||
</Button> | ||
</div> | ||
); | ||
} |