generated from epilande/gatsby-starter-theme-ts
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathprotected.tsx
90 lines (77 loc) · 2.31 KB
/
protected.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/** @jsx jsx */
import { jsx } from "theme-ui";
import React from "react";
import { Link } from "gatsby";
import { auth, useAuth } from "gatsby-theme-firebase";
import { Gallery } from "gatsby-theme-gallery";
import Button from "gatsby-theme-firebase/src/components/Button";
import LoginModal from "../components/LoginModal";
import { Code, CodeBlock, Header, A } from "../components/Styles";
import { Layout } from "../components";
const ProtectedDemo = () => {
const [toggleLogin, setToggleLogin] = React.useState(false);
const { isLoading, isLoggedIn, profile } = useAuth();
if (isLoading) {
return null;
}
return (
<Layout>
<Header title="Protected Demo" isLoggedIn={isLoggedIn} />
{isLoggedIn ? (
<div>
<p sx={{ mt: 3, mb: 2 }}>Hello {profile!.displayName}.</p>
<p sx={{ mb: 3 }}>
This gallery is generated with{" "}
<A
href="https://github.com/epilande/gatsby-theme-gallery"
target="_blank"
rel="noopener noreferrer"
>
<Code>{`gatsby-theme-gallery`}</Code>
</A>
.
</p>
<Gallery />
<h2 sx={{ mt: 3 }}>Code Sample</h2>
<CodeBlock
sx={{ mt: 2, mb: 0 }}
link={
"https://github.com/epilande/gatsby-theme-firebase/blob/master/demo/src/pages/protected.tsx"
}
title="protected.tsx"
>
{`import { useAuth } from "gatsby-theme-firebase";
const ProtectedPage = () => {
const { isLoggedIn } = useAuth();
return isLoggedIn ? <Gallery /> : <LoginButton />
}
`}
</CodeBlock>
<Link sx={{ display: "inline-block", mt: 3, mr: 3 }} to="/">
<Button>Back</Button>
</Link>
<Button
onClick={() => {
auth.signOut();
}}
>
Sign Out
</Button>
</div>
) : (
<div>
<p sx={{ my: 3 }}>You must be logged in to see this page.</p>
<Button
onClick={() => {
setToggleLogin(true);
}}
>
Login
</Button>
</div>
)}
{toggleLogin && <LoginModal setToggleLogin={setToggleLogin} />}
</Layout>
);
};
export default ProtectedDemo;