Skip to content

Commit df30b3f

Browse files
committed
add isConnecting status to Auth
1 parent f349dba commit df30b3f

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/features/auth/stores/authModalStore.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ type AuthError = {
88
}
99
interface AuthModalState {
1010
isAuthModalOpen: boolean
11+
isConnecting: boolean
1112
authError: AuthError | null
1213
openAuthModal: () => void
1314
closeAuthModal: () => void
1415
setAuthError: (error: AuthError | null) => void
16+
setConnecting: (isConnecting: boolean) => void
1517
}
1618

1719
export const AuthModalStore = create<AuthModalState>((set) => ({
1820
isAuthModalOpen: false,
1921
authError: null,
22+
isConnecting: false,
2023
setAuthError: (error) => set({ authError: error }),
2124
openAuthModal: () => set({ isAuthModalOpen: true }),
2225
closeAuthModal: () => set({ isAuthModalOpen: false }),
26+
setConnecting: (isConnecting) => set({ isConnecting }),
2327
}))

src/providers/AuthProvider.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,41 @@ import { firebaseAuth } from 'src/lib/firebase'
88
export const AuthProvider = ({ children }: { children: React.ReactNode }) => {
99
const navigate = useNavigate()
1010
const [searchParams] = useSearchParams()
11-
const { closeAuthModal, initState, setAuthError, openAuthModal } = useAuth()
11+
const { closeAuthModal, initState, setAuthError, openAuthModal, setConnecting } = useAuth()
1212

1313
const connectTheUser = useCallback((token?: string | null, provider?: string | null) => {
1414
const allowedProviders = ['google', 'github']
1515
if ((provider && !allowedProviders.includes(provider)) || !token) {
1616
return Promise.resolve()
1717
}
1818

19+
setConnecting(true)
1920
const authProvider =
2021
provider === 'google'
2122
? GoogleAuthProvider.credential(null, token)
2223
: GithubAuthProvider.credential(token)
2324

24-
return signInWithCredential(firebaseAuth, authProvider).then((userCredential) => {
25-
const user = userCredential.user
25+
return signInWithCredential(firebaseAuth, authProvider)
26+
.then((userCredential) => {
27+
const user = userCredential.user
2628

27-
initState({
28-
user: {
29-
id: user.uid,
30-
name: user.displayName || 'Anonymous',
31-
imageURL: user.photoURL || '',
32-
},
33-
providerId: authProvider.providerId,
29+
initState({
30+
user: {
31+
id: user.uid,
32+
name: user.displayName || 'Anonymous',
33+
imageURL: user.photoURL || '',
34+
},
35+
providerId: authProvider.providerId,
36+
})
37+
if (user.displayName) {
38+
toast(`Welcome, ${user.displayName}`, { theme: 'successToast' })
39+
}
40+
closeAuthModal()
41+
navigate(window.location.pathname, { replace: true })
42+
})
43+
.finally(() => {
44+
setConnecting(false)
3445
})
35-
if (user.displayName) {
36-
toast(`Welcome, ${user.displayName}`, { theme: 'successToast' })
37-
}
38-
closeAuthModal()
39-
navigate(window.location.pathname, { replace: true })
40-
})
4146
}, [])
4247

4348
/**

0 commit comments

Comments
 (0)