Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging a user out #2

Open
jBachalo opened this issue Mar 11, 2016 · 8 comments
Open

Logging a user out #2

jBachalo opened this issue Mar 11, 2016 · 8 comments

Comments

@jBachalo
Copy link

Hi
How do you log a user out? On subsequent get requests to the server the previous user seems to remain logged in.

@rajojon23
Copy link

Inside profile.ejs, add the following line:

<p><a href="/logout">Logout</a></p>

Then go to server.js and add a logout route:

app.get('/logout', function(req, res) {
        req.logout(); 
        res.redirect('/');
    });

From what I tried, passport takes care of the entire authentication process in the background so you can just call req.logout(). You can redirect to whatever page you want the user to go after logging out.

@fbukevin
Copy link

fbukevin commented Apr 8, 2016

@rajojon23 Good work!

Hope @passport can add this sample to file.

@jBachalo
Copy link
Author

jBachalo commented Jun 8, 2016

this doesn't seem to work any more. Can anyone else confirm?

@fbukevin
Copy link

fbukevin commented Jun 13, 2016

Hi, @jBachalo! @rajojon23 's answer is work for me. But I packaged server.js into a router (routers/passport.js).

This is what my app.js looks like:

var express = require('express')
    , passport = require('passport');

var fb = require('./routers/passport')(passport);

app.use(passport.initialize());
app.use(passport.session());

app.use('/fb', fb);

and the routers/passport.js

var express = require('express');
var router = express.Router();
module.exports = router;

module.exports = function(passport){

  var Strategy = require('passport-facebook').Strategy;

  passport.use(new Strategy({
      clientID: process.env.CLIENT_ID,
      clientSecret: process.env.CLIENT_SECRET,
      callbackURL: '...',  
    },
    function(accessToken, refreshToken, profile, cb) {
      return cb(null, profile);
    }));


  passport.serializeUser(function(user, cb) {
    cb(null, user);
  });

  passport.deserializeUser(function(obj, cb) {
    cb(null, obj);
  });


  router.get('/login', passport.authenticate('facebook', {}), 
    function(req, res) {
      /*  seems not be executed  */
    });

  // callback
  router.get('/return', passport.authenticate('facebook', {failureRedirect: '/'}),
    function(req, res){
        ...
  });

  router.get('/logout', function(req, res) {
      req.logout(); 
      res.redirect('/');
    });

  return router;
}

The profile.ejs content is the same as he said.

@WORMSS
Copy link

WORMSS commented Sep 30, 2016

@fbukevin I presume the passprt.serializeUser is a singleton? so if you had multiple routes that were similar to this? eg, facebook/google/blarblar then which ever is called last would have the serializeUser function ??

also, I presume require(./routes/passport_facebook) is meant to be ./routers/passport ??
or have i understood something incorrectly?

@fbukevin
Copy link

fbukevin commented Oct 5, 2016

@WORMSS Yap, you're right. There is a typo of required file name. ./routes/passport_facebook in app.js should be ./routers/passport. I fixed it. Thanks!

@mitalimn
Copy link

How to log a user out? On subsequent get requests to the server the previous user seems to remain logged in.
if the next person wants to login , i have to forcibly logout from facebook then let the next person login to my app

@cryskram
Copy link

Use express session and in the logout route destroy the session

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants