|
1 | | -import React from "react"; |
2 | | -import { Link } from "react-router-dom"; |
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { Auth } from "aws-amplify"; |
| 3 | +import { Link, useHistory } from "react-router-dom"; |
3 | 4 | import { Nav, Navbar, NavItem } from "react-bootstrap"; |
4 | 5 | import { LinkContainer } from "react-router-bootstrap"; |
| 6 | +import { AppContext } from "./libs/contextLib"; |
| 7 | +import { onError } from "./libs/errorLib"; |
5 | 8 | import Routes from "./Routes"; |
6 | 9 | import "./App.css"; |
7 | 10 |
|
8 | 11 | function App() { |
| 12 | + const history = useHistory(); |
| 13 | + const [isAuthenticating, setIsAuthenticating] = useState(true); |
| 14 | + const [isAuthenticated, userHasAuthenticated] = useState(false); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + onLoad(); |
| 18 | + }, []); |
| 19 | + |
| 20 | + async function onLoad() { |
| 21 | + try { |
| 22 | + await Auth.currentSession(); |
| 23 | + userHasAuthenticated(true); |
| 24 | + } |
| 25 | + catch(e) { |
| 26 | + if (e !== 'No current user') { |
| 27 | + onError(e); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + setIsAuthenticating(false); |
| 32 | + } |
| 33 | + |
| 34 | + async function handleLogout() { |
| 35 | + await Auth.signOut(); |
| 36 | + |
| 37 | + userHasAuthenticated(false); |
| 38 | + |
| 39 | + history.push("/login"); |
| 40 | + } |
| 41 | + |
9 | 42 | return ( |
10 | | - <div className="App container"> |
11 | | - <Navbar fluid collapseOnSelect> |
12 | | - <Navbar.Header> |
13 | | - <Navbar.Brand> |
14 | | - <Link to="/">Scratch</Link> |
15 | | - </Navbar.Brand> |
16 | | - <Navbar.Toggle /> |
17 | | - </Navbar.Header> |
18 | | - <Navbar.Collapse> |
19 | | - <Nav pullRight> |
20 | | - <LinkContainer to="/signup"> |
21 | | - <NavItem>Signup</NavItem> |
22 | | - </LinkContainer> |
23 | | - <LinkContainer to="/login"> |
24 | | - <NavItem>Login</NavItem> |
25 | | - </LinkContainer> |
26 | | - </Nav> |
27 | | - </Navbar.Collapse> |
28 | | - </Navbar> |
29 | | - <Routes /> |
30 | | - </div> |
| 43 | + !isAuthenticating && ( |
| 44 | + <div className="App container"> |
| 45 | + <Navbar fluid collapseOnSelect> |
| 46 | + <Navbar.Header> |
| 47 | + <Navbar.Brand> |
| 48 | + <Link to="/">Scratch</Link> |
| 49 | + </Navbar.Brand> |
| 50 | + <Navbar.Toggle /> |
| 51 | + </Navbar.Header> |
| 52 | + <Navbar.Collapse> |
| 53 | + <Nav pullRight> |
| 54 | + {isAuthenticated ? ( |
| 55 | + <> |
| 56 | + <LinkContainer to="/settings"> |
| 57 | + <NavItem>Settings</NavItem> |
| 58 | + </LinkContainer> |
| 59 | + <NavItem onClick={handleLogout}>Logout</NavItem> |
| 60 | + </> |
| 61 | + ) : ( |
| 62 | + <> |
| 63 | + <LinkContainer to="/signup"> |
| 64 | + <NavItem>Signup</NavItem> |
| 65 | + </LinkContainer> |
| 66 | + <LinkContainer to="/login"> |
| 67 | + <NavItem>Login</NavItem> |
| 68 | + </LinkContainer> |
| 69 | + </> |
| 70 | + )} |
| 71 | + </Nav> |
| 72 | + </Navbar.Collapse> |
| 73 | + </Navbar> |
| 74 | + <AppContext.Provider |
| 75 | + value={{ isAuthenticated, userHasAuthenticated }} |
| 76 | + > |
| 77 | + <Routes /> |
| 78 | + </AppContext.Provider> |
| 79 | + </div> |
| 80 | + ) |
31 | 81 | ); |
32 | 82 | } |
33 | 83 |
|
|
0 commit comments