introduce the auth provider
This commit is contained in:
		
							
								
								
									
										69
									
								
								src/contexts/authcontext.tsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										69
									
								
								src/contexts/authcontext.tsx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,69 @@ | ||||
| import axios from 'axios'; | ||||
| import { createContext, Dispatch, SetStateAction, useEffect, useState } from 'react'; | ||||
| import { useNavigate } from 'react-router-dom'; | ||||
| import { Configuration } from '../services'; | ||||
|  | ||||
| export interface User { | ||||
|   vatsimId: string; | ||||
|   fullName: string; | ||||
|   administrator: boolean; | ||||
|   airportConfigurationAccess: string[]; | ||||
| }; | ||||
|  | ||||
| const AuthContext = createContext<{ | ||||
|   auth: { valid: boolean; user: User }; | ||||
|   setAuth: Dispatch<SetStateAction<{ valid: boolean; user: User }>>; | ||||
| }>({ auth: { | ||||
|   valid: false, | ||||
|   user: { | ||||
|     vatsimId: '', | ||||
|     fullName: '', | ||||
|     administrator: false, | ||||
|     airportConfigurationAccess: [], | ||||
|   }, | ||||
| }, setAuth: () => {} }); | ||||
|  | ||||
| export const AuthProvider = ({ children }: { children: any }) => { | ||||
|   const [auth, setAuth] = useState<{ valid: boolean; user: User }>({ | ||||
|     valid: false, | ||||
|     user: { | ||||
|       vatsimId: '', | ||||
|       fullName: '', | ||||
|       administrator: false, | ||||
|       airportConfigurationAccess: [], | ||||
|     }, | ||||
|   }); | ||||
|   const navigate = useNavigate(); | ||||
|  | ||||
|   useEffect(() => { | ||||
|     axios.get<User>(`${Configuration.resourceServer}/auth/user`, { | ||||
|       headers: { | ||||
|         Authorization: `Bearer ${sessionStorage.getItem('token')}`, | ||||
|       }, | ||||
|     }).then((response) => { | ||||
|       setAuth({ valid: true, user: response.data }); | ||||
|     }).catch(() => { | ||||
|       setAuth({ | ||||
|         valid: false, | ||||
|         user: { | ||||
|           vatsimId: '', | ||||
|           fullName: '', | ||||
|           administrator: false, | ||||
|           airportConfigurationAccess: [], | ||||
|         }, | ||||
|       }); | ||||
|       navigate('/'); | ||||
|     }) | ||||
|   // eslint-disable-next-line react-hooks/exhaustive-deps | ||||
|   }, []); | ||||
|  | ||||
|   return ( | ||||
|     <> | ||||
|       <AuthContext.Provider value={{ auth, setAuth }}> | ||||
|         {children} | ||||
|       </AuthContext.Provider> | ||||
|     </> | ||||
|   ); | ||||
| }; | ||||
|  | ||||
| export default AuthContext; | ||||
							
								
								
									
										3
									
								
								src/contexts/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								src/contexts/index.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,3 @@ | ||||
| import AuthContext, { AuthProvider } from "./authcontext"; | ||||
|  | ||||
| export { AuthContext, AuthProvider }; | ||||
		Reference in New Issue
	
	Block a user