"use client";

import {createContext,useContext,useEffect,useState} from "react";
import * as auth from "../services/auth/auth";

const AuthContext=createContext(null);

export function AuthProvider({children}){

const [user,setUser]=useState(null);

useEffect(()=>{
auth.profile()
.then(setUser)
.catch(()=>setUser(null));
},[]);

return(
<AuthContext.Provider value={{user,setUser,...auth}}>
{children}
</AuthContext.Provider>
);

}

export function useAuth(){
return useContext(AuthContext);
}
