sahat / megaboilerplate

Handcrafted starter projects, optimized for simplicity and ease of use.
MIT License
3.83k stars 257 forks source link

React: user cookie should be cleared if user no longer exists #155

Open sahat opened 8 years ago

sahat commented 8 years ago

If a user has been manually deleted from the database or no longer exists for whatever reason, a cookie - that normally gets deleted during logout - should be automatically cleared from the Browser.

screenshot 2016-07-06 17 57 57

screenshot 2016-07-06 18 04 15
MikeMcChillin commented 7 years ago

I was able to fix this issue with the following lines in server.js (around line 102):

// React server rendering
app.use(function(req, res) {

  var cookieToken = req.cookies.token;
  var tokenUser = req.user;
  if (cookieToken && (tokenUser === null)) { // Okay so we've got a token but no user
    cookieToken = '';
    res.cookie('token', '', {expires: new Date(0) }); // delete the cookie
  }

  var initialState = {
    auth: { token: cookieToken, user: tokenUser }, // carry on as usual
    messages: {}
  };

  var store = configureStore(initialState);