0

I am trying to add logout feature using passport local strategy.

I followed the solutions discussed here, here and here

I added a

 a(href='/logout') logout

link in my index.jade file.

In my app.js I added the following:

app.get('/logout', function (req, res) {
    console.log("trying to logout....")
    req.session.destroy()
    req.logout()
    res.redirect('/signin');
});

But when I click the logout href It goes to my 404 page. In the browser url I see that it is attempting to go to:

localhost:3030/logout

I event added an empty logout.jade view but it makes no difference.

What am i doing wrong?

Also - I do not see the console.log("trying to logout...") message in my terminal... so it looks like it never reached the correct method.

Community
  • 1
  • 1
A.D
  • 1,480
  • 2
  • 18
  • 33
  • Perhaps you haven't restarted your Node server? Looks like it should work. – brandonscript Nov 18 '15 at 20:06
  • I have restarted it several times... – A.D Nov 18 '15 at 20:07
  • Do you have a route like `app.get('/:id')` declared *before* your logout route? What might be happening is that your request can be caught by the first route and genuinely not finding anything with id === 'logout', resulting in an 404 error. Can you confirm it is not happening? PS.: Routing order matters a lot – Renato Gama Nov 18 '15 at 20:15
  • @renatoargh I did not add a logout route.. i have made no changes in my routes.js file – A.D Nov 18 '15 at 20:18
  • 1
    @renatoargh thank you for mentioning order! that was the problem indeed. – A.D Nov 18 '15 at 20:21
  • @renatoargh may I ask you to explain why in this example of adi's that order would matter? I am having the same problem but in an auth.js file that just has routes for signup, login, and logout "router.get('/logout', function(req, res) {". I can't see how order would matter in my case. If you could take a moment and share with me some insight it would really help. – Ric Nov 15 '16 at 20:23

1 Answers1

0

Found answer thanks to @renatoargh pointing out that order of routes matter. Somewhat annoyed that the passport docs don't mention this!!

BEFORE:

app.use(route.notFound404);

app.get('/logout', function (req, res) {
    console.log("trying to logout....")
    req.session.destroy()
    req.logout()
    res.redirect('/signin');
});

SOLUTION:

app.get('/logout', function (req, res) {
    console.log("trying to logout....")
    req.session.destroy()
    req.logout()
    res.redirect('/signin');
});

app.use(route.notFound404);
A.D
  • 1,480
  • 2
  • 18
  • 33