Pass page props user to auth provider if present (#724)
* Pass page props user to auth provider if present * Rename `user` -> `serverUser` * Don't load from local storage if server told us a user
This commit is contained in:
parent
fd308151b3
commit
5649161348
|
@ -1,4 +1,4 @@
|
||||||
import { createContext, useEffect } from 'react'
|
import { ReactNode, createContext, useEffect } from 'react'
|
||||||
import { User } from 'common/user'
|
import { User } from 'common/user'
|
||||||
import { onIdTokenChanged } from 'firebase/auth'
|
import { onIdTokenChanged } from 'firebase/auth'
|
||||||
import {
|
import {
|
||||||
|
@ -28,15 +28,20 @@ const ensureDeviceToken = () => {
|
||||||
return deviceToken
|
return deviceToken
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AuthContext = createContext<AuthUser>(null)
|
export const AuthContext = createContext<AuthUser>(undefined)
|
||||||
|
|
||||||
export function AuthProvider({ children }: any) {
|
|
||||||
const [authUser, setAuthUser] = useStateCheckEquality<AuthUser>(undefined)
|
|
||||||
|
|
||||||
|
export function AuthProvider(props: {
|
||||||
|
children: ReactNode
|
||||||
|
serverUser?: AuthUser
|
||||||
|
}) {
|
||||||
|
const { children, serverUser } = props
|
||||||
|
const [authUser, setAuthUser] = useStateCheckEquality<AuthUser>(serverUser)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (serverUser === undefined) {
|
||||||
const cachedUser = localStorage.getItem(CACHED_USER_KEY)
|
const cachedUser = localStorage.getItem(CACHED_USER_KEY)
|
||||||
setAuthUser(cachedUser && JSON.parse(cachedUser))
|
setAuthUser(cachedUser && JSON.parse(cachedUser))
|
||||||
}, [setAuthUser])
|
}
|
||||||
|
}, [setAuthUser, serverUser])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return onIdTokenChanged(auth, async (fbUser) => {
|
return onIdTokenChanged(auth, async (fbUser) => {
|
||||||
|
|
|
@ -79,7 +79,7 @@ function MyApp({ Component, pageProps }: AppProps) {
|
||||||
content="width=device-width, initial-scale=1, maximum-scale=1"
|
content="width=device-width, initial-scale=1, maximum-scale=1"
|
||||||
/>
|
/>
|
||||||
</Head>
|
</Head>
|
||||||
<AuthProvider>
|
<AuthProvider serverUser={pageProps.user}>
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
<Welcome {...pageProps} />
|
<Welcome {...pageProps} />
|
||||||
<Component {...pageProps} />
|
<Component {...pageProps} />
|
||||||
|
|
Loading…
Reference in New Issue
Block a user