-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathApp.tsx
273 lines (267 loc) · 7.73 KB
/
App.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import React from 'react';
import { Router, Link, navigate } from '@reach/router';
import './App.css';
import {
useIdentityContext,
IdentityContextProvider,
// Settings,
} from 'react-netlify-identity';
import useLoading from './useLoading';
type MaybePathProps = { path?: string };
function PrivateRoute(
props: React.PropsWithoutRef<
MaybePathProps & { as: React.ComponentType<MaybePathProps> }
>
) {
const identity = useIdentityContext();
let { as: Comp } = props;
return identity.user ? (
<Comp />
) : (
<div>
<h3>You are trying to view a protected page. Please log in</h3>
<Login />
</div>
);
}
// eslint-disable-next-line
function Login({ }: MaybePathProps) {
const {
loginUser,
signupUser,
settings,
loginProvider,
} = useIdentityContext();
const formRef = React.useRef<HTMLFormElement>(null!);
const [msg, setMsg] = React.useState('');
const [isLoading, load] = useLoading();
const signup = () => {
const email = formRef.current.email.value;
const password = formRef.current.password.value;
load(
signupUser(email, password, {
data: 'signed up thru react-netlify-identity',
})
)
.then(user => {
console.log('Success! Signed up', user);
navigate('/dashboard');
})
.catch(err => void console.error(err) || setMsg('Error: ' + err.message));
};
return (
<form
ref={formRef}
onSubmit={e => {
e.preventDefault();
const email = formRef.current.email.value;
const password = formRef.current.password.value;
load(loginUser(email, password))
.then(user => {
console.log('Success! Logged in', user);
navigate('/dashboard');
})
.catch(
err => void console.error(err) || setMsg('Error: ' + err.message)
);
}}
>
<div>
<label>
Email:
<input type="email" name="email" />
</label>
</div>
<div>
<label>
Password:
<input type="password" name="password" />
</label>
</div>
{isLoading ? (
<Spinner />
) : (
<div>
<input type="submit" value="Log in" />
<button onClick={signup}>Sign Up </button>
{msg && <pre>{msg}</pre>}
</div>
)}
{settings && <pre>{JSON.stringify(settings, null, 2)}</pre>}
{settings && settings.external.bitbucket && (
<button className="btn" onClick={() => loginProvider('bitbucket')}>
bitbucket
</button>
)}
{settings && settings.external.github && (
<button
className="btn"
style={{ background: 'lightblue' }}
onClick={() => loginProvider('github')}
>
GitHub
</button>
)}
{settings && settings.external.gitlab && (
<button
className="btn"
style={{ background: 'darkgreen' }}
onClick={() => loginProvider('gitlab')}
>
Gitlab
</button>
)}
{settings && settings.external.google && (
<button
className="btn"
style={{ background: 'lightsalmon' }}
onClick={() => loginProvider('google')}
>
Google
</button>
)}
</form>
);
}
// eslint-disable-next-line
function Home({ }: MaybePathProps) {
return (
<div>
<h3>Welcome to the Home page!</h3>
<p>
this is a <b>Public Page</b>, not behind an authentication wall
</p>
<div style={{ backgroundColor: '#EEE', padding: '1rem' }}>
<div>
<a
href={`https://app.netlify.com/start/deploy?repository=https://github.com/netlify/create-react-app-lambda/tree/reachRouterAndGoTrueDemo&stack=cms`}
>
<img
src="https://www.netlify.com/img/deploy/button.svg"
alt="Deploy to Netlify"
/>
</a>
</div>
This demo is{' '}
<a href="https://github.com/netlify/create-react-app-lambda/tree/reachRouterAndGoTrueDemo">
Open Source.
</a>{' '}
</div>
</div>
);
}
// eslint-disable-next-line
function About({ }: MaybePathProps) {
return <div>About</div>;
}
// eslint-disable-next-line
function Dashboard({ }: MaybePathProps) {
const props = useIdentityContext();
const { isConfirmedUser, authedFetch } = props;
const [isLoading, load] = useLoading();
const [msg, setMsg] = React.useState('Click to load something');
const handler = () => {
load(authedFetch.get('/.netlify/functions/authEndPoint')).then(setMsg);
};
return (
<div>
<h3>This is a Protected Dashboard!</h3>
{!isConfirmedUser && (
<pre style={{ backgroundColor: 'papayawhip' }}>
You have not confirmed your email. Please confirm it before you ping
the API.
</pre>
)}
<hr />
<div>
<p>You can try pinging our authenticated API here.</p>
<p>
If you are logged in, you should be able to see a `user` info here.
</p>
<button onClick={handler}>Ping authenticated API</button>
{isLoading ? <Spinner /> : <pre>{JSON.stringify(msg, null, 2)}</pre>}
</div>
</div>
);
}
function Spinner() {
return (
<div className="sk-folding-cube">
<div className="sk-cube1 sk-cube" />
<div className="sk-cube2 sk-cube" />
<div className="sk-cube4 sk-cube" />
<div className="sk-cube3 sk-cube" />
</div>
);
}
function Nav() {
const { isLoggedIn } = useIdentityContext();
return (
<nav>
<Link to="/">Home</Link> | <Link to="dashboard">Dashboard</Link>
{' | '}
<span>
{isLoggedIn ? <Logout /> : <Link to="login">Log In/Sign Up</Link>}
</span>
</nav>
);
}
function Logout() {
const { logoutUser } = useIdentityContext();
return <button onClick={logoutUser}>You are signed in. Log Out</button>;
}
function App() {
// TODO: SUPPLY A URL EITHER FROM ENVIRONMENT VARIABLES OR SOME OTHER STRATEGY
// e.g. 'https://unruffled-roentgen-04c3b8.netlify.com'
const domainToUse =
new URL(window.location.origin).hostname === 'localhost'
? 'http://react-netlify-identity.netlify.com'
: window.location.origin;
const [url, setUrl] = React.useState(domainToUse);
return (
<IdentityContextProvider url={url}>
<div className="App">
<div className="Appheader">
<h1 className="title">
<span>Netlify Identity</span>
<span className="italic">&</span> <span>Reach Router</span>
</h1>
<label>
<a href="https://www.netlify.com/docs/identity/">
Netlify Identity
</a>{' '}
Instance:{' '}
<input
type="text"
placeholder="your instance here e.g. https://unruffled-roentgen-04c3b8.netlify.com"
value={url}
onChange={e => setUrl(e.target.value)}
size={50}
/>
<div>
<div style={{ display: 'inline-block' }}>
{window.location.hostname === 'localhost' ? (
<pre>WARNING: this demo doesn't work on localhost</pre>
) : (
<pre>
your instance here e.g.
https://unruffled-roentgen-04c3b8.netlify.com
</pre>
)}
</div>
</div>
</label>
</div>
<Nav />
<Router>
<Home path="/" />
<About path="/about" />
<Login path="/login" />
<PrivateRoute as={Dashboard} path="/dashboard" />
</Router>
</div>
<footer>last updated react-netlify-identity v0.1.7</footer>
</IdentityContextProvider>
);
}
export default App;