2022-08-09 05:42:52 +00:00
|
|
|
import { ReactNode, createContext, useEffect } from 'react'
|
2022-07-21 07:38:26 +00:00
|
|
|
import { onIdTokenChanged } from 'firebase/auth'
|
|
|
|
import {
|
2022-08-12 20:41:00 +00:00
|
|
|
UserAndPrivateUser,
|
2022-07-21 07:38:26 +00:00
|
|
|
auth,
|
|
|
|
listenForUser,
|
2022-08-12 20:41:00 +00:00
|
|
|
listenForPrivateUser,
|
|
|
|
getUserAndPrivateUser,
|
2022-07-21 07:38:26 +00:00
|
|
|
setCachedReferralInfoForUser,
|
|
|
|
} from 'web/lib/firebase/users'
|
2022-08-06 03:49:29 +00:00
|
|
|
import { deleteTokenCookies, setTokenCookies } from 'web/lib/firebase/auth'
|
2022-07-21 07:38:26 +00:00
|
|
|
import { createUser } from 'web/lib/firebase/api'
|
|
|
|
import { randomString } from 'common/util/random'
|
|
|
|
import { identifyUser, setUserProperty } from 'web/lib/service/analytics'
|
|
|
|
import { useStateCheckEquality } from 'web/hooks/use-state-check-equality'
|
|
|
|
|
|
|
|
// Either we haven't looked up the logged in user yet (undefined), or we know
|
2022-08-12 20:41:00 +00:00
|
|
|
// the user is not logged in (null), or we know the user is logged in.
|
|
|
|
type AuthUser = undefined | null | UserAndPrivateUser
|
2022-07-21 07:38:26 +00:00
|
|
|
|
2022-08-12 20:41:00 +00:00
|
|
|
const CACHED_USER_KEY = 'CACHED_USER_KEY_V2'
|
2022-07-21 07:38:26 +00:00
|
|
|
|
|
|
|
const ensureDeviceToken = () => {
|
|
|
|
let deviceToken = localStorage.getItem('device-token')
|
|
|
|
if (!deviceToken) {
|
|
|
|
deviceToken = randomString()
|
|
|
|
localStorage.setItem('device-token', deviceToken)
|
|
|
|
}
|
|
|
|
return deviceToken
|
|
|
|
}
|
|
|
|
|
2022-08-09 05:42:52 +00:00
|
|
|
export const AuthContext = createContext<AuthUser>(undefined)
|
2022-07-21 07:38:26 +00:00
|
|
|
|
2022-08-09 05:42:52 +00:00
|
|
|
export function AuthProvider(props: {
|
|
|
|
children: ReactNode
|
|
|
|
serverUser?: AuthUser
|
|
|
|
}) {
|
|
|
|
const { children, serverUser } = props
|
|
|
|
const [authUser, setAuthUser] = useStateCheckEquality<AuthUser>(serverUser)
|
2022-08-12 20:41:00 +00:00
|
|
|
|
2022-07-21 07:38:26 +00:00
|
|
|
useEffect(() => {
|
2022-08-09 05:42:52 +00:00
|
|
|
if (serverUser === undefined) {
|
|
|
|
const cachedUser = localStorage.getItem(CACHED_USER_KEY)
|
|
|
|
setAuthUser(cachedUser && JSON.parse(cachedUser))
|
|
|
|
}
|
|
|
|
}, [setAuthUser, serverUser])
|
2022-07-21 07:38:26 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return onIdTokenChanged(auth, async (fbUser) => {
|
2022-08-25 22:21:51 +00:00
|
|
|
console.log('onIdTokenChanged', fbUser)
|
2022-07-21 07:38:26 +00:00
|
|
|
if (fbUser) {
|
2022-08-06 03:49:29 +00:00
|
|
|
setTokenCookies({
|
|
|
|
id: await fbUser.getIdToken(),
|
|
|
|
refresh: fbUser.refreshToken,
|
|
|
|
})
|
2022-08-12 20:41:00 +00:00
|
|
|
let current = await getUserAndPrivateUser(fbUser.uid)
|
|
|
|
if (!current.user || !current.privateUser) {
|
2022-07-21 07:38:26 +00:00
|
|
|
const deviceToken = ensureDeviceToken()
|
2022-08-12 20:41:00 +00:00
|
|
|
current = (await createUser({ deviceToken })) as UserAndPrivateUser
|
2022-07-21 07:38:26 +00:00
|
|
|
}
|
2022-08-12 20:41:00 +00:00
|
|
|
setAuthUser(current)
|
2022-07-21 07:38:26 +00:00
|
|
|
// Persist to local storage, to reduce login blink next time.
|
|
|
|
// Note: Cap on localStorage size is ~5mb
|
2022-08-12 20:41:00 +00:00
|
|
|
localStorage.setItem(CACHED_USER_KEY, JSON.stringify(current))
|
|
|
|
setCachedReferralInfoForUser(current.user)
|
2022-07-21 07:38:26 +00:00
|
|
|
} else {
|
|
|
|
// User logged out; reset to null
|
2022-08-06 03:49:29 +00:00
|
|
|
deleteTokenCookies()
|
2022-07-21 07:38:26 +00:00
|
|
|
setAuthUser(null)
|
|
|
|
localStorage.removeItem(CACHED_USER_KEY)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}, [setAuthUser])
|
|
|
|
|
2022-08-12 20:41:00 +00:00
|
|
|
const uid = authUser?.user.id
|
|
|
|
const username = authUser?.user.username
|
2022-07-21 07:38:26 +00:00
|
|
|
useEffect(() => {
|
2022-08-12 20:41:00 +00:00
|
|
|
if (uid && username) {
|
|
|
|
identifyUser(uid)
|
|
|
|
setUserProperty('username', username)
|
|
|
|
const userListener = listenForUser(uid, (user) =>
|
|
|
|
setAuthUser((authUser) => {
|
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
|
|
|
return { ...authUser!, user: user! }
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const privateUserListener = listenForPrivateUser(uid, (privateUser) => {
|
|
|
|
setAuthUser((authUser) => {
|
|
|
|
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
|
|
|
return { ...authUser!, privateUser: privateUser! }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return () => {
|
|
|
|
userListener()
|
|
|
|
privateUserListener()
|
|
|
|
}
|
2022-07-21 07:38:26 +00:00
|
|
|
}
|
2022-08-12 20:41:00 +00:00
|
|
|
}, [uid, username, setAuthUser])
|
2022-07-21 07:38:26 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<AuthContext.Provider value={authUser}>{children}</AuthContext.Provider>
|
|
|
|
)
|
|
|
|
}
|