diff --git a/README.md b/README.md index b998182..35b8f8c 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ And that's it! Your `SkateHub Frontend` should now be up and running locally on ### 2024 +- 2024-12-19 - Implement server-side redirection for authenticated users [#103](https://github.com/jpcmf/Frontend-GraduateProgram-FullStack-2024/pull/103) _(v0.1.30)_ - 2024-12-12 - Update user avatar handling and modify user data structure [#99](https://github.com/jpcmf/Frontend-GraduateProgram-FullStack-2024/pull/99) _(v0.1.29)_ - 2024-12-07 - Add custom `404 error` page [#91](https://github.com/jpcmf/Frontend-GraduateProgram-FullStack-2024/pull/91) _(v0.1.28)_ - 2024-12-07 - Implement user profile update functionality in AuthContext [#89](https://github.com/jpcmf/Frontend-GraduateProgram-FullStack-2024/pull/89) _(v0.1.27)_ diff --git a/package.json b/package.json index 6978e88..ad9c050 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "skatehub-frontend", - "version": "0.1.29", + "version": "0.1.30", "private": true, "scripts": { "dev": "next dev", diff --git a/src/pages/auth/forgot-password.tsx b/src/pages/auth/forgot-password.tsx index 58a790a..5ed57d6 100644 --- a/src/pages/auth/forgot-password.tsx +++ b/src/pages/auth/forgot-password.tsx @@ -11,6 +11,7 @@ import { Box, Button, Divider, Flex, Stack, Text } from "@chakra-ui/react"; import { API } from "@/utils/constant"; import { Toast } from "@/components/Toast"; import { Input } from "@/shared/components/Form/Input"; +import { redirectIfAuthenticated } from "@/utils/auth"; const forgotPasswordFormSchema = z.object({ email: z.string().email("E-mail inválido.").nonempty("Campo obrigatório.") @@ -155,3 +156,7 @@ export default function ForgotPassword() { ); } + +export const getServerSideProps = async (ctx: any) => { + return redirectIfAuthenticated(ctx); +}; diff --git a/src/pages/auth/reset-password.tsx b/src/pages/auth/reset-password.tsx index c10a6e5..93d7f69 100644 --- a/src/pages/auth/reset-password.tsx +++ b/src/pages/auth/reset-password.tsx @@ -10,6 +10,7 @@ import { Button, Divider, Flex, Stack, Text } from "@chakra-ui/react"; import { API } from "@/utils/constant"; import { Toast } from "@/components/Toast"; import { Input } from "@/shared/components/Form/Input"; +import { redirectIfAuthenticated } from "@/utils/auth"; const resetPasswordFormSchema = z .object({ @@ -157,3 +158,7 @@ export default function ResetPassword() { ); } + +export const getServerSideProps = async (ctx: any) => { + return redirectIfAuthenticated(ctx); +}; diff --git a/src/pages/auth/signin.tsx b/src/pages/auth/signin.tsx index 80dbe1d..d7646f8 100644 --- a/src/pages/auth/signin.tsx +++ b/src/pages/auth/signin.tsx @@ -12,6 +12,7 @@ import { Input } from "@/shared/components/Form/Input"; import { Toast } from "@/components/Toast"; import { AuthContext } from "@/contexts/AuthContext"; import { LogoSkateHub } from "@/components/LogoSkateHub"; +import { redirectIfAuthenticated } from "@/utils/auth"; const signInFormSchema = z.object({ email: z.string().email({ message: "E-mail deve ser um e-mail válido." }).min(1, { message: "Campo obrigatório." }), @@ -184,3 +185,7 @@ export default function SignIn() { ); } + +export const getServerSideProps = async (ctx: any) => { + return redirectIfAuthenticated(ctx); +}; diff --git a/src/pages/auth/signup.tsx b/src/pages/auth/signup.tsx index 18847a6..9b7e509 100644 --- a/src/pages/auth/signup.tsx +++ b/src/pages/auth/signup.tsx @@ -13,6 +13,7 @@ import { Button, Flex, Text, Stack, Box, Divider } from "@chakra-ui/react"; import { API } from "@/utils/constant"; import { Input } from "@/shared/components/Form/Input"; import { Toast } from "@/components/Toast"; +import { redirectIfAuthenticated } from "@/utils/auth"; const signUpSchema = z .object({ @@ -248,3 +249,7 @@ export default function SignUp() { ); } + +export const getServerSideProps = async (ctx: any) => { + return redirectIfAuthenticated(ctx); +}; diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 7749679..1357cf9 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -4,6 +4,9 @@ import { Button, Flex, Grid, GridItem, Stack, Text } from "@chakra-ui/react"; import packageJson from "../../package.json"; import { LogoSkateHub } from "@/components/LogoSkateHub"; +// import { useEffect } from "react"; +// import { destroyCookie, parseCookies } from "nookies"; +import { redirectIfAuthenticated } from "@/utils/auth"; export default function Home() { const router = useRouter(); @@ -112,3 +115,7 @@ export default function Home() { ); } + +export const getServerSideProps = async (ctx: any) => { + return redirectIfAuthenticated(ctx); +}; diff --git a/src/utils/auth.ts b/src/utils/auth.ts new file mode 100644 index 0000000..6dd6e47 --- /dev/null +++ b/src/utils/auth.ts @@ -0,0 +1,19 @@ +import { GetServerSidePropsContext } from "next"; +import { parseCookies } from "nookies"; + +export const redirectIfAuthenticated = (ctx: GetServerSidePropsContext) => { + const { ["auth.token"]: token } = parseCookies(ctx); + + if (token) { + return { + redirect: { + destination: "/dashboard", + permanent: false + } + }; + } + + return { + props: {} + }; +};