angular-fullstackを使用してWebアプリケーションを構築しています。スタックは、セッションストレージ(Mongodbの場合)にはexpress-sessions、認証にはpassport.jsを使用しています。Passportミドルウェア、ライブセッションが既にあるかどうかを確認してください
各ユーザーを1回のログインセッションに制限したいと考えています。私は、ログインしたユーザーがすでにライブセッションを持っているかどうかを確認する方法を見つけようとしています。
パスポートミドルウェアからmongodbを照会するルートをプログラムで呼び出す方法はありますか?
'use strict';
import path from 'path';
import passport from 'passport';
import {Strategy as LocalStrategy} from 'passport-local';
import express from 'express';
import session from 'express-session';
import _ from 'lodash';
import Session from '../../api/session/session.model';
var app = express();
require('run-middleware')(app);
function localAuthenticate(User, email, password, done, req) {
User.findOne({
email: email.toLowerCase()
}).exec()
.then(user => {
if (!user) {
return done(null, false, {
message: 'This email is not registered.'
});
}
// HERE is where I am trying to check if a user
// already has a living session when they login
// I tried to use the runMiddleware
// to query mongodb for all the existing sessions
// but I get this error: http://pastebin.com/YTeu5AwA
app.runMiddleware('/sessions',{},function(code,data){
console.log(code) // 200
console.log(data) // { user: '20', name: 'Moyshale' }
});
// Is there a way to access and use an existing route?
user.authenticate(password, function(authError, authenticated) {
if (authError) {
return done(authError);
}
if (!authenticated) {
return done(null, false, { message: 'This password is not correct.' });
} else {
return done(null, user);
}
});
})
.catch(err => done(err));
}
export function setup(User, config) {
passport.use(new LocalStrategy({
passReqToCallback: true,
usernameField: 'email',
passwordField: 'password' // this is the virtual field on the model
}, function(req, email, password, done) {
return localAuthenticate(User, email, password, done, req);
}));
}
[この質問](http://stackoverflow.com/questions/21434922/node-js-one-session-per-user)とその答えを確認してください。 – robertklep