我不知道如何在注册表单中使用带有passport.js的快速验证器,以及哪些用户将数据保存在数据库中我发送这些文件以检查它并检查错误 当我在没有expressvalidator的情况下单独使用护照时,它总是会给我带来失败的重定向没有成功的重定向
``在这里输入代码`这是用户模型
var express = require('express');
var router = express.Router();
var User = require("../models/user");
var passport = require("passport");
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.get("/signup", function (req, res) {
"use strict";
res.render("signup", {title: "signup"});
});
router.get("/login", function (req, res) {
"use strict";
res.render("login", {title: "login"});
});
router.get("/profile", function (req, res, next) {
"use strict";
res.render("user/profile");
});
router.post("/signup", function (req, res, next) {
"use strict";
// form values
var name = req.body.name;
var username = req.body.username;
var email = req.body.email;
var password = req.body.password;
var confirmpassword = req.body.confirmpassword;
//check for profile image
if (req.files && req.files.profileimage) {
var profileImageOriginalNmae = req.files.profileimage.originalname;
var profileImageName = req.files.profileimage.name;
var profileImagePath = req.files.profileimage.path;
var profileImageExt = req.files.profileimage.extention;
var profileImageSize = req.files.profileimage.size;
var profileImageMimeType = req.files.profileimage.mimetype;
} else {
var profileImageNmae = "noimage.png";
}
//check form validation
req.checkBody("name", "name is required").notEmpty();
req.checkBody("username", "username is required").notEmpty();
req.checkBody("email", "email is invalid").isEmail();
req.checkBody("password", "password is required").notEmpty();
req.checkBody("confirmpassword", "password does not match").equals(req.body.password);
//check for errors
var errors = req.validationErrors();
if (errors) {
res.render("signup", {
errors: errors,
name: name,
username: username,
email: email,
password: password,
confirmpassword: confirmpassword
});
} else {
var newUser = new User({
name: name,
username: username,
email: email,
password: password,
profileimage: profileImageName
});
User.createUser(newUser, function (err, user) {
if (err) {
throw err;
}
console.log(user);
});
//success message
req.flash("success_msg", "now you can login");
res.redirect("/user/login");
}
}, passport.authenticate("local-signup", {
successRedirect: "/user/profile",
failureRedirect: "/user/login",
failureFlash: true
}));
module.exports = router;
// importing passport modules
var passport = require("passport");
var LocalStrategy = require("passport-local").Strategy;
var User = require("../models/user");
passport.serializeUser(function (user, done) {
"use strict";
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
"use strict";
User.findById(id, function (err, user) {
done(err, user);
});
});
passport.use("local-signup", new LocalStrategy({
usrnameField: "username",
passwordField: "password",
passReqToCallback: true
}, function (req, username, password, done) {
"use strict";
User.findOne({"username": username}, function (err, user) {
if (err) {
return done(err);
}
if (user) {
return done(null, false, {message: "username is already un use"});
} else {
var newUser = new User();
newUser.username = username;
newUser.password = password;
}
});
}));
## this is user routes file ##
var express = require('express');
var router = express.Router();
var User = require("../models/user");
var passport = require("passport");
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
router.get("/signup", function (req, res) {
"use strict";
res.render("signup", {title: "signup"});
});
router.get("/login", function (req, res) {
"use strict";
res.render("login", {title: "login"});
});
router.get("/profile", function (req, res, next) {
"use strict";
res.render("user/profile");
});
router.post("/signup", function (req, res, next) {
"use strict";
// form values
var name = req.body.name;
var username = req.body.username;
var email = req.body.email;
var password = req.body.password;
var confirmpassword = req.body.confirmpassword;
//check for profile image
if (req.files && req.files.profileimage) {
var profileImageOriginalNmae = req.files.profileimage.originalname;
var profileImageName = req.files.profileimage.name;
var profileImagePath = req.files.profileimage.path;
var profileImageExt = req.files.profileimage.extention;
var profileImageSize = req.files.profileimage.size;
var profileImageMimeType = req.files.profileimage.mimetype;
} else {
var profileImageNmae = "noimage.png";
}
//check form validation
req.checkBody("name", "name is required").notEmpty();
req.checkBody("username", "username is required").notEmpty();
req.checkBody("email", "email is invalid").isEmail();
req.checkBody("password", "password is required").notEmpty();
req.checkBody("confirmpassword", "password does not match").equals(req.body.password);
//check for errors
var errors = req.validationErrors();
if (errors) {
res.render("signup", {
errors: errors,
name: name,
username: username,
email: email,
password: password,
confirmpassword: confirmpassword
});
} else {
var newUser = new User({
name: name,
username: username,
email: email,
password: password,
profileimage: profileImageName
});
User.createUser(newUser, function (err, user) {
if (err) {
throw err;
}
console.log(user);
});
//success message
req.flash("success_msg", "now you can login");
res.redirect("/user/login");
}
}, passport.authenticate("local-signup", {
successRedirect: "/user/profile",
failureRedirect: "/user/login",
failureFlash: true
}));
module.exports = router;
`