loopback redis based sessions

Viewed 1805

first i would like to say that i have already seen questions regarding related issues but did not find the answer.

I want to use redis for session in loopback, but req is not available in code.

server/server.js

var redis   = require("redis");
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var client  = redis.createClient();

var app = module.exports = loopback();

app.use(session({
    secret: '0`3VTw;hQ|3/`:95ZYu{0J82O>{}7JC/',
    // create new redis store.
    store: new redisStore({ host: 'localhost', port: 6379, client: client,ttl :  260}),
    saveUninitialized: false,
    resave: false
}));

I want to implement Redis Based express sessions on loopback application but i cant find the way to do it without writing the whole route controller myself.

I am currently extending the model and creating a remoteMethod() but i cannot access "req" in anyway.

this is my TrmptUsers Model code (see line 3)

common/models/trmpt-users.js

module.exports = function(TrmptUsers) {
 TrmptUsers.login = function(username, password, cb) {
    console.log(TrmptUsers.app.session); //i want access to req variable to work with redis here.
 var ursalt = security.gen_salt(username);
        password = security.decrypt256(password, ursalt);
        TrmptUsers.findOne({fields:{usrPwd:true,usrAccessToken:true},where:{and:[{usr_username:username}]}},function(err,result){
        security.verifyPassword(password, result.usrPwd, function(err, verify) {
                if (verify) {
                User.generateVerificationToken(username, cb)
                    cb(null,result.usrAccessToken);
                }
                else {
                   cb({status:401,message:"Invalid Username/Password"},null);   
                }
            });
        });
    }
    TrmptUsers.remoteMethod(
        'login', 
        {
          accepts: [
          {arg: 'username', type: 'string', required: 'true'},
          {arg: 'password', type: 'string', required: 'true'},
          ],
          returns: {arg: 'accesstoken', type: 'string'}
        }
    );
};
1 Answers
Related