0
votes

I want to make authentication function with Passport.js+Express.

But If I run this code, I receive failure result and null in 'req.user'.

I want to find the problem, but console.log() in localStrategy is not called when I was using passport.authenticate(). I don't know where I have to fix.

How can I fix this code to get proper function?

-app.js

var express = require('express');
var bodyparser=require('body-parser');
var path = require('path');
var cookieParser = require('cookie-parser');
var passport = require('passport'); //passport module add
var localStrategy = require('passport-local').Strategy;
var cookieSession=require('cookie-session');

var index = require('./index.js');
var app=express();

app.use(cookieParser());

app.use(express.static(__dirname+'/views'));

app.use(cookieSession({
    keys : ['login'],
    cookie : {maxAge:1000*60*60} 
}));


app.set('views',__dirname+'/views');
app.set('view engine','html'); 
app.engine('html',require('ejs').renderFile);

app.use('/',index);

app.listen(8000,function(){
    console.log('server started');
});

module.exports=app;

-index.js

var express = require('express');
var router = express.Router();
var bodyparser=require('body-parser');
var crypto=require('crypto');
var passport=require('passport');
var localStrategy=require('passport-local').Strategy;

var mysql=require('mysql');

var config={
    host : 'localhost',
    port : 3306,
    user : 'root',
    password : 'xxxxxx',
    ssl : true
};

router.use(bodyparser.json());
router.use(passport.initialize());
router.use(passport.session());

const conn = new mysql.createConnection(config);

conn.connect(function(err){
    if(err){
        console.log('Cannot Connect to database : ');
        throw err;
    }
    else{
        console.log('Success to connect database');
    }
});

passport.use(new localStrategy({
    usernameField : 'username',
    passwordField : 'password',
},function(req,username,password,done){
        conn.query('USE Database');
        conn.query('select userID as username, userPWD as password from UserTable where userID=?',[username],function(err,userinfo,fields){
                    if(err){
                        return done(err);
                    }
                    else{
                        if(username==userinfo[0].username){
                                    var encryptHash=crypto.createHash('sha512').update(password);
                                    if(encryptHash===password){
                                                    return done(null,userinfo[0]);
                                    }
                                    console.log('login succeed');
                                    return done(null,userinfo[0]);
                        }
                        else{
                            return done(null,false);
                        }
          }
        });
    })
);

passport.serializeUser((user,done)=>{
    console.log("hello2");
    done(null, user.username);
});
passport.deserializeUser((id,done)=>{
    console.log("hello");
    var sql='select userID as username, userPWD as password from UserTable where userID=?';
    conn.query(sql,[id],(err,userinfo,fields)=>{
        if(err){
            console.log("error 발생 : "+err);
            res.status(500).send("Internal Server Error");
        }
        else{
            if(userinfo[0].username==id){
                done(null,userinfo[0]);
            }
        }
    });
});

router.get('/',function(req,res,next){
    console.log('homepage');
    res.render('index');
});

router.post('/login',passport.authenticate('local',{failureRedirect:'/',successRedirect:'/main'}));

module.exports=router;

1

1 Answers

0
votes
You should check the "req" param  inside 
function(req,username,password,done)

may be this function takes only three parameters 
**http://www.passportjs.org/packages/passport-local/**

you should check what you get on post request through this `req` using this function