use the new session management
This commit is contained in:
		| @@ -3,10 +3,12 @@ import { useNavigate } from 'react-router-dom'; | ||||
| import { Auth } from '../services'; | ||||
| import { User } from '../types'; | ||||
|  | ||||
| const AuthContext = createContext<{ | ||||
|   auth: { valid: boolean; user: User }; | ||||
|   setAuth: Dispatch<SetStateAction<{ valid: boolean; user: User }>>; | ||||
| }>({ auth: { | ||||
| export interface IAuthState { | ||||
|   valid: boolean, | ||||
|   user: User, | ||||
| }; | ||||
|  | ||||
| const InitialAuthState: IAuthState = { | ||||
|   valid: false, | ||||
|   user: { | ||||
|     vatsimId: '', | ||||
| @@ -14,33 +16,29 @@ const AuthContext = createContext<{ | ||||
|     administrator: false, | ||||
|     airportConfigurationAccess: [], | ||||
|   }, | ||||
| }, setAuth: () => {} }); | ||||
| }; | ||||
|  | ||||
| const AuthContext = createContext<{ | ||||
|   auth: IAuthState; | ||||
|   setAuth: Dispatch<SetStateAction<IAuthState>>; | ||||
|   resetAuth: () => void; | ||||
| }>({ auth: InitialAuthState, setAuth: () => {}, resetAuth: () => {} }); | ||||
|  | ||||
| export const AuthProvider = ({ children }: { children: any }) => { | ||||
|   const [auth, setAuth] = useState<{ valid: boolean; user: User }>({ | ||||
|     valid: false, | ||||
|     user: { | ||||
|       vatsimId: '', | ||||
|       fullName: '', | ||||
|       administrator: false, | ||||
|       airportConfigurationAccess: [], | ||||
|     }, | ||||
|   }); | ||||
|   const [auth, setAuth] = useState<IAuthState>(InitialAuthState); | ||||
|   const navigate = useNavigate(); | ||||
|  | ||||
|   const resetAuth = () => setAuth(InitialAuthState); | ||||
|  | ||||
|   useEffect(() => { | ||||
|     Auth.user().then((user) => { | ||||
|       setAuth({ valid: true, user }); | ||||
|       if (user !== undefined) { | ||||
|         setAuth({ valid: true, user }); | ||||
|       } else { | ||||
|         setAuth(InitialAuthState); | ||||
|       } | ||||
|     }).catch(() => { | ||||
|       setAuth({ | ||||
|         valid: false, | ||||
|         user: { | ||||
|           vatsimId: '', | ||||
|           fullName: '', | ||||
|           administrator: false, | ||||
|           airportConfigurationAccess: [], | ||||
|         }, | ||||
|       }); | ||||
|       setAuth(InitialAuthState); | ||||
|       navigate('/'); | ||||
|     }); | ||||
|   // eslint-disable-next-line react-hooks/exhaustive-deps | ||||
| @@ -48,7 +46,7 @@ export const AuthProvider = ({ children }: { children: any }) => { | ||||
|  | ||||
|   return ( | ||||
|     <> | ||||
|       <AuthContext.Provider value={{ auth, setAuth }}> | ||||
|       <AuthContext.Provider value={{ auth, setAuth, resetAuth }}> | ||||
|         {children} | ||||
|       </AuthContext.Provider> | ||||
|     </> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user