import React, { createContext, Dispatch, PropsWithChildren, SetStateAction, useContext, useEffect, useState } from "react" import axios from "axios" const AuthUserContext = createContext(undefined) interface AuthUserProps { authUser?: User|null, setAuthUser: Dispatch>, loadingAuthUser: boolean, logout: () => void, } export const AuthUserProvider = ({children}: PropsWithChildren) => { const [authUser, setAuthUser] = useState(null) const [loadingAuthUser, setLoadingAuthUser] = useState(true) useEffect(() => { (async () => { try { const res = await axios.get('/api/user') setAuthUser(res.data) } catch (error) { // @ts-expect-error 401 error to redirect if (error.response.status === 401) { console.info('no user login') if (!['/connexion', '/sinscrire'].includes(window.location.pathname)) { window.location.href = '/connexion' } } } finally { setLoadingAuthUser(false) } })() }, []) const logout = async () => { try { setLoadingAuthUser(false) await axios.delete('/api/logout') setAuthUser(null) window.location.replace('/') } catch (e) { console.error(e) } finally { setLoadingAuthUser(false) } } return {children} } const useAuthUser = () => { const context = useContext(AuthUserContext) if (context === undefined) { throw new Error('Add AuthUserProvider to use AuthUserContext') } return context } export default useAuthUser interface User { id: number, name: string, email: string, locations: {id: number, latitude: number, longitude: number}[], }