listenForLogin: avoid race condition
This commit is contained in:
		
							parent
							
								
									cbbc6ef33b
								
							
						
					
					
						commit
						839d3c4c15
					
				|  | @ -13,7 +13,7 @@ export const resolveMarket = cloudFunction('resolveMarket') | ||||||
| 
 | 
 | ||||||
| export const sellBet = cloudFunction('sellBet') | export const sellBet = cloudFunction('sellBet') | ||||||
| 
 | 
 | ||||||
| export const createUser = () => | export const createUser: () => Promise<User | null> = () => | ||||||
|   cloudFunction('createUser')({}).then( |   cloudFunction('createUser')({}) | ||||||
|     (r) => (r.data as any)?.user as User | undefined |     .then((r) => (r.data as any)?.user || null) | ||||||
|   ) |     .catch(() => null) | ||||||
|  |  | ||||||
|  | @ -54,11 +54,14 @@ export function listenForUser(userId: string, setUser: (user: User) => void) { | ||||||
| 
 | 
 | ||||||
| const CACHED_USER_KEY = 'CACHED_USER_KEY' | const CACHED_USER_KEY = 'CACHED_USER_KEY' | ||||||
| 
 | 
 | ||||||
|  | // used to avoid weird race condition
 | ||||||
|  | let createUserPromise: Promise<User | null> | undefined = undefined | ||||||
|  | 
 | ||||||
| export function listenForLogin(onUser: (user: User | null) => void) { | export function listenForLogin(onUser: (user: User | null) => void) { | ||||||
|   const cachedUser = localStorage.getItem(CACHED_USER_KEY) |   const cachedUser = localStorage.getItem(CACHED_USER_KEY) | ||||||
|   onUser(cachedUser ? JSON.parse(cachedUser) : null) |   onUser(cachedUser ? JSON.parse(cachedUser) : null) | ||||||
| 
 | 
 | ||||||
|   if (!cachedUser) createUser().catch(() => {}) // warm up cloud function
 |   if (!cachedUser) createUser() // warm up cloud function
 | ||||||
| 
 | 
 | ||||||
|   return onAuthStateChanged(auth, async (fbUser) => { |   return onAuthStateChanged(auth, async (fbUser) => { | ||||||
|     if (fbUser) { |     if (fbUser) { | ||||||
|  | @ -66,7 +69,11 @@ export function listenForLogin(onUser: (user: User | null) => void) { | ||||||
| 
 | 
 | ||||||
|       if (!user) { |       if (!user) { | ||||||
|         // User just created an account; save them to our database.
 |         // User just created an account; save them to our database.
 | ||||||
|         user = (await createUser()) || null |         if (!createUserPromise) { | ||||||
|  |           createUserPromise = createUser() | ||||||
|  |           console.log('this should only be logged once') | ||||||
|  |         } | ||||||
|  |         user = (await createUserPromise) || null | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       onUser(user) |       onUser(user) | ||||||
|  | @ -78,6 +85,7 @@ export function listenForLogin(onUser: (user: User | null) => void) { | ||||||
|       // User logged out; reset to null
 |       // User logged out; reset to null
 | ||||||
|       onUser(null) |       onUser(null) | ||||||
|       localStorage.removeItem(CACHED_USER_KEY) |       localStorage.removeItem(CACHED_USER_KEY) | ||||||
|  |       createUserPromise = undefined | ||||||
|     } |     } | ||||||
|   }) |   }) | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user