Skip to content

Commit

Permalink
login: authorization organization
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelharlow committed Oct 26, 2024
1 parent 76cf1a9 commit a431859
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
23 changes: 3 additions & 20 deletions src/main/frontend/src/components/Auth.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import {createContext, useContext, useState} from "react";
import {Navigate, Outlet} from "react-router-dom";


interface AuthContextType {
user: Login | undefined;
signIn: (data: any) => void;
signOut: () => void;
}

const AuthContext = createContext<AuthContextType | undefined>(undefined);

function AuthProvider({ children}: Props) {
const [user, setUser] = useState<any | undefined>(() => {
const [user, setUser] = useState<Login | undefined>(() => {
const user = sessionStorage.getItem("user");
return user ? JSON.parse(user) : undefined;
});

const signIn = (data: any) => {
const signIn = (data: Login) => {
setUser(data);
sessionStorage.setItem("user", JSON.stringify(data));
};
Expand All @@ -41,13 +33,4 @@ const useAuth = () => {
return context;
}

function ProtectedRoute() {
const {user} = useAuth();
console.log(`User2: ${user}`);
if (user === undefined) {
return <Navigate to={"/login"} replace />;
}
return <Outlet />;
}

export { AuthProvider, useAuth, ProtectedRoute };
export { AuthProvider, useAuth };
13 changes: 13 additions & 0 deletions src/main/frontend/src/components/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {Navigate, Outlet} from "react-router-dom";
import {useAuth} from "@/components/Auth.tsx";

function ProtectedRoute() {
const {user} = useAuth();
console.log(`User2: ${user}`);
if (user === undefined) {
return <Navigate to={"/login"} replace />;
}
return <Outlet />;
}

export default ProtectedRoute;
3 changes: 2 additions & 1 deletion src/main/frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import './index.css'
import {createBrowserRouter, createRoutesFromElements, Navigate, Route, RouterProvider} from "react-router-dom";
import Layout from "@/components/Layout.tsx";
import Login from "@/routes/Login.tsx";
import {AuthProvider, ProtectedRoute} from "@/components/Auth.tsx";
import { AuthProvider } from "@/components/Auth.tsx";
import ProtectedRoute from "@/components/ProtectedRoute.tsx";

const router = createBrowserRouter(
createRoutesFromElements(
Expand Down
6 changes: 6 additions & 0 deletions src/main/frontend/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ interface Props {
children?: React.ReactNode;
}

interface AuthContextType {
user: Login | undefined;
signIn: (data: any) => void;
signOut: () => void;
}

interface Account {
id: number;
userType: number;
Expand Down

0 comments on commit a431859

Please sign in to comment.