Some changes to make auth better (#846)
* Handle the case where a user is surprisingly not in the DB * Only set referral info on user after creation * More reliably cache current user info in local storage * Don't jam username stuff into user listener hook
This commit is contained in:
parent
e924061c54
commit
8318621d51
|
@ -67,6 +67,16 @@ export function AuthProvider(props: {
|
||||||
}
|
}
|
||||||
}, [setAuthUser, serverUser])
|
}, [setAuthUser, serverUser])
|
||||||
|
|
||||||
|
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])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return onIdTokenChanged(
|
return onIdTokenChanged(
|
||||||
auth,
|
auth,
|
||||||
|
@ -77,17 +87,13 @@ export function AuthProvider(props: {
|
||||||
if (!current.user || !current.privateUser) {
|
if (!current.user || !current.privateUser) {
|
||||||
const deviceToken = ensureDeviceToken()
|
const deviceToken = ensureDeviceToken()
|
||||||
current = (await createUser({ deviceToken })) as UserAndPrivateUser
|
current = (await createUser({ deviceToken })) as UserAndPrivateUser
|
||||||
|
setCachedReferralInfoForUser(current.user)
|
||||||
}
|
}
|
||||||
setAuthUser(current)
|
setAuthUser(current)
|
||||||
// Persist to local storage, to reduce login blink next time.
|
|
||||||
// Note: Cap on localStorage size is ~5mb
|
|
||||||
localStorage.setItem(CACHED_USER_KEY, JSON.stringify(current))
|
|
||||||
setCachedReferralInfoForUser(current.user)
|
|
||||||
} else {
|
} else {
|
||||||
// User logged out; reset to null
|
// User logged out; reset to null
|
||||||
setUserCookie(undefined)
|
setUserCookie(undefined)
|
||||||
setAuthUser(null)
|
setAuthUser(null)
|
||||||
localStorage.removeItem(CACHED_USER_KEY)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
(e) => {
|
(e) => {
|
||||||
|
@ -97,29 +103,32 @@ export function AuthProvider(props: {
|
||||||
}, [setAuthUser])
|
}, [setAuthUser])
|
||||||
|
|
||||||
const uid = authUser?.user.id
|
const uid = authUser?.user.id
|
||||||
const username = authUser?.user.username
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (uid && username) {
|
if (uid) {
|
||||||
identifyUser(uid)
|
identifyUser(uid)
|
||||||
setUserProperty('username', username)
|
const userListener = listenForUser(uid, (user) => {
|
||||||
const userListener = listenForUser(uid, (user) =>
|
setAuthUser((currAuthUser) =>
|
||||||
setAuthUser((authUser) => {
|
currAuthUser && user ? { ...currAuthUser, user } : null
|
||||||
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
)
|
||||||
return { ...authUser!, user: user! }
|
})
|
||||||
})
|
|
||||||
)
|
|
||||||
const privateUserListener = listenForPrivateUser(uid, (privateUser) => {
|
const privateUserListener = listenForPrivateUser(uid, (privateUser) => {
|
||||||
setAuthUser((authUser) => {
|
setAuthUser((currAuthUser) =>
|
||||||
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
|
currAuthUser && privateUser ? { ...currAuthUser, privateUser } : null
|
||||||
return { ...authUser!, privateUser: privateUser! }
|
)
|
||||||
})
|
|
||||||
})
|
})
|
||||||
return () => {
|
return () => {
|
||||||
userListener()
|
userListener()
|
||||||
privateUserListener()
|
privateUserListener()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [uid, username, setAuthUser])
|
}, [uid, setAuthUser])
|
||||||
|
|
||||||
|
const username = authUser?.user.username
|
||||||
|
useEffect(() => {
|
||||||
|
if (username != null) {
|
||||||
|
setUserProperty('username', username)
|
||||||
|
}
|
||||||
|
}, [username])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AuthContext.Provider value={authUser}>{children}</AuthContext.Provider>
|
<AuthContext.Provider value={authUser}>{children}</AuthContext.Provider>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user