1+ import { auth , User } from 'firebase/app' ;
12import * as React from 'react' ;
2- import { auth , performance , User } from 'firebase/app' ;
3- import { useUser , useFirebaseApp } from './index' ;
4- const { Suspense, useState, useLayoutEffect } = React ;
3+ import { user } from 'rxfire/auth' ;
4+ import { useObservable , useFirebaseApp , ReactFireOptions } from '..' ;
55
6- export interface SuspensePerfProps {
7- children : React . ReactNode ;
8- traceId : string ;
9- fallback : React . ReactNode ;
10- firePerf ?: performance . Performance ; // TODO(jeff): Add firePerf here when it's available
11- }
12-
13- function getPerfFromContext ( ) : performance . Performance {
6+ function getAuthFromContext ( ) : auth . Auth {
147 const firebaseApp = useFirebaseApp ( ) ;
158
169 if ( ! firebaseApp ) {
@@ -19,39 +12,34 @@ function getPerfFromContext(): performance.Performance {
1912 ) ;
2013 }
2114
22- const perfFunc = firebaseApp . performance ;
15+ const authFunc = firebaseApp . auth ;
2316
24- if ( ! perfFunc || ! perfFunc ( ) ) {
17+ if ( ! authFunc || ! authFunc ( ) ) {
2518 throw new Error (
26- "No perf object off of Firebase. Did you forget to import 'firebase/performance ' in a component?"
19+ "No auth object off of Firebase. Did you forget to import 'firebase/auth ' in a component?"
2720 ) ;
2821 }
2922
30- return perfFunc ( ) ;
23+ return authFunc ( ) ;
3124}
3225
33- export function SuspenseWithPerf ( {
34- children,
35- traceId,
36- fallback,
37- firePerf
38- } : SuspensePerfProps ) {
39- firePerf = firePerf || getPerfFromContext ( ) ;
40- const trace = React . useMemo ( ( ) => firePerf . trace ( traceId ) , [ traceId ] ) ;
41-
42- const Fallback = ( ) => {
43- useLayoutEffect ( ( ) => {
44- trace . start ( ) ;
45-
46- return ( ) => {
47- trace . stop ( ) ;
48- } ;
49- } , [ ] ) ;
50-
51- return < > { fallback } </ > ;
52- } ;
53-
54- return < Suspense fallback = { < Fallback /> } > { children } </ Suspense > ;
26+ /**
27+ * Subscribe to Firebase auth state changes, including token refresh
28+ *
29+ * @param auth - the [firebase.auth](https://firebase.google.com/docs/reference/js/firebase.auth) object
30+ * @param options
31+ */
32+ export function useUser < T = unknown > (
33+ auth ?: auth . Auth ,
34+ options ?: ReactFireOptions < T >
35+ ) : User | T {
36+ auth = auth || getAuthFromContext ( ) ;
37+
38+ return useObservable (
39+ user ( auth ) ,
40+ 'user' ,
41+ options ? options . startWithValue : undefined
42+ ) ;
5543}
5644
5745export interface AuthCheckProps {
@@ -69,7 +57,7 @@ export function AuthCheck({
6957} : AuthCheckProps ) : React . ReactNode {
7058 const user = useUser < User > ( auth ) ;
7159
72- useLayoutEffect ( ( ) => {
60+ React . useLayoutEffect ( ( ) => {
7361 // TODO(jeff) see if this actually works
7462 if ( requiredClaims ) {
7563 throw user . getIdTokenResult ( ) . then ( idTokenResult => {
0 commit comments