everything works
This commit is contained in:
+12
-22
@@ -1,6 +1,5 @@
|
|||||||
import { getAuth, GoogleAuthProvider, onAuthStateChanged, signInWithPopup, User } from "firebase/auth";
|
import { getAuth, GoogleAuthProvider, onAuthStateChanged, signInWithPopup, signOut, User } from "firebase/auth";
|
||||||
import React, { useContext, useEffect, useState } from "react";
|
import React, { useContext, useEffect, useState } from "react";
|
||||||
import { useRouter } from 'next/router'
|
|
||||||
import firebase from '../services/firebase'
|
import firebase from '../services/firebase'
|
||||||
|
|
||||||
const AuthContext = React.createContext(null);
|
const AuthContext = React.createContext(null);
|
||||||
@@ -9,31 +8,29 @@ const googleProvider = new GoogleAuthProvider()
|
|||||||
const auth = getAuth();
|
const auth = getAuth();
|
||||||
|
|
||||||
export function AuthProvider({ children }) {
|
export function AuthProvider({ children }) {
|
||||||
const [currUser, setCurrUser] = useState(null);
|
const [currUser, setCurrUser] = useState();
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
|
|
||||||
const router = useRouter()
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsubscribe = onAuthStateChanged(auth, user => {
|
const unsubscribe = onAuthStateChanged(auth, user => {
|
||||||
setLoading(false)
|
console.log('change in auth')
|
||||||
|
|
||||||
console.log(user)
|
|
||||||
|
|
||||||
// will be null or the firebase logged in user
|
// will be null or the firebase logged in user
|
||||||
setCurrUser(user)
|
setCurrUser(user)
|
||||||
|
|
||||||
|
setLoading(false)
|
||||||
});
|
});
|
||||||
|
|
||||||
return unsubscribe
|
return unsubscribe
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const signInGoogle = () => {
|
const signInGoogle = () => {
|
||||||
signInWithPopup(auth, googleProvider).then((res) => {
|
return signInWithPopup(auth, googleProvider).then((res) => {
|
||||||
// This gives you a Google Access Token. You can use it to access the Google API.
|
// This gives you a Google Access Token. You can use it to access the Google API.
|
||||||
const credential = GoogleAuthProvider.credentialFromResult(result);
|
const credential = GoogleAuthProvider.credentialFromResult(res);
|
||||||
const token = credential.accessToken;
|
const token = credential.accessToken;
|
||||||
// The signed-in user info.
|
// The signed-in user info.
|
||||||
const user = result.user;
|
const user = res.user;
|
||||||
|
|
||||||
setCurrUser(user)
|
setCurrUser(user)
|
||||||
|
|
||||||
@@ -47,27 +44,20 @@ export function AuthProvider({ children }) {
|
|||||||
// The AuthCredential type that was used.
|
// The AuthCredential type that was used.
|
||||||
const credential = GoogleAuthProvider.credentialFromError(error);
|
const credential = GoogleAuthProvider.credentialFromError(error);
|
||||||
console.log(error)
|
console.log(error)
|
||||||
|
throw error
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const signOut = () => {
|
const logOut = () => {
|
||||||
console.log('logging user out')
|
console.log('logging user out')
|
||||||
|
|
||||||
signOut(auth).then(() => {
|
|
||||||
// Sign-out successful.
|
|
||||||
console.log("signed out fine, redirecting to proper place")
|
|
||||||
|
|
||||||
router.push('/')
|
return signOut(auth)
|
||||||
}).catch((error) => {
|
|
||||||
// An error happened.
|
|
||||||
console.log(error)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const value = {
|
const value = {
|
||||||
currUser,
|
currUser,
|
||||||
signInGoogle,
|
signInGoogle,
|
||||||
signOut
|
logOut
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -5,21 +5,35 @@ import { useEffect } from 'react'
|
|||||||
import { useAuth } from '../../contexts/authContext'
|
import { useAuth } from '../../contexts/authContext'
|
||||||
|
|
||||||
export default function Dashboard() {
|
export default function Dashboard() {
|
||||||
const { currUser, signOut } = useAuth()
|
const { currUser, logOut } = useAuth()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
|
console.log("in dashboard " + currUser)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// if not authed, go to home
|
// if not authed, go to home
|
||||||
if (!currUser) {
|
if (!currUser) {
|
||||||
console.log('routing from dashboard to teams home')
|
console.log('not authenticated...routing from dashboard to teams home')
|
||||||
router.push('/teams')
|
router.push('/teams')
|
||||||
}
|
}
|
||||||
}, [])
|
}, [currUser])
|
||||||
|
|
||||||
|
async function handleSignOut() {
|
||||||
|
console.log("clicked log out button")
|
||||||
|
|
||||||
|
try {
|
||||||
|
await logOut()
|
||||||
|
|
||||||
|
router.push('/teams')
|
||||||
|
} catch(error) {
|
||||||
|
console.log(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h1 className='text-white'>This is the dashboard</h1>
|
<h1 className='text-white'>This is the dashboard</h1>
|
||||||
<button onClick={signOut} className='mt-10 text-sm text-orange-500 font-semibold py-1 px-4 bg-orange-200 rounded'>👋 Logout</button>
|
<button onClick={handleSignOut} className='mt-10 text-sm text-orange-500 font-semibold py-1 px-4 bg-orange-200 rounded'>👋 Logout</button>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user