gpt4 book ai didi

node.js - Passport 本地 Mongoose : Authenticate user right after registration

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:38 25 4
gpt4 key购买 nike

我正在尝试使用passport-local-mongoose在/register 表单上提交 POST 后对用户进行身份验证。我不明白为什么我总是得到“未经授权”并且奇怪的是数据库上没有提交任何数据(如果我从 app.post("/register") 中删除 "passport.authenticate('local') ,它就会起作用)

有什么想法吗?


app.js:

    // ====== Dependencies
var express = require('express');
bodyParser = require('body-parser');
validator = require('express-validator');
cookieParser = require('cookie-parser');
expressSession = require('express-session');
passport = require('passport');
LocalStrategy = require('passport-local').Strategy;
auth = require('./controllers/auth');
registration = require('./controllers/register');
User = require('./models/user');
// ====== Options
var app = express()
.use(bodyParser.urlencoded({ extended: false }))
.use(bodyParser.json())
.use(validator())
.set('view engine','ejs')
.use(express.static("views"))
.use(cookieParser())
.use(expressSession({
secret : 'soooosecret',
resave : true,
saveUninitialized : false
}))
.use(passport.initialize())
.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.post('/register', passport.authenticate('local'), registration.reg );

app.listen(8080);
console.log('Server running at 8080');

register.js:

var User = require('../models/user');

var reg = function (req, res) {

/* some form validation... */
var errors = req.validationErrors();

if (errors) {
res.render("register" , { errors : errors } );
}
else {
var newUser = new User({
username : username,
password : password,
email : email,
tel : tel,
country : country
});

User.createUser(newUser);
}

}
module.exports = { reg : reg }

user.js:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mvpem');

var passportLocalMongoose = require('passport-local-mongoose');

var db = mongoose.connection;

var Schema = mongoose.Schema;

var UserSchema = new Schema({
username : String,
password : String,
email : String,
tel : Number,
country : String
});

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model('User', UserSchema);

module.exports.createUser = function(newUser) {
newUser.save();
}

最佳答案

根据passport-local-mongoose的API文档https://github.com/saintedlama/passport-local-mongoose你通过调用register(user,password,cb)来注册一个用户

所以你的 register.js 应该看起来像这样

 ...

if (errors) {
res.render("register" , { errors : errors } );
}
else {
var newUser = new User({
username : username,
email : email,
tel : tel,
country : country
});

User.register(newUser, password, function(err, user) {
if (errors) {
// handle the error
}
passport.authenticate("local")(req, res, function() {
// redirect user or do whatever you want
});
});
}

并且不要忘记在 register.js 中需要 Passport

关于node.js - Passport 本地 Mongoose : Authenticate user right after registration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36465196/

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