Friday, June 17, 2016

passport of nodejs throw pass is not defined (nodejs 0.12.7)

Leave a Comment

my app use passport for auth user, but it throw exception sometimes like this:

ReferenceError: pass is not defined at deserialized (node_modules/passport/lib/authenticator.js:345:7) at app/app.js:89:10

the app.js is:

    var app, bodyParser, config, cookies, cors, e, express, http, keys, localStrategy, logger, multipart, passport, path, router, server, session, sessionStore, sessionUsed, session_store, util, uuid;  express = require('express');  http = require('http');  path = require('path');  session = require('express-session');  cors = require('cors');  session_store = require('sessionstore');  keys = require("keygrip")(['a', 'b']);  bodyParser = require('body-parser');  cookies = require('cookies').express;  passport = require('passport');  uuid = require('node-uuid');  multipart = require('connect-multiparty');  localStrategy = require('passport-local').Strategy;  config = require('./config/config');  util = require('./libs/util');  logger = require('./libs/logger');  sessionStore = session_store.createSessionStore(config.getMemCache());  sessionUsed = session({   secret: config.getCookieSecret(),   genid: function() {     return uuid.v4();   },   cookie: {},   saveUninitialized: true,   resave: true,   store: sessionStore });  app = express();  router = express.Router();  app.set('port', process.env.PORT || config.getServerPort());  app.set('views', __dirname + '/../app/public');  app.set('view engine', 'html');  app.use(sessionUsed);  app.use(cors());  app.use(bodyParser.json());  app.use(bodyParser.urlencoded({   extended: true }));  app.use(multipart({   maxFilesSize: 2 * 1024 * 1024 }));  app.use(express["static"](path.join(__dirname, '/../app/public')));  app.use(cookies(keys));  app.use(passport.initialize());  app.use(passport.session());  passport.use('local', new localStrategy({   passReqToCallback: true }, util.checkUserPassword));  passport.serializeUser(function(user, done) {   return done(null, user); });  passport.deserializeUser(function(user, done) {   return done(null, user); });  require('./routes/router')(app, router, passport);  server = http.createServer(app);  try {   server.listen(app.get('port'), function() {     return logger.info('Express is running at:' + app.get('port'));   }); } catch (_error) {   e = _error;   console.log(e); } 

I have view the code which throw exception, like this enter image description here

1 Answers

Answers 1

First, your error is not shown in the screenshot. In the screenshot, you're showing line 282 from authenticator.js. Your error message shows where your problem actually is- line 345 in function deserialized.

The reason for this error is because your deserialized function in your app.js is not passing on a user object. Your deserialized function is a copy of your serialized function, and it should not be. Here is what the two should look like:

passport.serializeUser(function(user, done){     done(null, user.id); });  passport.deserializeUser(function(id, done){     User.findById(id, function(err, user){         done(err, user);     }) }); 

Of course, you can't use this deserialization function without changing your serialization function to store the user.id or some unique identifier instead of trying to store the whole user. I don't know what happens when you try serializing the whole user, but I'm confident it's what's causing your problems.

Hope that helps!

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment