diff --git a/src/main/frontend/src/components/Auth.tsx b/src/main/frontend/src/components/Auth.tsx index 91744d7..664637a 100644 --- a/src/main/frontend/src/components/Auth.tsx +++ b/src/main/frontend/src/components/Auth.tsx @@ -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(undefined); function AuthProvider({ children}: Props) { - const [user, setUser] = useState(() => { + const [user, setUser] = useState(() => { 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)); }; @@ -41,13 +33,4 @@ const useAuth = () => { return context; } -function ProtectedRoute() { - const {user} = useAuth(); - console.log(`User2: ${user}`); - if (user === undefined) { - return ; - } - return ; -} - -export { AuthProvider, useAuth, ProtectedRoute }; \ No newline at end of file +export { AuthProvider, useAuth }; \ No newline at end of file diff --git a/src/main/frontend/src/components/ProtectedRoute.tsx b/src/main/frontend/src/components/ProtectedRoute.tsx new file mode 100644 index 0000000..a6c2b2c --- /dev/null +++ b/src/main/frontend/src/components/ProtectedRoute.tsx @@ -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 ; + } + return ; +} + +export default ProtectedRoute; \ No newline at end of file diff --git a/src/main/frontend/src/main.tsx b/src/main/frontend/src/main.tsx index 0726a47..7616a0f 100644 --- a/src/main/frontend/src/main.tsx +++ b/src/main/frontend/src/main.tsx @@ -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( diff --git a/src/main/frontend/src/vite-env.d.ts b/src/main/frontend/src/vite-env.d.ts index 3489bcf..b4f2572 100644 --- a/src/main/frontend/src/vite-env.d.ts +++ b/src/main/frontend/src/vite-env.d.ts @@ -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;