这是我的app.js
var express = require("express")
var User = require("./models/user");
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var passport = require("passport");
var passportStrategy = require("passport-local");
var passportLocalMongoose = require("passport-local-mongoose");
//mongo db
mongoose.connect("mongodb://localhost/auth_demo_app");
var app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true }));
app.use(require("express-session")({
secret: "Rusty",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
//ROUTS
app.get("/", function(req,res) {
res.render("home");
});
app.get("/secret", function(req,res) {
res.render("secret");
});
//Auth Routhes
//show the form
app.get("/register", function(req,res) {
res.render("register");
});
app.post("/register", function(req,res) {
req.body.username
req.body.password
User.register(new User({username: req.body.username}), req.body.password, function(err, user) {
if(err) {
console.log(err);
return res.render('register');
}
passport.authenticate("local")(req, res, function(){
res.redirect("/secret");
});
});
});
//this is the locall host
app.listen(3002, function(){
console.log("Server on port 3002")
});
这是用户模型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);
有人可以帮助我发现为什么我的页面一直处于等待状态的问题吗? 因为我在 User.register 所在的行中没有发现代码的任何问题 解决问题。 看起来它无法进入此方法,但我仍然不确定为什么 如果发现错误,那就太好了。
因为每次我查看stackoverflow时,我都会看到人们忘记像以前那样放置用户模型。 但事实并非如此
答案 0 :(得分:1)
你需要添加这一行:
passport.use(User.createStrategy());
在上面添加:
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
这样做,您的代码将是:
var express = require("express")
var User = require("./models/user");
var bodyParser = require("body-parser");
var mongoose `enter code here`= require("mongoose");
var passport = require("passport");
var passportStrategy = require("passport-local");
var passportLocalMongoose = require("passport-local-mongoose");
//mongo db
mongoose.connect("mongodb://localhost/auth_demo_app");
var app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true }));
app.use(require("express-session")({
secret: "Rusty",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(User.createStrategy);
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
答案 1 :(得分:0)
尝试一下
Users=new User({ username : req.body.username, name : req.body.name,
email: req.body.email });
User.register(Users, req.body.password,function(err, user) {
if (err) {
if(err.name === "UserExistsError" ){
res.json({success:false, message: "Username already exist"})
}else{
res.json({success:false, message:"Your account could not be saved. Error: ", err})
}
}else{
res.json({success: true, message: "Your account has been saved, Now you can login!"})
}
});