gpt4 book ai didi

javascript - Node.js 上的 Passport 模块 - 引用错误 : LocalStrategy is not defined

转载 作者:太空宇宙 更新时间:2023-11-04 02:56:22 28 4
gpt4 key购买 nike

当我尝试运行代码时,它给出了引用错误:LocalStrategy 未定义。

这是我第一次使用node.js,但我遇到了困难。我非常感谢您提前提供的帮助。

我将所有代码放在一个片段中,以便您可以轻松浏览。我尝试过其他帖子进行修复,但没有成功。

/***********
Modules
***********/

//Load the express library
var express = require('express');
//Create a new variable called “app”; we pass on the express() method.
var app = express();
//Set Port
var port = 7878;
var mongoose = require('mongoose'); //Place this on top; Loads mongoose library
var bodyParser = require('body-parser');
var passport = require('passport');
var LocalStratgy = require('passport-local').Strategy;

/*Body parser*///whenever you do a post request from the form, it gets the data through a URL encoded format.
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/js', express.static(__dirname + '/js'));

/*Initialize Passport*/
app.use(passport.initialize());
app.use(passport.session());

/***********
Database
***********/

/*Database connection - MongoDB*/

//Created from the command earlier. Ensure this is done on the first_db instance
var usr = 'admin';
var pwd = '123456';

var dbHost = 'localhost';
var dbPort = '27017';
var database = 'first_db';

var url = 'mongodb://' + usr + ':' + pwd + '@' + dbHost + ':' + dbPort + '/' + database;
console.log('mongodb connection = ' + url);

mongoose.connect(url, function(err) {
if(err) {
console.log('connection error: ', err);
} else {
console.log('connection successful');
}
});

/***********
Models
***********/

//User model
//Define our fields for the table
var UserSchema = new mongoose.Schema({
user_id: mongoose.Schema.ObjectId,
username: String,
password: String
});
//Create model object
var User = mongoose.model('user', UserSchema);


/***********
Routes
***********/
var bcrypt = require('bcrypt-nodejs'); //should be placed on top
//Renders our html file
app.get('/', function (req, res, next) {
res.sendFile( __dirname + '/index.html');
});
//render register.html when /register is called
app.get('/register', function (req, res, next) {
res.sendFile( __dirname + '/register.html');
});

app.get('/home', function (req, res, next) {
res.sendFile(__dirname + '/home.html');
});

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

/* Login logic for passport.authenticate*/
passport.use(new LocalStrategy(
function(username, password, done) {
User.findOne({ username: username }, function (err, user) {
if(user !== null) {
var isPasswordCorrect = bcrypt.compareSync(password, user.password);
if(isPasswordCorrect) {
console.log("Username and password correct!");
return done(null, user);
} else {
console.log("Password incorrect!");
return done(null, false);
}
} else {
console.log("Username does not exist!");
return done(null, false);
}
});
}
));

/**********
Serialize and Deserialize here for passport.authenticate
**********/
passport.serializeUser(function(user, done) {
done(null, user);
});

passport.deserializeUser(function(user, done) {
done(err, user);
});

app.post('/register', function (req, res, next) {
var password = bcrypt.hashSync(req.body.password);
req.body.password = password;

User.create(req.body, function(err, saved) {
if(err) {
console.log(err);
res.json({ message : err });
} else {
res.json({ message : "User successfully registered!"});
}
});
});

app.listen(port, '0.0.0.0', function() {
console.log('Server running at port ' + port);
});

最佳答案

原因是您定义了 var LocalStratgy,而不是 LocalStrategy。

关于javascript - Node.js 上的 Passport 模块 - 引用错误 : LocalStrategy is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44470961/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com