From c2a7124cd20e5dea4d6f412e18ec60a3b626315e Mon Sep 17 00:00:00 2001 From: falendikategar Date: Tue, 29 Oct 2024 09:37:01 +0700 Subject: [PATCH] update: changes to the redirect page when the user login, it will be differentiated according to their role --- apps/backend/src/routes/auth/route.ts | 1 + apps/frontend/src/contexts/AuthContext.tsx | 32 +++++++++++-------- apps/frontend/src/routes/login/index.lazy.tsx | 27 ++++++++++------ 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/apps/backend/src/routes/auth/route.ts b/apps/backend/src/routes/auth/route.ts index 89b1405..1adbcb5 100644 --- a/apps/backend/src/routes/auth/route.ts +++ b/apps/backend/src/routes/auth/route.ts @@ -134,6 +134,7 @@ const authRoutes = new Hono() user: { id: user[0].users.id, name: user[0].users.name, + role: user[0].roles?.code, permissions: Array.from(permissions), }, }); diff --git a/apps/frontend/src/contexts/AuthContext.tsx b/apps/frontend/src/contexts/AuthContext.tsx index 69ad1ce..0253b93 100644 --- a/apps/frontend/src/contexts/AuthContext.tsx +++ b/apps/frontend/src/contexts/AuthContext.tsx @@ -2,19 +2,20 @@ import { ReactNode } from "@tanstack/react-router"; import { createContext, useState } from "react"; interface AuthContextType { - user: { - id: string; - name: string; - permissions: string[]; - } | null; - accessToken: string | null; - saveAuthData: ( - userData: NonNullable, - accessToken?: NonNullable - ) => void; - clearAuthData: () => void; - checkPermission: (permission: string) => boolean; - isAuthenticated: boolean; + user: { + id: string; + name: string; + permissions: string[]; + role: string; + } | null; + accessToken: string | null; + saveAuthData: ( + userData: { id: string; name: string; permissions: string[]; role: string }, + accessToken?: string + ) => void; + clearAuthData: () => void; + checkPermission: (permission: string) => boolean; + isAuthenticated: boolean; } export const AuthContext = createContext( @@ -25,6 +26,7 @@ export function AuthProvider({ children }: { children: ReactNode }) { const [userId, setUserId] = useState(null); const [userName, setUserName] = useState(null); const [permissions, setPermissions] = useState(null); + const [role, setRole] = useState(null); const [accessToken, setAccessToken] = useState( localStorage.getItem("accessToken") ); @@ -36,6 +38,7 @@ export function AuthProvider({ children }: { children: ReactNode }) { setUserId(userData.id); setUserName(userData.name); setPermissions(userData.permissions); + setRole(userData.role); if (accessToken) { setAccessToken(accessToken); localStorage.setItem("accessToken", accessToken); @@ -46,6 +49,7 @@ export function AuthProvider({ children }: { children: ReactNode }) { setUserId(null); setUserName(null); setPermissions(null); + setRole(null); setAccessToken(null); localStorage.removeItem("accessToken"); }; @@ -60,7 +64,7 @@ export function AuthProvider({ children }: { children: ReactNode }) { { if (isAuthenticated) { - navigate({ - to: "/dashboard", - replace: true, - }); + // Redirect based on user role + const userRole = JSON.parse(localStorage.getItem('userRole') || '{}'); + if (userRole === 'super-admin') { + navigate({ + to: "/users", + replace: true, + }); + } else { + navigate({ + to: "/assessmentRequest", + replace: true, + }); + } } }, [navigate, isAuthenticated]); @@ -63,33 +72,31 @@ export default function LoginPage() { const res = await client.auth.login.$post({ form: values, }); - + if (res.ok) { return await res.json(); } - + throw res; }, - onSuccess: (data) => { saveAuthData( { id: data.user.id, name: data.user.name, permissions: data.user.permissions, + role: data.user.role || '', }, data.accessToken ); + localStorage.setItem('userRole', JSON.stringify(data.user.role)); }, - onError: async (error) => { - console.log("error!"); if (error instanceof Response) { const body = await error.json(); setErrorMessage(body.message as string); return; } - console.log("bukan error"); }, });