2022-08-09 05:42:52 +00:00
|
|
|
import { ReactNode, createContext, useEffect } from 'react'
|
2022-07-21 07:38:26 +00:00
|
|
|
import { User } from 'common/user'
|
|
|
|
import { onIdTokenChanged } from 'firebase/auth'
|
|
|
|
import {
|
|
|
|
auth,
|
|
|
|
listenForUser,
|
|
|
|
getUser,
|
|
|
|
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
|
|
|
|
// the user is not logged in (null), or we know the user is logged in (User).
|
|
|
|
type AuthUser = undefined | null | User
|
|
|
|
|
|
|
|
const CACHED_USER_KEY = 'CACHED_USER_KEY'
|
|
|
|
|
|
|
|
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-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) => {
|
|
|
|
if (fbUser) {
|
2022-08-06 03:49:29 +00:00
|
|
|
setTokenCookies({
|
|
|
|
id: await fbUser.getIdToken(),
|
|
|
|
refresh: fbUser.refreshToken,
|
|
|
|
})
|
2022-07-21 07:38:26 +00:00
|
|
|
let user = await getUser(fbUser.uid)
|
|
|
|
if (!user) {
|
|
|
|
const deviceToken = ensureDeviceToken()
|
|
|
|
user = (await createUser({ deviceToken })) as User
|
|
|
|
}
|
|
|
|
setAuthUser(user)
|
|
|
|
// Persist to local storage, to reduce login blink next time.
|
|
|
|
// Note: Cap on localStorage size is ~5mb
|
|
|
|
localStorage.setItem(CACHED_USER_KEY, JSON.stringify(user))
|
|
|
|
setCachedReferralInfoForUser(user)
|
|
|
|
} 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])
|
|
|
|
|
|
|
|
const authUserId = authUser?.id
|
|
|
|
const authUsername = authUser?.username
|
|
|
|
useEffect(() => {
|
|
|
|
if (authUserId && authUsername) {
|
|
|
|
identifyUser(authUserId)
|
|
|
|
setUserProperty('username', authUsername)
|
|
|
|
return listenForUser(authUserId, setAuthUser)
|
|
|
|
}
|
|
|
|
}, [authUserId, authUsername, setAuthUser])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<AuthContext.Provider value={authUser}>{children}</AuthContext.Provider>
|
|
|
|
)
|
|
|
|
}
|