From 92f1c6e9ae27996cac2de237d14905678f9547e6 Mon Sep 17 00:00:00 2001 From: Dominic Garms Date: Sat, 27 Mar 2021 11:10:49 +0800 Subject: [PATCH 1/4] add options to useAuthState hook "onUserChanged" Add possibility to pass options for onUserLoaded, a function which is called if a user auth state changed. This might close https://github.com/CSFrequency/react-firebase-hooks/issues/33 --- auth/useAuthState.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/auth/useAuthState.ts b/auth/useAuthState.ts index fad14ed..c35e9eb 100644 --- a/auth/useAuthState.ts +++ b/auth/useAuthState.ts @@ -1,5 +1,5 @@ import firebase from 'firebase/app'; -import { useEffect, useMemo } from 'react'; +import { useEffect, useMemo, useState } from 'react'; import { LoadingHook, useLoadingValue } from '../util'; export type AuthStateHook = LoadingHook< @@ -7,14 +7,20 @@ export type AuthStateHook = LoadingHook< firebase.auth.Error >; -export default (auth: firebase.auth.Auth): AuthStateHook => { +export default (auth: firebase.auth.Auth, options: {onUserChanged?: Promise}): AuthStateHook => { const { error, loading, setError, setValue, value } = useLoadingValue< firebase.User | null, firebase.auth.Error >(() => auth.currentUser); useEffect(() => { - const listener = auth.onAuthStateChanged(setValue, setError); + const listener = auth.onAuthStateChanged(async (user) => { + if(typeof options?.onUserChanged === 'function') { + // onUserLoaded function to process custom claims on any other trigger function + await options.onUserChanged(user) + } + setValue(user); + }, setError); return () => { listener(); From b6d7a841a8efb6b5fa1094e4c2e1508fbd5e383e Mon Sep 17 00:00:00 2001 From: Dominic Garms Date: Sat, 27 Mar 2021 11:11:51 +0800 Subject: [PATCH 2/4] Update useAuthState.ts --- auth/useAuthState.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auth/useAuthState.ts b/auth/useAuthState.ts index c35e9eb..2dfaa70 100644 --- a/auth/useAuthState.ts +++ b/auth/useAuthState.ts @@ -1,5 +1,5 @@ import firebase from 'firebase/app'; -import { useEffect, useMemo, useState } from 'react'; +import { useEffect, useMemo } from 'react'; import { LoadingHook, useLoadingValue } from '../util'; export type AuthStateHook = LoadingHook< From 8a0c2ec27b307eb858f5e4929a5191ba16af9bc5 Mon Sep 17 00:00:00 2001 From: Dominic Garms Date: Sat, 27 Mar 2021 11:13:31 +0800 Subject: [PATCH 3/4] Update useAuthState.ts --- auth/useAuthState.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/auth/useAuthState.ts b/auth/useAuthState.ts index 2dfaa70..17ae40e 100644 --- a/auth/useAuthState.ts +++ b/auth/useAuthState.ts @@ -7,7 +7,7 @@ export type AuthStateHook = LoadingHook< firebase.auth.Error >; -export default (auth: firebase.auth.Auth, options: {onUserChanged?: Promise}): AuthStateHook => { +export default (auth: firebase.auth.Auth, options: {onUserChanged?: (user?: firebase.User) => Promise}): AuthStateHook => { const { error, loading, setError, setValue, value } = useLoadingValue< firebase.User | null, firebase.auth.Error From a898bb5cd3da9cd6affe058b0451655f9e31ab53 Mon Sep 17 00:00:00 2001 From: Dominic Garms Date: Sun, 25 Apr 2021 15:47:28 +0800 Subject: [PATCH 4/4] Update useAuthState.ts --- auth/useAuthState.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/auth/useAuthState.ts b/auth/useAuthState.ts index 17ae40e..088d3de 100644 --- a/auth/useAuthState.ts +++ b/auth/useAuthState.ts @@ -17,7 +17,11 @@ export default (auth: firebase.auth.Auth, options: {onUserChanged?: (user?: fire const listener = auth.onAuthStateChanged(async (user) => { if(typeof options?.onUserChanged === 'function') { // onUserLoaded function to process custom claims on any other trigger function - await options.onUserChanged(user) + try { + await options.onUserChanged(user) + } catch(e) { + setError(e) + } } setValue(user); }, setError);