From 33bb8afc4d01b35c72c6be2be1f624a347a68247 Mon Sep 17 00:00:00 2001 From: sripwoud Date: Mon, 9 Sep 2024 16:40:03 +0200 Subject: [PATCH] fix(client): protect `/x` route (#3) * feat(client): protect `/x` route * chore: add comment * feat(client): do not show login button link on login page --- .biome.jsonc | 10 ++++++++ client/src/app/x/page.tsx | 39 ++++++++++++++++-------------- client/src/components/Header.tsx | 1 + client/src/components/withAuth.tsx | 21 ++++++++++++++++ client/src/hooks/useAuthorized.ts | 1 + 5 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 client/src/components/withAuth.tsx diff --git a/.biome.jsonc b/.biome.jsonc index 226808c..82668c5 100644 --- a/.biome.jsonc +++ b/.biome.jsonc @@ -34,6 +34,16 @@ "include": ["server/src/lib/verify.ts"], "linter": { "rules": { "style": { "noUnusedTemplateLiteral": "off", "useTemplate": "off" } } }, }, + { + "include": ["client/src/components/withAuth.tsx"], + "linter": { + "rules": { + "correctness": { + "useExhaustiveDependencies": "off", + }, + }, + }, + }, ], "vcs": { "enabled": true, diff --git a/client/src/app/x/page.tsx b/client/src/app/x/page.tsx index 85642da..d8bb8c9 100644 --- a/client/src/app/x/page.tsx +++ b/client/src/app/x/page.tsx @@ -1,20 +1,23 @@ -export default function X() { - return ( -
-

Gallery

-
-
- {[0, 1, 2].map((index) => ( -
- {`Dog -
- ))} -
+'use client' +import { withAuth } from 'c/withAuth' + +const X = () => ( +
+

Gallery

+
+
+ {[0, 1, 2].map((index) => ( +
+ {`Dog +
+ ))}
- ) -} +
+) + +export default withAuth(X) diff --git a/client/src/components/Header.tsx b/client/src/components/Header.tsx index 3b45f89..81fc5fb 100644 --- a/client/src/components/Header.tsx +++ b/client/src/components/Header.tsx @@ -10,6 +10,7 @@ export const Header = () => { const pathname = usePathname() const render = () => { + if (pathname === '/login') return null if (loading === true) return if (auth === true) { diff --git a/client/src/components/withAuth.tsx b/client/src/components/withAuth.tsx new file mode 100644 index 0000000..0acec5a --- /dev/null +++ b/client/src/components/withAuth.tsx @@ -0,0 +1,21 @@ +import { useAuthorized } from 'h/useAuthorized' +import { useRouter } from 'next/navigation' +import { useEffect } from 'react' +import type { ComponentType } from 'react' + +export const withAuth =

(Component: ComponentType

) => { + return function WithAuth(props: P) { + const { auth, loading } = useAuthorized() + const router = useRouter() + useEffect(() => { + if (auth === false) { + alert('Nice try! But we need to verify your age first.') + router.push('/login') + } + }, [loading]) + if (auth === false || loading === true) + return null + + return + } +} diff --git a/client/src/hooks/useAuthorized.ts b/client/src/hooks/useAuthorized.ts index 5e2ee33..4c3c3ef 100644 --- a/client/src/hooks/useAuthorized.ts +++ b/client/src/hooks/useAuthorized.ts @@ -17,6 +17,7 @@ export const useAuthorized = () => { } useEffect(() => { + // TODO: of course this is unsecure because trivial to craft setAuth(Cookies.get(config.cookie.name) === 'true') setLoading(false) }, [setAuth])