2017-02-26 78 views
1

我正在使用const SessionStore = require('express-session-sequelize')(expressSession.Store);來存儲會話。 我想保存在會話currenty用戶,該用戶登錄到網站。nodejs express中的會話

這裏是商店的配置:

const expressSession = require('express-session'); 
const SessionStore = require('express-session-sequelize')(expressSession.Store); 
const Sequelize = require('sequelize'); 
const myDatabase = new Sequelize('analytic', 'root', '', { 
    host: 'localhost', 
    dialect: 'mysql' 
}); 

const sequelizeSessionStore = new SessionStore({ 
    checkExpirationInterval: 15 * 60 * 1000, // The interval at which to cleanup expired sessions in milliseconds. 
    expiration: 24 * 60 * 60 * 1000, // The maximum age (in milliseconds) of a valid session. 
    db: myDatabase 
}); 
const cookieParser = require('cookie-parser'); 
app.use(cookieParser()); 
app.use(expressSession({ 
    secret: '412415415415415121212121', 
    store: sequelizeSessionStore, 
    name: 'session_id', 
    resave: false, 
    saveUninitialized: false 
})); 

所以,現在登錄我有這樣的:

var express = require('express'); 
var url = require('url'); 
var router = express.Router(); 
var User = require('../../model').User; 
var jwt = require('jsonwebtoken'); 
var app = require("../../application"); 
router.post('/authenticate', function(req, res) { 
    User.doLogin(req).then((result)=> { 
     if (result) { 
      if (result.error) { 
       if (result.error.hasOwnProperty("email")) { 
        res.send(400, { success: false, message: "Incorrect user/password"}); 
       } else if (result.error.hasOwnProperty("activated")) { 
        res.send(400, {success: false, message: "You are not activated yet"}); 
       } 
      } else { 
       //app.createSession(req); 
       req.session.user = result; 
       res.send(200, result); 


      } 
     } 
    }); 
}); 


module.exports = router; 

現在,這req.session.user = result;後,我在DB會話表與當前用戶數據。 現在,

  1. 如何驗證此用戶是否已通過身份驗證?
  2. ,我注意到,爲每個請求我得到diferrent會話ID(這是正確的)

感謝

回答

0

您好用戶管理員可以使用passportjs 它是表達一個偉大的用戶管理和提供高質量的處理您的用戶管理器邏輯。