Check each node.js request for authentication credentials

前端 未结 3 1443
北海茫月
北海茫月 2021-01-30 23:08

I\'m using node.js with Express and connect-auth to authenticate users.

This is the verification when requesting /index:

if(req.isAuthenticated()) {
  re         


        
3条回答
  •  野的像风
    2021-01-30 23:31

    You can use sessions mechanism provided by connect. Put this code in app.configure() to enable it:

      app.use(express.cookieParser());
      app.use(express.session({
        secret: 'some string used for calculating hash'
      }));
    

    After that, you′ll be able to use req.session object (different for each request) to store your authentication data (or anything else). So, your example code will look something like this:

    if (req.session && req.session.authorized) {
      res.redirect('/dashboard');
    }
    else {
      res.render('index', {layout: 'nonav'});
    }
    

    And authentication will look like this:

    req.session.authorized = checkPassword(login, passw);
    

    Logout:

    req.session.destroy();
    

    More info can be found here.

提交回复
热议问题