Skip to content

Commit

Permalink
fix: redirect.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrskr committed Nov 30, 2023
1 parent 0668e2c commit a089d25
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
8 changes: 7 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React from "react";
import { SnackbarProvider } from "notistack";
import CssBaseline from "@mui/material/CssBaseline";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import {
BrowserRouter as Router,
Route,
Routes,
Navigate,
} from "react-router-dom";

import { AdminPages } from "./pages/admin/AdminPages";
import { AccountPages } from "./pages/account/AccountPages";
Expand Down Expand Up @@ -34,6 +39,7 @@ function App() {
<Route path="errors">
<Route path="unauthorized" element={<ForbiddenPage />} />
</Route>
<Route path="*" element={<Navigate to="/accounts/login" />} />
</Routes>
</ApiContextProvider>
</AuthContextProvider>
Expand Down
11 changes: 8 additions & 3 deletions frontend/src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface AuthContextHelpersType {
*
* @param token - The valid JWT auth token
*/
authenticate: (token: string, userId: string, isAdmin: boolean) => void;
authenticate: (token: string) => Promise<AuthContextPayloadType>;

/**
* Function, used for logging out the currently authenticated user
Expand Down Expand Up @@ -87,10 +87,16 @@ export const AuthContextProvider: React.FC<React.PropsWithChildren<any>> = ({
) => {
localStorage.setItem(LocalStorage.AuthToken, token);

const decoded = decodeToken();

setAuthContextState((prevState) => ({
...prevState,
isAuthenticated: true,
isAdmin: decoded.isAdmin!,
userId: decoded.userId!,
}));

return authContextState;
};

const logout: AuthContextHelpersType["logout"] = async () => {
Expand Down Expand Up @@ -129,13 +135,12 @@ export const AuthContextProvider: React.FC<React.PropsWithChildren<any>> = ({

// endregion


return (
<AuthContext.Provider
value={{
...authContextState,
authenticate,
logout
logout,
}}
children={children}
/>
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/pages/account/pages/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export const LoginPage: React.FC = () => {
password: data.password,
});

const { accessToken, userId, isAdmin } = response;
const { accessToken } = response;

authenticate(accessToken, userId, isAdmin);
const auth = await authenticate(accessToken);

if (isAdmin) {
if (auth.isAdmin) {
navigate("/admin/orders");
} else {
navigate("/client/issue");
Expand Down

0 comments on commit a089d25

Please sign in to comment.