|
1 |
| -import React, { Component } from 'react'; |
2 |
| -import logo from './logo.svg'; |
| 1 | +import React from 'react'; |
| 2 | +import { Router, Link, navigate } from '@reach/router'; |
3 | 3 | import './App.css';
|
| 4 | +import { useNetlifyIdentity } from './useNetlifyIdentity'; |
4 | 5 |
|
5 |
| -class LambdaDemo extends Component { |
6 |
| - constructor(props) { |
7 |
| - super(props); |
8 |
| - this.state = { loading: false, msg: null }; |
9 |
| - } |
| 6 | +let IdentityContext = React.createContext(); |
10 | 7 |
|
11 |
| - handleClick = api => e => { |
12 |
| - e.preventDefault(); |
| 8 | +function PrivateRoute(props) { |
| 9 | + const identity = React.useContext(IdentityContext); |
| 10 | + let { as: Comp, ...rest } = props; |
| 11 | + return identity.user ? ( |
| 12 | + <Comp {...rest} /> |
| 13 | + ) : ( |
| 14 | + <div> |
| 15 | + <h3>You are trying to view a protected page. Please log in</h3> |
| 16 | + <Login /> |
| 17 | + </div> |
| 18 | + ); |
| 19 | +} |
13 | 20 |
|
14 |
| - this.setState({ loading: true }); |
15 |
| - fetch('/.netlify/functions/' + api) |
16 |
| - .then(response => response.json()) |
17 |
| - .then(json => this.setState({ loading: false, msg: json.msg })); |
| 21 | +function Login() { |
| 22 | + const { loginUser, signupUser } = React.useContext(IdentityContext); |
| 23 | + const formRef = React.useRef(); |
| 24 | + const [msg, setMsg] = React.useState(''); |
| 25 | + const signup = () => { |
| 26 | + const email = formRef.current.email.value; |
| 27 | + const password = formRef.current.password.value; |
| 28 | + signupUser(email, password) |
| 29 | + .then(user => { |
| 30 | + console.log('Success! Signed up', user); |
| 31 | + navigate('/dashboard'); |
| 32 | + }) |
| 33 | + .catch(err => console.error(err) || setMsg('Error: ' + err.message)); |
18 | 34 | };
|
| 35 | + return ( |
| 36 | + <form |
| 37 | + ref={formRef} |
| 38 | + onSubmit={e => { |
| 39 | + e.preventDefault(); |
| 40 | + const email = e.target.email.value; |
| 41 | + const password = e.target.password.value; |
| 42 | + loginUser(email, password) |
| 43 | + .then(user => { |
| 44 | + console.log('Success! Logged in', user); |
| 45 | + navigate('/dashboard'); |
| 46 | + }) |
| 47 | + .catch(err => console.error(err) || setMsg('Error: ' + err.message)); |
| 48 | + }} |
| 49 | + > |
| 50 | + <div |
| 51 | + style={{ textAlign: 'left', margin: '0 auto', display: 'inline-block' }} |
| 52 | + > |
| 53 | + <div> |
| 54 | + <label> |
| 55 | + Email: |
| 56 | + <input type="email" name="email" /> |
| 57 | + </label> |
| 58 | + </div> |
| 59 | + <div> |
| 60 | + <label> |
| 61 | + Password: |
| 62 | + <input type="password" name="password" /> |
| 63 | + </label> |
| 64 | + </div> |
| 65 | + <div> |
| 66 | + <input type="submit" value="Log in" /> |
| 67 | + <button onClick={signup}>Sign Up </button> |
| 68 | + {msg && <pre>{msg}</pre>} |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + </form> |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +function Home() { |
| 76 | + return ( |
| 77 | + <div> |
| 78 | + <h3>Welcome to the Home page!</h3> |
| 79 | + <p>this is not behind an authentication wall</p> |
| 80 | + </div> |
| 81 | + ); |
| 82 | +} |
| 83 | + |
| 84 | +function About() { |
| 85 | + return <div>About</div>; |
| 86 | +} |
| 87 | + |
| 88 | +function Dashboard() { |
| 89 | + const props = React.useContext(IdentityContext); |
| 90 | + console.log({ props }); |
| 91 | + const { isConfirmedUser, authedFetch } = props; |
| 92 | + const [msg, setMsg] = React.useState('Click to load something'); |
| 93 | + const handler = () => { |
| 94 | + authedFetch.get('/.netlify/functions/authEndPoint').then(setMsg); |
| 95 | + }; |
| 96 | + return ( |
| 97 | + <div> |
| 98 | + <h3>This is a Protected Dashboard!</h3> |
| 99 | + {!isConfirmedUser && <pre>You have not confirmed your email.</pre>} |
| 100 | + <hr /> |
| 101 | + <div> |
| 102 | + You can try pinging our authenticated API here. If you are logged in, |
| 103 | + you should be able to see a `user` info here. |
| 104 | + <button onClick={handler}>Ping authenticated API</button> |
| 105 | + <pre>{JSON.stringify(msg, null, 2)}</pre> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + ); |
| 109 | +} |
| 110 | +function Nav() { |
| 111 | + const { isLoggedIn } = React.useContext(IdentityContext); |
| 112 | + return ( |
| 113 | + <nav> |
| 114 | + <Link to="/">Home</Link> |<Link to="dashboard">Dashboard</Link> | |
| 115 | + <span> |
| 116 | + {isLoggedIn ? <Logout /> : <Link to="login">Log In/Sign Up</Link>} |
| 117 | + </span> |
| 118 | + </nav> |
| 119 | + ); |
| 120 | +} |
| 121 | +function Logout() { |
| 122 | + const { logoutUser } = React.useContext(IdentityContext); |
| 123 | + return <button onClick={logoutUser}>You are signed in. Log Out</button>; |
| 124 | +} |
19 | 125 |
|
20 |
| - render() { |
21 |
| - const { loading, msg } = this.state; |
22 |
| - |
23 |
| - return ( |
24 |
| - <p> |
25 |
| - <button onClick={this.handleClick('hello')}> |
26 |
| - {loading ? 'Loading...' : 'Call Lambda'} |
27 |
| - </button> |
28 |
| - <button onClick={this.handleClick('async-chuck-norris')}> |
29 |
| - {loading ? 'Loading...' : 'Call Async Lambda'} |
30 |
| - </button> |
31 |
| - <br /> |
32 |
| - <span>{msg}</span> |
33 |
| - </p> |
34 |
| - ); |
35 |
| - } |
36 |
| -} |
37 |
| - |
38 |
| -class App extends Component { |
39 |
| - render() { |
40 |
| - return ( |
| 126 | +function App() { |
| 127 | + const identity = useNetlifyIdentity( |
| 128 | + 'https://optimistic-dubinsky-dacadd.netlify.com' |
| 129 | + ); |
| 130 | + return ( |
| 131 | + <IdentityContext.Provider value={identity}> |
41 | 132 | <div className="App">
|
42 |
| - <header className="App-header"> |
43 |
| - <img src={logo} className="App-logo" alt="logo" /> |
44 |
| - <p> |
45 |
| - Edit <code>src/App.js</code> and save to reload. |
46 |
| - </p> |
47 |
| - <LambdaDemo /> |
48 |
| - </header> |
| 133 | + <h1>Netlify Identity + Reach Router demo</h1> |
| 134 | + <Nav /> |
| 135 | + <Router> |
| 136 | + <Home path="/" /> |
| 137 | + <About path="/about" /> |
| 138 | + <Login path="/login" /> |
| 139 | + <PrivateRoute as={Dashboard} path="/dashboard" /> |
| 140 | + </Router> |
49 | 141 | </div>
|
50 |
| - ); |
51 |
| - } |
| 142 | + </IdentityContext.Provider> |
| 143 | + ); |
52 | 144 | }
|
53 | 145 |
|
54 | 146 | export default App;
|
0 commit comments