I am following an online course on Nodejs and was trying to implement basic authentication in my express application. But I am getting error on app.use(new localStrategy(User.Authenticate()); I have tried reinstalling the 'passport-local'. Why this error keeps persisting ?
var express = require('express'),
app = express(),
mongoose = require('mongoose'),
passport = require('passport'),
User = require('./models/user'),
bodyParser = require('body-parser'),
localStrategy = require('passport-local'); ;
passportLocalMongoose = require('passport-local-mongoose');
mongoose.connect('mongodb://localhost/auth_DB');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static('images'));
app.use(require('express-session')({
secret: 'pppppqpqsda dasdqw ksndfkjnzmmuawt8ikweabmdsfj a',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.set('view engine', 'ejs');
app.get('/', function (req, res) {
// body...
res.render('home');
});
app.get('/secret', function (req, res) {
// body...
res.render('secret');
});
app.get('/register', function (req, res) {
// body...
res.render('signup');
});
app.post('/register', function (req, res) {
User.register(new User({username: req.body.username}), req.body.password, function(err, user){
if(err){
console.log(err);
return res.render('signup');
}
passport.authenticate('local')(req, res, function(){
res.redirect('/secret');
});
});
});
app.get('/login', function (req, res) {
// body...
res.render('signin');
});
app.post('/login', passport.authenticate('local',
{successRedirect: '/secret',
failureRedirect: '/login'} ),
function(req, res){});
app.listen(3000, function(){
console.log('Server Started!');
});
Package.json :-
{
"name": "test-auth",
"version": "1.0.0",
"description": "Testing the authentication",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Rishabh & Colt",
"license": "ISC",
"dependencies": {
"body-parser": "^1.17.1",
"ejs": "^2.5.6",
"express": "^4.15.2",
"express-session": "^1.15.2",
"mongoose": "^4.9.7",
"passport": "^0.3.2",
"passport-local": "^1.0.0",
"passport-local-mongoose": "^4.0.0"
}
}
User.js:-
var mongoose = require('mongoose');
var passportLocalMongoose = require('passport-local-mongoose');
var userSchema = new mongoose.Schema({
username: String,
password: String
});
userSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model('User', userSchema);
via feedammo
No comments:
Post a Comment