-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix with-firebase-authentication example: Update the cookie when ID token refreshes on client #15628
Fix with-firebase-authentication example: Update the cookie when ID token refreshes on client #15628
Changes from 7 commits
399c0ac
118b265
2cac15e
d1dd7d5
7c28a20
affee0b
0db0c5d
163b181
c7b434c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const mapUserData = (user) => { | ||
const { uid, email, xa } = user | ||
return { | ||
id: uid, | ||
email, | ||
token: xa, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useRouter } from 'next/router' | ||
import cookies from 'js-cookie' | ||
import firebase from 'firebase/app' | ||
import 'firebase/auth' | ||
import initFirebase from '../auth/initFirebase' | ||
import { | ||
removeUserCookie, | ||
setUserCookie, | ||
getUserFromCookie, | ||
} from './userCookies' | ||
import { mapUserData } from './mapUserData' | ||
|
||
initFirebase() | ||
|
||
|
@@ -17,8 +22,6 @@ const useUser = () => { | |
.signOut() | ||
.then(() => { | ||
// Sign-out successful. | ||
cookies.remove('auth') | ||
setUser() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This happens automatically via the |
||
router.push('/auth') | ||
}) | ||
.catch((e) => { | ||
|
@@ -27,12 +30,26 @@ const useUser = () => { | |
} | ||
|
||
useEffect(() => { | ||
const cookie = cookies.get('auth') | ||
if (!cookie) { | ||
// Firebase updates the id token every hour, this | ||
// makes sure the react state and the cookie are | ||
// both kept up to date | ||
firebase.auth().onIdTokenChanged((user) => { | ||
if (user) { | ||
const userData = mapUserData(user) | ||
setUserCookie(userData) | ||
setUser(userData) | ||
} else { | ||
removeUserCookie() | ||
setUser() | ||
} | ||
}) | ||
|
||
const userFromCookie = getUserFromCookie() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the user retrieval from the cookie that existed before. It's not really necessary now, as it will be handled by |
||
if (!userFromCookie) { | ||
router.push('/') | ||
return | ||
} | ||
setUser(JSON.parse(cookie)) | ||
setUser(userFromCookie) | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import cookies from 'js-cookie' | ||
|
||
export const getUserFromCookie = () => { | ||
const cookie = cookies.get('auth') | ||
if (!cookie) { | ||
return | ||
} | ||
return JSON.parse(cookie) | ||
} | ||
|
||
export const setUserCookie = (user) => { | ||
cookies.set('auth', user, { | ||
// firebase id tokens expire in one hour | ||
// set cookie expiry to match | ||
expires: 1 / 24, | ||
}) | ||
} | ||
|
||
export const removeUserCookie = () => cookies.remove('auth') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a tiny UI bug - when an error happened it still showed "Loading..." as well as "Failed to fetch food!"