Wednesday, 3 May 2017

Passport session is not presisting

Hi I am trying to implement login with passport-local strategy. For some odd reason my code works when I send request from postman, but when I call the same action from my angular project it dose not work. Tracing down the problem I figured out:

When I send request from postman the session looks like this:

Session {
    cookie: { 
           path: '/',
           _expires: null,
            originalMaxAge: null,
            httpOnly: true 
    },
    passport: { 
             user: 58f0865eb9b69e1d38fa135b 
    } 
}

When I send request from angular the session looks like this:

Session {
    cookie: { 
           path: '/',
           _expires: null,
            originalMaxAge: null,
            httpOnly: true 
    }
}

because of with passport strategy fails to deserialize user. I have no clue why this is happening, any help will be highly appreciated.

Server code :

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator({
    customValidators: {
        isArray: function (value) {
            return Array.isArray(value);
        }
    },
    errorFormatter: function (param, msg, value) {
        var namespace = param.split('.')
            , root = namespace.shift()
            , formParam = root;

        while (namespace.length) {
            formParam += '[' + namespace.shift() + ']';
        }
        return {
            param: formParam,
            msg: msg,
            value: value
        };
    }
}));

app.use(cookieParser());
app.use(expressSession({ secret: 'untoldstoryofninja', resave: false, saveUninitialized: true }));
app.use(cors());
app.use(passport.initialize());
app.use(passport.session());

// routes ========================================================================================
app.use(require('./config/routes/routes.js')(passport));
app.all('*', (req, res) => res.status(404).send({ msg: 'No API Route.' }));  

passport strategy:

module.exports = function (passport) {
    passport.use(new LocalStrategy({ usernameField: "username", passwordField: "password" },
        function (username, password, done) {
            User.findOne({ username: username }, function (err, user) {
                if (err) { return done(err); }
                if (!user) { return done(null, false); }
                if (user.comparePassword(password, function (err, isMatch) {
                    if (isMatch && !err) {
                        done(null, user)
                    } else {
                        done(null, false);
                    }
                }));
            });
        }
    ));

    passport.serializeUser(function (user, done) {
        done(null, user._id);
    });

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



via Ahmad Abdullah

No comments:

Post a Comment