import React , { useState , useEffect , useContext , createContext } from 'react' ; import nookies from 'nookies' ; import { firebaseClient } from './initializers/firebaseClient' ; export const AuthProvider = ( { children } ) => { const [ user , setUser ] = useState < User | null > ( null ) ; useEffect ( ( ) => { if ( typeof window !== undefined ) { ( window as any ) . nookies = nookies ; } return firebaseClient . auth ( ) . onIdTokenChanged ( async ( user ) => { if ( ! user ) { console . log ( `[AUTH] No user account found, destroying cookie` ) ; setUser ( null ) ; nookies . destroy ( null , 'token' ) ; return ; } const idToken = ( user as any ) . za ; const refreshToken = user . refreshToken ; const tokenCookieContent = { idToken , refreshToken , } ; nookies . destroy ( null , 'token' ) ; nookies . set ( null , 'token' , JSON . stringify ( tokenCookieContent ) , { } ) ; console . log ( `[AUTH] Updated cookies token` ) ; } ) ; } , [ ] ) ; return < > { children } < />; } ;