cleaning things up and getting the cookies way somewhat working but it's just a pain...keep it client side maybe

This commit is contained in:
Arjun Patel
2022-01-14 00:07:12 -08:00
parent 4e4bc2bcc9
commit 6e5e7ab22f
7 changed files with 174 additions and 33 deletions
+13 -4
View File
@@ -1,6 +1,7 @@
import { getAuth, GoogleAuthProvider, onAuthStateChanged, signInWithPopup, signOut, User } from "firebase/auth";
import React, { useContext, useEffect, useState } from "react";
import firebase from '../services/firebaseService'
import nookies from 'nookies'
const AuthContext = React.createContext(null);
const googleProvider = new GoogleAuthProvider()
@@ -12,12 +13,20 @@ export function AuthProvider({ children }) {
const [loading, setLoading] = useState(true)
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, user => {
const unsubscribe = onAuthStateChanged(auth, async (user) => {
console.log('change in auth')
if (user) {
setCurrUser(user)
// will be null or the firebase logged in user
setCurrUser(user)
// set the token in the cookies for ssr verification
const token = await user.getIdToken();
nookies.set(undefined, 'token', token, { path: '/' });
} else {
nookies.set(undefined, 'token', '', { path: '/' });
setCurrUser(null)
}
setLoading(false)
});