Tuesday, 30 May 2017

User authentication using passport.js Giving error I can't understand/solve(first ques. on stack)

I'm very new to node.js, passport.js etc and was trying to learn how to make an authentication app using :https://www.youtube.com/watch?v=OlapNW9Jc8s this is a great video but I keep getting this error after reaching 31:04 in the video-

"TypeError: Cannot read property 'name' of undefined at Authenticator.use"

This is my app.js file

var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressSession = require('express-session');

var passport = require('passport');
var passportLocal = require('passport-local');

var app = express();

app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
    secret: process.env.SESSION_SECRET
app.use(expressSession({  || 'secret',
    resave: false,
    saveUninitialized: false
}));


app.use(passport.initialize());
app.use(passport.session());

passport.use(passportLocal.Strategy(function(username, password, done){
    //connect to a real db here
    if(username===password){
        done(null,{id: username, name: username});
        //these actually have to bbe pulled from the db
    }
    else{
        done(null,null);
    }
}));


app.get('/', function(req, res){
    res.render('index',{
        isAuthenticated: req.isAuthenticated(),
        user: req.user

    });
});

app.get('/login', function(req, res){
    res.render('login');
});

app.post('/login', passport.authenticate('local' , function(req,res){
    res.redirect('/');
}));



var port = process.env.PORT || 1337;

app.listen(port, function(err,res){
    if(err)
        console.log(err);
    else
        console.log('server started on localhost:'+ port +' /');
});



via Ishaan Shakunt

No comments:

Post a Comment