Saturday 15 March 2014

Use Express' Connect-Redis to store sessions on remote Redis server -


I'm building the express app in conjunction with the sofas and radis locally, it's working fine .

I am now trying to use the remote radis server hosted on IRIS radis. I have tried today but with the passport's local strategy, after returning a cookie, the express 'connect-rades' can not be found, it is working just fine, but when I have a local reads server Used to be.

  app.configure (function () {app.set ('views', __dirname + '/ views'); app asset ('visual engine', 'z'); ap. (Express.favicon ()); ap. Use (express.logger ('dev')); ap. User ('/ upload', upload filehandler (ap; o.se. (express.bodyParser ({Extension: true , Upload Dear: 'Public / Upload'}); App Usage (Express.MathOrrayide ()); App Usage (Express cookieParser ()); Application Uses (Express Season ({Cookie: {Max Age: 60000000} , Mystery: "Sauce", Store: New Radissoner ({Port: 637 9, host: 'url', p C: 'sauce'}}}); app.use (flash ()); app usage (passport intrise); app usage (passport session); app user (app quarters); app Use (Express. Static (path (__deirnam, 'public'));   

})


  // authentication passport .use (new locale (function (username, password, done) {username = username.toLowerCase (); db.get (user name, function (error, doctor) {if (err) {console.log (err) ; Return done (err);} if (! D oc) {returned (empty, incorrect; message: 'no sub by that name No Ogkrta. '}); } If (doc.password! == password) {returned (empty, wrong, {message: 'wrong password.'}); } Returned (empty, doctor); }); })); Passport. Cyrilize user (function (user, done) {done (tap, user ._id);}); Passport.deserializeUser (function (userId, done) {db.get (userId, function (err, user) {if (err) {return (500, err);} else {done (null, _.omit (user, "password"])); } }); });   
  // root export. Authentication = Passport Authentication ('local', {successRedirect: '/', failure redirect: '/ login'});    

You are not specifying redis db; This can be the cause of failure.

More generally, I recommend using a URL in an environment variable to radis a parameter, and therefore the same parsing code is whether you have local or local remote servers. For example, from app.js:

  var config = require ('.config / config.js') app.use (express.session ({store: new redisstore ({port : Config.redishost, host: config.redisHost, db: config.redisDatabase, pass: config.redisPassword}), Incognito: 'Choose a good password', Proxy: true, // if you are behind the proxy cookie: {secure : True}}))   

and config.js:

  var url = require ('url') var config = {}; Var redisUrl; If (typef (process.env.REDISTOGO_URL)! = 'Undefined') {redisUrl = url.parse (process.env.REDISTOGO_URL); } Else redisUrl = url.parse ('redis: //: 127.0.0.1: 637 9/0'); Config.redisProtocol = redisUrl.protocol.substr (0, redisUrl.protocol.length - 1); // Remove trailing: 'config.redisUsername = redisUrl.auth.split (': ') [0]; Config.redisPassword = redisUrl.auth.split (':') [1]; Config.redisHost = redisUrl.hostname; Config.redisPort = redisUrl.port; Config.redisDatabase = redisUrl.path.substring (1); Console.log ('Using Radis Store' + config.redisDatabase) module.exports = config;    

No comments:

Post a Comment