javascript - Session Node.js + Passport.js + Redis, Store session by user.id -


when user logs in session created him, if go computer , login 2nd session created account. make user not have more 1 valid session. there anyway store sessions in redis user.steamid first session becomes invalid?

any great thanks!

app.js

    var express = require('express'),     http = require('http'); var app = express(); var cookie = require('cookie'); var server = http.createserver(app); var io = require('socket.io').listen(server); var redis = require('redis'); var client = redis.createclient(); var session = require('express-session'); var redisstore = require('connect-redis')(session); io.set('transports', ['websocket']);  var path = require('path'); var logger = require('morgan'); var cookieparser = require('cookie-parser'); var bodyparser = require('body-parser'); var passport = require('passport'); const fs = require('fs'); require('./config/passport')(passport);   var sessionmiddleware = session({     store:new redisstore({host:'localhost',port:6379,client:client}),     secret:'secrettextchange',     saveuninitialized:false,     resave:false }); // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs');  // uncomment after placing favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev'));  app.use(sessionmiddleware); app.use(passport.initialize()); app.use(passport.session()); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: false })); app.use(cookieparser()); app.use(express.static(path.join(__dirname, 'public')));  require('./routes/routes')(app,passport,client);  // catch 404 , forward error handler app.use(function(req, res, next) {   var err = new error('not found');   err.status = 404;   next(err); }); io.use(function(socket, next) {     sessionmiddleware(socket.request, {}, next); }); io.sockets.on('connection', function (socket) {         console.log("verified");         socket.on('message',function(msg){             io.sockets.emit('rmessage', {                 name:socket.request.session.passport.user.name,                 avatarurl:socket.request.session.passport.user.avatarurl,                 message:msg             });         });  });  // error handlers  // development error handler // print stacktrace if (app.get('env') === 'development') {   app.use(function(err, req, res, next) {     res.status(err.status || 500);     res.render('error', {       message: err.message,       error: err     });   }); }  // production error handler // no stacktraces leaked user app.use(function(err, req, res, next) {   res.status(err.status || 500);   res.render('error', {     message: err.message,     error: {}   }); }); server.listen(3000);  module.exports = app; 

passport.js

var openidstrategy = require('passport-openid').strategy; var auth = require('./auth'); var steam = require('./steam'); var s = new steam({     apikey: auth.steam.apikey,     format:'json' }) module.exports = function(passport){      passport.serializeuser(function(user, done) {          done(null, user);     });      passport.deserializeuser(function(user, done) {           done(null,user);     });      var steamstrategy = new openidstrategy({             // openid provider configuration             providerurl: auth.steam.providerurl,             stateless: auth.steam.stateless,             // how openid provider should return client             returnurl: auth.steam.returnurl,             realm: auth.steam.realm,         },          function(identifier, done) {              process.nexttick(function () {                 console.log("passport-"+identifier);                  s.getplayersummaries({                     steamids:identifier.match(/\d+$/)[0],                     callback:function(err,data){                         var user = {                             steamid:identifier.match(/\d+$/)[0],                             avatarurl: data.response.players[0].avatar,                             name:data.response.players[0].personaname                         };                         return done(null, user);                     }                 });                 // in case of error, invoke done(err).                 // if cannot find or don't login attempt, invoke                 // done(null, false).                 // if went fine, invoke done(null, user).             });         });      passport.use(steamstrategy);  } 

routes.js

module.exports = function(app,passport,client){      app.get('/', function (req,res) {         res.render('index.ejs',{                                 user: req.user,                                title:"yo"});     });      app.get('/auth',passport.authenticate('openid'));      app.get('/auth/return',passport.authenticate('openid'),function(req,res){          if (req.user) {             res.redirect('/');         } else {             res.redirect('/');         }     }); } 

could use this: https://www.npmjs.com/package/redis-sessions ?

there's method called soid gets sessions of single id. query user's id log in. sessions id. if soid returns empty can safely assume user had no sessions. if returns things inside it, user has sessions.

this best attempt right now.

good luck.


Comments