-
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(boilerplate): add auth guard in webapp
- Loading branch information
Showing
6 changed files
with
199 additions
and
81 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
examples/common_nestjs_remix/apps/web/app/modules/Auth/authEffect.ts
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,18 @@ | ||
import { useEffect } from "react"; | ||
import { useAuthStore } from "./authStore"; | ||
import { useNavigate } from "@remix-run/react"; | ||
|
||
/** | ||
* @returns void | ||
* @description This hook is used to rediect the user to the login page if they are not logged in. | ||
*/ | ||
export function useAuthEffect() { | ||
const loggedIn = useAuthStore((state) => state.isLoggedIn); | ||
const navigate = useNavigate(); | ||
|
||
useEffect(() => { | ||
if (!loggedIn) { | ||
navigate("/login"); | ||
} | ||
}, [loggedIn, navigate]); | ||
} |
15 changes: 15 additions & 0 deletions
15
examples/common_nestjs_remix/apps/web/app/modules/Auth/authGuard.ts
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,15 @@ | ||
import { redirect } from "@remix-run/react"; | ||
import { useAuthStore } from "./authStore"; | ||
|
||
/** | ||
* @returns void | ||
* @description This function is used to rediect the user to the login page if they are not logged in. | ||
* @description Should be moved to middlewares once they are supported in Remix/ReactRouter7 | ||
*/ | ||
export function authGuard() { | ||
const authStore = useAuthStore.getState(); | ||
|
||
if (!authStore.isLoggedIn) { | ||
return redirect("/login"); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
examples/common_nestjs_remix/apps/web/app/modules/Auth/authStore.ts
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,11 @@ | ||
import { create } from "zustand"; | ||
|
||
type AuthStore = { | ||
isLoggedIn: boolean; | ||
setAuthState: (isLoggedIn: boolean) => void; | ||
}; | ||
|
||
export const useAuthStore = create<AuthStore>((set) => ({ | ||
isLoggedIn: false, | ||
setAuthState: (isLoggedIn) => set({ isLoggedIn }), | ||
})); |
8 changes: 8 additions & 0 deletions
8
examples/common_nestjs_remix/apps/web/app/modules/Dashboard/Dashboard.layout.tsx
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
Oops, something went wrong.