Now, it’s time to log out of the application. Do that with the following steps.
The steps that the application will take are these:
LogoutButton
component dispatches a thunkLogoutButton
will redirect the application back to “/login”In src/store/authentication.js:
REMOVE_TOKEN
REMOVE_TOKEN
logout
that
DELETE /api/session
(using the token in the state)TOKEN_KEY
removeToken
actionHandles the REMOVE_TOKEN
action type in the reducer by creating a new object that does not have the “token” key in it and returning that.
Remember that handlers in reducers must return new objects if they want to modify the state.
In src/LogoutButton.js:
(If you make a mistake with this and get into an inconsistent state, just delete all of the contents of your local storage and refresh your browser.)
connect
from “react-redux”logout
thunk you just createdmapStateToProps
and set a property named loggedOut
to true
if the token in the state is empty, and false
if there is a value for the token in the statemapDispatchToProps
and set the “logout” property equal to a function that dispatches the result of the logout
thunk you importedLogoutButton
’s method named “logout”, instead of making an AJAX call, have it call this.props.logout()
, insteadthis.state.loggedOut
to this.props.loggedOut
If you check the console, now, you’ll see that Babel is reporting a “useless” constructor. Sure enough, it is. LogoutButton
no longer has any state, so there’s no reason to leave it as a class-based component. Convert it to a function-based component. If you’ve followed these instructions, your LogoutButton
should end up looking something like this.