-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathAuth.tsx
74 lines (61 loc) · 1.93 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
69
70
71
72
73
74
import * as React from 'react';
import { useAuth, useSigninCheck } from 'reactfire';
import { WideButton } from '../display/Button';
import { CardSection } from '../display/Card';
import { LoadingSpinner } from '../display/LoadingSpinner';
import { GoogleAuthProvider, signInWithPopup } 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 { status, data: signInCheckResult } = useSigninCheck();
if (!children) {
throw new Error('Children must be provided');
}
if (status === 'loading') {
return <LoadingSpinner />;
} else if (signInCheckResult.signedIn === true) {
return children as JSX.Element;
}
return fallback;
};
const UserDetails = ({ user }) => {
const auth = useAuth();
return (
<>
<CardSection title="Displayname">{user.displayName}</CardSection>
<CardSection title="Providers">
<ul>
{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 = () => {
const { status, data: signinResult } = useSigninCheck();
if (status === 'loading') {
return <LoadingSpinner />;
}
const { signedIn, user } = signinResult;
if (signedIn === true) {
return <UserDetails user={user} />;
} else {
return <SignInForm />;
}
};