Skip to content

Commit c668855

Browse files
committed
Working on building app
1 parent 3ac619e commit c668855

File tree

5 files changed

+121
-12
lines changed

5 files changed

+121
-12
lines changed

src/App.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
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, withRouter } from "react-router-dom";
34
import { Nav, Navbar, NavItem } from "react-bootstrap";
45
import { LinkContainer } from "react-router-bootstrap";
56
import Routes from "./Routes";
67
import "./App.css";
78

89
function App() {
10+
const [isAuthenticating, setIsAuthenticating] = useState(true);
11+
const [isAuthenticated, userHasAuthenticated] = useState(false);
12+
13+
useEffect(() => {
14+
onLoad();
15+
}, []);
16+
17+
async function onLoad() {
18+
try {
19+
await Auth.currentSession();
20+
userHasAuthenticated(true);
21+
}
22+
catch(e) {
23+
if (e !== 'No current user') {
24+
alert(e);
25+
}
26+
}
27+
28+
setIsAuthenticating(false);
29+
}
30+
31+
async function handleLogout() {
32+
await Auth.signOut();
33+
34+
userHasAuthenticated(false);
35+
36+
props.history.push("/login");
37+
}
38+
939
return (
40+
!isAuthenticating &&
1041
<div className="App container">
1142
<Navbar fluid collapseOnSelect>
1243
<Navbar.Header>
@@ -17,18 +48,23 @@ function App() {
1748
</Navbar.Header>
1849
<Navbar.Collapse>
1950
<Nav pullRight>
20-
<LinkContainer to="/signup">
21-
<NavItem>Signup</NavItem>
22-
</LinkContainer>
23-
<LinkContainer to="/login">
24-
<NavItem>Login</NavItem>
25-
</LinkContainer>
51+
{isAuthenticated
52+
? <NavItem onClick={handleLogout}>Logout</NavItem>
53+
: <>
54+
<LinkContainer to="/signup">
55+
<NavItem>Signup</NavItem>
56+
</LinkContainer>
57+
<LinkContainer to="/login">
58+
<NavItem>Login</NavItem>
59+
</LinkContainer>
60+
</>
61+
}
2662
</Nav>
2763
</Navbar.Collapse>
2864
</Navbar>
29-
<Routes />
65+
<Routes appProps={{ isAuthenticated, userHasAuthenticated }} />
3066
</div>
3167
);
3268
}
3369

34-
export default App;
70+
export default withRouter(App);

src/Routes.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import React from "react";
22
import { Route, Switch } from "react-router-dom";
3+
import AppliedRoute from "./components/AppliedRoute";
34
import Home from "./containers/Home";
5+
import Login from "./containers/Login";
46
import NotFound from "./containers/NotFound";
57

6-
export default function Routes() {
8+
export default function Routes({ appProps }) {
79
return (
810
<Switch>
9-
<Route path="/" exact component={Home} />
11+
<AppliedRoute path="/" exact component={Home} appProps={appProps} />
12+
<AppliedRoute path="/login" exact component={Login} appProps={appProps} />
1013
{ /* Finally, catch all unmatched routes */ }
1114
<Route component={NotFound} />
1215
</Switch>

src/components/AppliedRoute.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from "react";
2+
import { Route } from "react-router-dom";
3+
4+
export default function AppliedRoute({ component: C, appProps, ...rest }) {
5+
return (
6+
<Route {...rest} render={props => <C {...props} {...appProps} />} />
7+
);
8+
}

src/containers/Login.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@media all and (min-width: 480px) {
2+
.Login {
3+
padding: 60px 0;
4+
}
5+
6+
.Login form {
7+
margin: 0 auto;
8+
max-width: 320px;
9+
}
10+
}

src/containers/Login.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React, { useState } from "react";
2+
import { Auth } from "aws-amplify";
3+
import { Button, FormGroup, FormControl, ControlLabel } from "react-bootstrap";
4+
import "./Login.css";
5+
6+
export default function Login(props) {
7+
const [email, setEmail] = useState("");
8+
const [password, setPassword] = useState("");
9+
10+
function validateForm() {
11+
return email.length > 0 && password.length > 0;
12+
}
13+
14+
async function handleSubmit(event) {
15+
event.preventDefault();
16+
17+
try {
18+
await Auth.signIn(email, password);
19+
props.userHasAuthenticated(true);
20+
props.history.push("/");
21+
} catch (e) {
22+
alert(e.message);
23+
}
24+
}
25+
26+
return (
27+
<div className="Login">
28+
<form onSubmit={handleSubmit}>
29+
<FormGroup controlId="email" bsSize="large">
30+
<ControlLabel>Email</ControlLabel>
31+
<FormControl
32+
autoFocus
33+
type="email"
34+
value={email}
35+
onChange={e => setEmail(e.target.value)}
36+
/>
37+
</FormGroup>
38+
<FormGroup controlId="password" bsSize="large">
39+
<ControlLabel>Password</ControlLabel>
40+
<FormControl
41+
value={password}
42+
onChange={e => setPassword(e.target.value)}
43+
type="password"
44+
/>
45+
</FormGroup>
46+
<Button block bsSize="large" disabled={!validateForm()} type="submit">
47+
Login
48+
</Button>
49+
</form>
50+
</div>
51+
);
52+
}

0 commit comments

Comments
 (0)