question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding a logout route

See original GitHub issue

I have added require('./logout').default, to routes/index.js and created routes/logout/index.js like so:

export default {
  path: '/logout',
  action({ req, res }) {
      req.session.destroy(function (err) {
        res.redirect('/');
      });
  },
};

But the req and res params are undefined. Is this a correct way to implement a logout route?

I would also prefer to use a post request for logout, can I send a post request to this route?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
frenzzycommented, Jan 6, 2017

You can override default behavior like this:

function logout(event) {
  event.preventDefault(); // prevent page transition
  fetch('/logout', { method: 'POST' }).then(() =>
    window.location.reload() // stay at the same url
  )
}

<Link to="/logout" onClick={logout}>Logout Link</Link>
<a href="/logout" onClick={logout}>Logout Anchor</a>
<button type="button" onClick={logout}>Logout Button</button>
<form action="/logout" method="post" onSubmit={logout}>
  <button>Logout Form</button>
</form>

Also you can use redux action to logout user without page refresh:

const USER_LOGOUT = 'USER_LOGOUT'; // action type

function logout() { // redux action
  return (dispatch) => {
    dispatch({ type: USER_LOGOUT });
    return fetch('/logout', { method: 'POST' })
      .then(() => dispatch({ type: USER_LOGOUT, error: false }))
      .catch(error => dispatch({ type: USER_LOGOUT, payload: error, error: true }))
  }
}

const initialState = null;

function user(state = initialState, action) { // redux reducer
  switch (action.type) {
    case USER_LOGOUT:
      return action.error === false ? initialState : state;
    default:
      return state;
  }
}

There is not session auth in the master branch, so you can just clear cookie for logout:

/* src/server.js#L75 */
app.post('/logout', (req, res) => {
  res.clearCookie('id_token');
  res.redirect('/');
});
1reaction
frenzzycommented, Feb 23, 2017

You may use mapDispatchToProps inside react-redux connect(): Redux - Usage with React

Or dispatch manually:

class LogoutButton extends React.Component {
  static contextTypes = {
    store: PropTypes.object.isRequired,
  };

  handleClick = () => {
    this.context.store.dispatch(logout());
  };

  render() {
    return <button type="button" onClick={this.handleClick}>Logout</button>;
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to logout and redirect to login page using Laravel 5.4?
In your web.php (routes):. add: Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');. In your LoginController.php.
Read more >
Laravel 9 Logout For Your Authenticated User - Codeanddeploy
Let's implement Laravel logout for your authenticated users. Laravel logout is one of the most ... Step 1: Create a route; Step 2:...
Read more >
Logout - Auth0
Describes how logout works with Auth0. ... If you need different redirects for each application, you can add the URLs to your allow...
Read more >
Route to logout - Laracasts
Hello, does anyone have the route for in the web.php to logout, ... by Laravel you can simply add Auth::routes(); anywhere in your...
Read more >
Add Login Logout Functionality - YouTube
In this video we will add login and logout functionality to our application. We will also use NGX Bootstrap component to display drop-down ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found