-
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.
login: authorization on frontend login + all routes
- Loading branch information
1 parent
e6681c9
commit 76cf1a9
Showing
4 changed files
with
162 additions
and
17 deletions.
There are no files selected for viewing
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,53 @@ | ||
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 = sessionStorage.getItem("user"); | ||
return user ? JSON.parse(user) : undefined; | ||
}); | ||
|
||
const signIn = (data: any) => { | ||
setUser(data); | ||
sessionStorage.setItem("user", JSON.stringify(data)); | ||
}; | ||
|
||
const signOut = () => { | ||
setUser(undefined); | ||
sessionStorage.removeItem("user"); | ||
}; | ||
|
||
return ( | ||
<AuthContext.Provider value={{user, signIn, signOut}}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
} | ||
|
||
const useAuth = () => { | ||
const context = useContext(AuthContext); | ||
if (context === undefined) { | ||
throw new Error("useAuth must be used within an AuthProvider"); | ||
} | ||
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 }; |
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
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 |
---|---|---|
@@ -1,10 +1,19 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
interface Props { | ||
children?: React.ReactNode; | ||
} | ||
|
||
interface Account { | ||
id: number; | ||
userType: number; | ||
username: string; | ||
password: string; | ||
email: string; | ||
phoneNumber: string; | ||
} | ||
|
||
interface Login { | ||
email: string; | ||
password: string; | ||
} |