-
Notifications
You must be signed in to change notification settings - Fork 410
/
Copy pathAuth.tsx
68 lines (58 loc) · 1.88 KB
/
Auth.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import * as React from 'react';
import { useAuth, useUser, SuspenseWithPerf, useSigninCheck } from 'reactfire';
import { WideButton } from '../display/Button';
import { CardSection } from '../display/Card';
import { LoadingSpinner } from '../display/LoadingSpinner';
import { GoogleAuthProvider, signInWithPopup, User } from "firebase/auth";
const signOut = auth => auth.signOut().then(() => console.log('signed out'));
const signIn = async auth => {
const provider = new GoogleAuthProvider();
await signInWithPopup(auth, provider);
}
export const AuthWrapper = ({ children, fallback }: React.PropsWithChildren<{ fallback: JSX.Element }>): JSX.Element => {
const { data: signInCheckResult } = useSigninCheck();
if (!children) {
throw new Error('Children must be provided');
}
if (signInCheckResult.signedIn === true) {
return children as JSX.Element;
} else {
return fallback;
}
};
const UserDetails = () => {
const auth = useAuth();
const {data: user} = useUser();
return (
<>
<CardSection title="Displayname">{(user as User).displayName}</CardSection>
<CardSection title="Providers">
<ul>
{(user as User).providerData?.map(profile => (
<li key={profile?.providerId}>{profile?.providerId}</li>
))}
</ul>
</CardSection>
<CardSection title="Sign Out">
<WideButton label="Sign Out" onClick={() => signOut(auth)} />
</CardSection>
</>
);
};
const SignInForm = () => {
const auth = useAuth();
return (
<CardSection title="Sign-in form">
<WideButton label="Sign in with Google" onClick={() => signIn(auth)} />
</CardSection>
);
};
export const Auth = () => {
return (
<SuspenseWithPerf traceId={'firebase-user-wait'} fallback={<LoadingSpinner/>}>
<AuthWrapper fallback={<SignInForm />}>
<UserDetails />
</AuthWrapper>
</SuspenseWithPerf>
);
};