Skip to content

Commit cee5dfb

Browse files
maxwellgordonansonws
authored andcommitted
Sign Out
1 parent ef63daa commit cee5dfb

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/components/App.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import NewProductPage from './NewProductPage';
66
import SignInPage from './SignInPage';
77
import HomePage from './HomePage';
88
import NavBar from './NavBar';
9-
import { User } from '../requests'
9+
import { User, Session } from '../requests'
1010

1111
class App extends Component {
1212
constructor(props) {
@@ -15,6 +15,11 @@ class App extends Component {
1515
currentUser: null
1616
}
1717
this.getUser = this.getUser.bind(this);
18+
this.destroySession = this.destroySession.bind(this);
19+
}
20+
21+
destroySession() {
22+
Session.destroy().then(() => this.setState({ currentUser: null }));
1823
}
1924

2025
getUser() {
@@ -34,7 +39,7 @@ class App extends Component {
3439
return (
3540
<Router>
3641
<div className="App">
37-
<NavBar currentUser={currentUser} />
42+
<NavBar currentUser={currentUser} onSignOut={this.destroySession} />
3843
<Switch>
3944
<Route path="/session/new" exact render={
4045
(routeProps) => <SignInPage {...routeProps} onSignIn={this.getUser} />}

src/components/NavBar.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { NavLink } from 'react-router-dom';
33

44
function NavBar(props) {
55
const {currentUser} = props;
6+
const handleSignOutClick = event => {
7+
event.preventDefault();
8+
if (typeof props.onSignOut === "function") {
9+
props.onSignOut();
10+
}
11+
};
612
return (
713
<div className="NavBar">
814
<NavLink to="/">Home</NavLink>|<NavLink to="/products">Products</NavLink>|
@@ -13,6 +19,8 @@ function NavBar(props) {
1319
<NavLink to="/products/new">New Product</NavLink>
1420
|
1521
<span>{currentUser.full_name}</span>
22+
|
23+
<a href="#nope" onClick={handleSignOutClick}>Sign Out</a>
1624
</>
1725
</span>
1826
) : (

src/requests.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ export const Session = {
3838
body: JSON.stringify(params),
3939
}).then(res => res.json());
4040
},
41+
destroy() {
42+
return fetch(`${BASE_URL}/sessions`, {
43+
method: 'DELETE',
44+
credentials: 'include',
45+
}).then(res => res.json());
46+
}
4147
};
4248

4349
export const User = {

0 commit comments

Comments
 (0)