node.js - req.isAuthenticated() always returning false passportjs session management in nodejs -


i trying use passport js session management perform login, logout , rest api authentication in case req.isauthenticated() method returning false always. please refer code snippet below. in advance.

i'm using angular4 front end send api request , mysql database. searched same type of question didn't exact answer.

var express = require('express'); var app = express(); var config = require("./routes/config") var members = require('./routes/members'); var groups = require('./routes/groups'); var expenses = require('./routes/expenses');  var bodyparser = require('body-parser'); var expressvalidator = require("express-validator"); var expresssession = require("express-session"); var passport   = require('passport'); var localstrategy = require('passport-local').strategy; var flash = require('connect-flash');  app.use(bodyparser.json());   app.use(bodyparser.urlencoded({ extended: false })); app.use(expressvalidator()); app.use(expresssession({secret: "23yearsold",saveuninitialized: true, resave: true})); app.use(passport.initialize()); app.use(passport.session()); app.use(flash()); app.get('/get_member_dues',isloggedin, expenses.get_member_dues);   app.post('/login',passport.authenticate('local-login', {     failureflash: true }), function(req, res, info){     res.send({"message":"success", "mobile": req.body.mobile});     //res.render('login/index',{'message' :req.flash('message')}); });  app.post("/register", passport.authenticate('local-signup', {     failureflash: true }), function(req, res, info){     res.send({"message":"success"});     console.log("success");     //res.render('login/index',{'message' :req.flash('message')}); });   var server = app.listen(8081, function () {     var host = server.address().address     var port = server.address().port     console.log("example app listening @ http://%s:%s", host, port) });  passport.serializeuser(function(user, done) {     done(null, user.id); });  passport.deserializeuser(function(id, done) {     config.query("select * members id = "+id,function(err,rows){         done(err, rows[0]);     }); });  passport.use('local-signup', new localstrategy({     // default, local strategy uses username , password, override email     usernamefield : 'mobile',     passwordfield : 'password',     passreqtocallback : true // allows pass entire request callback }, function(req,mobile, password, done) {     var name;     config.query("select * members mobile = '"+mobile+"'",function(err,rows){         console.log(rows);         console.log("above row object");         if (err)             return done(err);         var string = json.stringify(rows);         var obj = json.parse(string);          if (rows.length && obj[0].password) {             return done('that mobile taken.');         } else {             var newusermysql = new object();             name = req.body.name;             console.log("name"+name);             newusermysql.mobile = mobile;             newusermysql.password = password;             var values = [[name, mobile,password]]             console.log(values)             if(!obj[0].password){                 var updatequery = "update members set name = ?, password = ? mobile = ?";                 console.log(updatequery);                 config.query(updatequery, [name, password, mobile], function (err, rows) {                     if(err){                         throw err;                     }                     newusermysql.id = obj[0].id;                     return done(null, newusermysql);                 });             }else {                 var insertquery = "insert members (name, mobile, password) values ?";                 console.log(insertquery);                 config.query(insertquery, [values], function (err, rows) {                     newusermysql.id = rows.insertid;                     return done(null, newusermysql);                 });             }         }     }); }));  passport.use('local-login', new localstrategy({     // default, local strategy uses username , password, override email     usernamefield : 'mobile',     passwordfield : 'password',     passreqtocallback : true // allows pass entire request callback }, function(req, mobile, password, done) { // callback email , password our form      config.query("select * `members` `mobile` = '" + mobile + "'",function(err,rows){         if (err)             return done(err);         if (!rows.length) {             return done ("no user found.");             //return done(null, false, req.flash('loginmessage', 'no user found.')); // req.flash way set flashdata using connect-flash         }          // if user found password wrong         if (!( rows[0].password == password))             return done ("oops! wrong password.");             //return done(null, false, req.flash('loginmessage', 'oops! wrong password.')); // create loginmessage , save session flashdata          // well, return successful user         return done(null, rows[0]);      }); }));  function isloggedin(req, res, next){     if (req.isauthenticated()) {         console.log("authenticated");         next();     }else{         console.log("not authenticated");         res.status(500).send("not authenticated");     } } 


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -