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'
|
|
|
|
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'
|
2022-09-01 05:13:26 +00:00
|
|
|
import { AUTH_COOKIE_NAME } from 'common/envs/constants'
|
|
|
|
import { setCookie } from 'web/lib/util/cookie'
|
2022-07-21 07:38:26 +00:00
|
|
|
|
|
|
|
// 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-09-01 05:13:26 +00:00
|
|
|
const TEN_YEARS_SECS = 60 * 60 * 24 * 365 * 10
|
2022-08-12 20:41:00 +00:00
|
|
|
const CACHED_USER_KEY = 'CACHED_USER_KEY_V2'
|
2022-09-01 05:13:26 +00:00
|
|
|
|
2022-08-29 05:40:57 +00:00
|
|
|
// Proxy localStorage in case it's not available (eg in incognito iframe)
|
|
|
|
const localStorage =
|
|
|
|
typeof window !== 'undefined'
|
|
|
|
? window.localStorage
|
|
|
|
: {
|
|
|
|
getItem: () => null,
|
|
|
|
setItem: () => {},
|
|
|
|
removeItem: () => {},
|
|
|
|
}
|
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-09-01 05:13:26 +00:00
|
|
|
export const setUserCookie = (cookie: string | undefined) => {
|
|
|
|
const data = setCookie(AUTH_COOKIE_NAME, cookie ?? '', [
|
|
|
|
['path', '/'],
|
|
|
|
['max-age', (cookie === undefined ? 0 : TEN_YEARS_SECS).toString()],
|
|
|
|
['samesite', 'lax'],
|
|
|
|
['secure'],
|
|
|
|
])
|
|
|
|
document.cookie = data
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-09-03 02:39:27 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (authUser != null) {
|
|
|
|
// Persist to local storage, to reduce login blink next time.
|
|
|
|
// Note: Cap on localStorage size is ~5mb
|
|
|
|
localStorage.setItem(CACHED_USER_KEY, JSON.stringify(authUser))
|
|
|
|
} else {
|
|
|
|
localStorage.removeItem(CACHED_USER_KEY)
|
|
|
|
}
|
|
|
|
}, [authUser])
|
|
|
|
|
2022-07-21 07:38:26 +00:00
|
|
|
useEffect(() => {
|
2022-08-29 05:26:12 +00:00
|
|
|
return onIdTokenChanged(
|
|
|
|
auth,
|
|
|
|
async (fbUser) => {
|
|
|
|
if (fbUser) {
|
2022-09-01 05:13:26 +00:00
|
|
|
setUserCookie(JSON.stringify(fbUser.toJSON()))
|
2022-08-29 05:26:12 +00:00
|
|
|
let current = await getUserAndPrivateUser(fbUser.uid)
|
|
|
|
if (!current.user || !current.privateUser) {
|
|
|
|
const deviceToken = ensureDeviceToken()
|
|
|
|
current = (await createUser({ deviceToken })) as UserAndPrivateUser
|
2022-09-03 02:39:27 +00:00
|
|
|
setCachedReferralInfoForUser(current.user)
|
2022-08-29 05:26:12 +00:00
|
|
|
}
|
|
|
|
setAuthUser(current)
|
|
|
|
} else {
|
|
|
|
// User logged out; reset to null
|
2022-09-01 05:13:26 +00:00
|
|
|
setUserCookie(undefined)
|
2022-08-29 05:26:12 +00:00
|
|
|
setAuthUser(null)
|
2022-07-21 07:38:26 +00:00
|
|
|
}
|
2022-08-29 05:26:12 +00:00
|
|
|
},
|
|
|
|
(e) => {
|
|
|
|
console.error(e)
|
2022-07-21 07:38:26 +00:00
|
|
|
}
|
2022-08-29 05:26:12 +00:00
|
|
|
)
|
2022-07-21 07:38:26 +00:00
|
|
|
}, [setAuthUser])
|
|
|
|
|
2022-08-12 20:41:00 +00:00
|
|
|
const uid = authUser?.user.id
|
2022-07-21 07:38:26 +00:00
|
|
|
useEffect(() => {
|
2022-09-03 02:39:27 +00:00
|
|
|
if (uid) {
|
2022-08-12 20:41:00 +00:00
|
|
|
identifyUser(uid)
|
2022-09-03 02:39:27 +00:00
|
|
|
const userListener = listenForUser(uid, (user) => {
|
|
|
|
setAuthUser((currAuthUser) =>
|
|
|
|
currAuthUser && user ? { ...currAuthUser, user } : null
|
|
|
|
)
|
|
|
|
})
|
2022-08-12 20:41:00 +00:00
|
|
|
const privateUserListener = listenForPrivateUser(uid, (privateUser) => {
|
2022-09-03 02:39:27 +00:00
|
|
|
setAuthUser((currAuthUser) =>
|
|
|
|
currAuthUser && privateUser ? { ...currAuthUser, privateUser } : null
|
|
|
|
)
|
2022-08-12 20:41:00 +00:00
|
|
|
})
|
|
|
|
return () => {
|
|
|
|
userListener()
|
|
|
|
privateUserListener()
|
|
|
|
}
|
2022-07-21 07:38:26 +00:00
|
|
|
}
|
2022-09-03 02:39:27 +00:00
|
|
|
}, [uid, setAuthUser])
|
|
|
|
|
|
|
|
const username = authUser?.user.username
|
|
|
|
useEffect(() => {
|
|
|
|
if (username != null) {
|
|
|
|
setUserProperty('username', username)
|
|
|
|
}
|
|
|
|
}, [username])
|
2022-07-21 07:38:26 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<AuthContext.Provider value={authUser}>{children}</AuthContext.Provider>
|
|
|
|
)
|
|
|
|
}
|