File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import NewProductPage from './NewProductPage';
66import SignInPage from './SignInPage' ;
77import HomePage from './HomePage' ;
88import NavBar from './NavBar' ;
9- import { User } from '../requests'
9+ import { User , Session } from '../requests'
1010
1111class 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 } /> }
Original file line number Diff line number Diff line change @@ -3,6 +3,12 @@ import { NavLink } from 'react-router-dom';
33
44function 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 ) : (
Original file line number Diff line number Diff 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
4349export const User = {
You can’t perform that action at this time.
0 commit comments