Unverified Commit 5ecdcab6 authored by Matsumoto Toshi's avatar Matsumoto Toshi Committed by GitHub

fix: Using getIdToken to get a token (#18599)

Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 7e51b29c
/* globals window */
import { useEffect, useState } from 'react'
import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth' import StyledFirebaseAuth from 'react-firebaseui/StyledFirebaseAuth'
import firebase from 'firebase/app' import firebase from 'firebase/app'
import 'firebase/auth' import 'firebase/auth'
...@@ -24,29 +22,19 @@ const firebaseAuthConfig = { ...@@ -24,29 +22,19 @@ const firebaseAuthConfig = {
credentialHelper: 'none', credentialHelper: 'none',
callbacks: { callbacks: {
signInSuccessWithAuthResult: async ({ user }, redirectUrl) => { signInSuccessWithAuthResult: async ({ user }, redirectUrl) => {
const userData = mapUserData(user) const userData = await mapUserData(user)
setUserCookie(userData) setUserCookie(userData)
}, },
}, },
} }
const FirebaseAuth = () => { const FirebaseAuth = () => {
// Do not SSR FirebaseUI, because it is not supported.
// https://github.com/firebase/firebaseui-web/issues/213
const [renderAuth, setRenderAuth] = useState(false)
useEffect(() => {
if (typeof window !== 'undefined') {
setRenderAuth(true)
}
}, [])
return ( return (
<div> <div>
{renderAuth ? (
<StyledFirebaseAuth <StyledFirebaseAuth
uiConfig={firebaseAuthConfig} uiConfig={firebaseAuthConfig}
firebaseAuth={firebase.auth()} firebaseAuth={firebase.auth()}
/> />
) : null}
</div> </div>
) )
} }
......
export const mapUserData = (user) => { export const mapUserData = async (user) => {
const { uid, email, xa, ya } = user const { uid, email } = user
const token = await user.getIdToken(true)
return { return {
id: uid, id: uid,
email, email,
token: xa || ya, token,
} }
} }
...@@ -33,9 +33,11 @@ const useUser = () => { ...@@ -33,9 +33,11 @@ const useUser = () => {
// Firebase updates the id token every hour, this // Firebase updates the id token every hour, this
// makes sure the react state and the cookie are // makes sure the react state and the cookie are
// both kept up to date // both kept up to date
const cancelAuthListener = firebase.auth().onIdTokenChanged((user) => { const cancelAuthListener = firebase
.auth()
.onIdTokenChanged(async (user) => {
if (user) { if (user) {
const userData = mapUserData(user) const userData = await mapUserData(user)
setUserCookie(userData) setUserCookie(userData)
setUser(userData) setUser(userData)
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment