nodejs中的会话表达

时间:2017-02-26 19:40:42

标签: node.js sequelize.js express-session

我正在使用const SessionStore = require('express-session-sequelize')(expressSession.Store);来存储会话。 我想保存在会话currenty用户,登录到站点。

这是商店的配置:

db.articles.aggregate(
[ { $match : { author : {"annT","ahn","ty"} } } ]
);

所以,现在登录我有这个:

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; 后,我在db session table中有当前用户数据。 现在,

  1. 如何验证此用户是否已通过身份验证?
  2. 我注意到每次请求我都会得到不同的sessionID(这没关系)
  3. 由于

1 个答案:

答案 0 :(得分:0)

您好用户经理可以使用passportjs 它是Express的优秀用户管理器,可为您的用户管理器逻辑提供高质量的处理。