From 91ce20776cf6a0a157f4cca57e609c7d802e7960 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Mon, 10 Apr 2023 16:42:20 +0100 Subject: [PATCH] Add route protection --- src/components/Authenticator/authenticator.ts | 25 ++++++++++++++----- src/components/Navigation/navigation.ts | 2 +- src/pages/signup.astro | 22 ++++++---------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/components/Authenticator/authenticator.ts b/src/components/Authenticator/authenticator.ts index 183e3dc9f..91cf60bf4 100644 --- a/src/components/Authenticator/authenticator.ts +++ b/src/components/Authenticator/authenticator.ts @@ -29,20 +29,33 @@ function showHideGuestElements(hideOrShow: 'hide' | 'show' = 'hide') { }); } -/** - * Prepares the UI for the user who is logged in - */ +// Prepares the UI for the user who is logged in function handleGuest() { + const authenticatedRoutes = [ + '/settings/profile', + '/settings/change-password', + ]; + showHideAuthElements('hide'); showHideGuestElements('show'); + + // If the user is on an authenticated route, redirect them to the home page + if (authenticatedRoutes.includes(window.location.pathname)) { + window.location.href = '/'; + } } -/** - * Prepares the UI for the user who is logged out - */ +// Prepares the UI for the user who is logged out function handleAuthenticated() { + const guestRoutes = ['/login', '/signup']; + showHideGuestElements('hide'); showHideAuthElements('show'); + + // If the user is on a guest route, redirect them to the home page + if (guestRoutes.includes(window.location.pathname)) { + window.location.href = '/'; + } } export function handleAuthRequired() { diff --git a/src/components/Navigation/navigation.ts b/src/components/Navigation/navigation.ts index ceab0a675..82aaf9233 100644 --- a/src/components/Navigation/navigation.ts +++ b/src/components/Navigation/navigation.ts @@ -4,7 +4,7 @@ import { handleAuthRequired } from '../Authenticator/authenticator'; function logout() { Cookies.remove(TOKEN_COOKIE_NAME); - handleAuthRequired(); + // Reloading will automatically redirect the user if required window.location.reload(); } diff --git a/src/pages/signup.astro b/src/pages/signup.astro index 1db5e3717..7cd9bb4cf 100644 --- a/src/pages/signup.astro +++ b/src/pages/signup.astro @@ -15,16 +15,17 @@ import { GoogleButton } from '../components/AuthenticationFlow/GoogleButton'; >

Sign Up

- -

- Create an account to track your progress, showcase your skill-set and be a part of the community. - videos. +

+ Create an account to track your progress, showcase your skill-set and + be a part of the community. videos.

@@ -46,12 +47,3 @@ import { GoogleButton } from '../components/AuthenticationFlow/GoogleButton';
- -