-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.js
86 lines (80 loc) · 1.99 KB
/
account.js
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
import React, { useEffect } from 'react';
import { Router } from '@reach/router';
import { useAuth } from 'gatsby-theme-auth0-minimal';
import { Link } from 'gatsby';
const MyAccount = () => <p>My Very Private Info</p>;
const Settings = () => <p>My personal Settings</p>;
const Billing = () => <p> My Billing info</p>;
const PrivateRoute = ({ component: Component, ...rest }) => {
const { login, isAuthenticated, isLoading } = useAuth();
if (isLoading) {
return <p>Loading...</p>;
}
return isAuthenticated ? (
<Component {...rest} />
) : (
<div>
<p>You have to login to view this page</p>
<button
type="button"
onClick={e => {
login();
}}
>
Login
</button>
</div>
);
};
const Account = () => {
const {
checkSession,
login,
logout,
isAuthenticated,
authState: { user },
} = useAuth();
useEffect(() => {
if (localStorage.getItem('isLoggedIn') === 'true') {
checkSession();
}
}, [checkSession]);
return (
<>
<nav>
<Link to="/">Home</Link>
<Link to="/account/">My Account</Link>{' '}
<Link to="/account/settings/">Settings</Link>{' '}
<Link to="/account/billing/">Billing</Link>{' '}
{isAuthenticated ? (
<a
href="#logout"
onClick={e => {
e.preventDefault();
logout();
}}
>
Log Out
</a>
) : (
<a
href="#login"
onClick={e => {
e.preventDefault();
login();
}}
>
Log In
</a>
)}
</nav>
{user && <pre>{JSON.stringify(user, null, 2)}</pre>}
<Router>
<PrivateRoute path="/account/" component={MyAccount} />
<PrivateRoute path="/account/settings" component={Settings} />
<PrivateRoute path="/account/billing" component={Billing} />
</Router>
</>
);
};
export default Account;