Skip to content

Commit c78328e

Browse files
committed
Create an Login page
1 parent 3b07d27 commit c78328e

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

components/Login/Login.tsx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { useForm } from "react-hook-form"
2+
import { useRouter } from "next/router"
3+
import { useSignInWithEmailAndPassword, SignInWithEmailAndPasswordData } from "../auth/hooks"
4+
import { Form, Stack, Button, Alert } from "../bootstrap"
5+
import Input from "../forms/Input"
6+
import PasswordInput from "../forms/PasswordInput"
7+
import { useTranslation } from "next-i18next"
8+
import { LoadingButton } from "../buttons"
9+
import SocialSignOnButtons from "components/auth/SocialSignOnButtons"
10+
import Divider from "../Divider/Divider"
11+
12+
export default function Login() {
13+
const router = useRouter()
14+
const redirect = (router.query.redirect as string) || "/"
15+
const { t } = useTranslation("auth")
16+
17+
const {
18+
register,
19+
handleSubmit,
20+
formState: { errors },
21+
} = useForm<SignInWithEmailAndPasswordData>()
22+
23+
const signIn = useSignInWithEmailAndPassword()
24+
25+
const success = () => {
26+
const safeRedirect = redirect.startsWith("/") ? redirect : "/"
27+
router.replace(safeRedirect)
28+
}
29+
const onSubmit = handleSubmit( credentials => {
30+
signIn.execute(credentials).then(success)
31+
})
32+
33+
34+
return (
35+
<Form onSubmit={onSubmit} noValidate>
36+
{signIn.error && <Alert variant="danger">{signIn.error.message}</Alert>}
37+
38+
<Stack gap={3}>
39+
<Input
40+
label={t("email")}
41+
type="email"
42+
{...register("email", {
43+
required: t("emailIsRequired") ?? "Email is required",
44+
})}
45+
error={errors.email?.message}
46+
/>
47+
48+
<PasswordInput
49+
label={t("password")}
50+
{...register("password", {
51+
required: t("passwordRequired") ?? "Password is required",
52+
})}
53+
error={errors.password?.message}
54+
/>
55+
56+
<LoadingButton
57+
type="submit"
58+
className="w-100"
59+
loading={signIn.loading}
60+
>
61+
{t("signIn")}
62+
</LoadingButton>
63+
64+
<Divider className="px-4">{t("or")}</Divider>
65+
66+
<SocialSignOnButtons onComplete={onSubmit} />
67+
</Stack>
68+
</Form>
69+
)
70+
}

components/auth/AuthRedirect.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect } from 'react';
22
import { useRouter } from 'next/router';
3-
import { useAuth } from '../auth'; // Adjust this path to your actual useAuth hook
3+
import { useAuth } from '../auth';
44

55
export function useAuthRedirect(isAuthRequired: boolean = true) {
66
const { user, loading } = useAuth();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useAuth } from '.'
2+
import { useRouter } from "next/router"
3+
import { useEffect } from "react"
4+
5+
interface ProtectedPageWrapperProps {
6+
children: React.ReactNode
7+
}
8+
9+
export const ProtectedPageWrapper = ({ children }: ProtectedPageWrapperProps) => {
10+
const { user, loading } = useAuth()
11+
const router = useRouter()
12+
13+
useEffect(() => {
14+
if (!loading && !user) {
15+
const currentPath = router.asPath
16+
router.replace(`/login?redirect=${encodeURIComponent(currentPath)}`)
17+
}
18+
}, [user, loading, router])
19+
20+
if (loading || !user) {
21+
return <div>Loading...</div>
22+
}
23+
24+
return <>{children}</>
25+
}

pages/login.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { createPage } from "../components/page"
2+
import Login from "components/Login/login"
3+
4+
export default createPage({
5+
title: "Login",
6+
Page: () => <Login />
7+
})
8+
import { serverSideTranslations } from "next-i18next/serverSideTranslations"
9+
export async function getStaticProps({ locale }: any) {
10+
return {
11+
props: {
12+
...(await serverSideTranslations(locale, ["auth", "common"]))
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)