用户验证失败:密码:路径“密码”是必需的
User validation failed: password: Path `password` is required
我正在尝试注册用户,然后使用 passport-local-mongoose 对他们进行身份验证。当我尝试注册用户时出现错误 'User validation failed: password: Path password
is required.' 我通过将 [=34= 中 userSchema 的 password
字段的 required
更改为 false
来消除错误] 但我认为必须输入密码。有谁知道更好的解决方案?
而且我不明白为什么当我为用户包含密码字段时它会给我错误。
当我将 required
更改为 false
以获取 password
时,注册有效,我最终将用户存储在用户集合中,但随后身份验证给了我一个 Status 400 Bad Request 错误,我完全被卡住了。
这是我的server.js
import express from "express";
import mongoose from "mongoose";
import userRouter from "./routers/userRouter.js";
import session from "express-session";
import bodyParser from "body-parser";
import passport from "passport";
import User from "./models/userModel.js";
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true}));
app.use(session({
secret: "some secret sentence",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect(process.env.MONGODB_URL || "mongodb://localhost/somedatabase", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use("/api/users", userRouter);
app.get("/", (req, res) => {
res.send("Server is ready");
});
app.use((err, req, res, next) => {
res.status(500).send({ message: err.message });
});
app.listen(5000, () => {
console.log("Serve at http://localhost:5000");
});
userRouter.js:
import express from "express";
import expressAsyncHandler from "express-async-handler";
import User from "../models/userModel.js";
import passport from "passport";
import passportLocalMongoose from "passport-local-mongoose";
userRouter.post("/register", expressAsyncHandler(async (req, res) =>{
const createdUser = new User({name: req.body.name, username: req.body.email})
User.register(createdUser, req.body.password, async function(err, user){
if(err){
console.log(err);
res.redirect("http://localhost:3000/register");
}else{
passport.authenticate("local")(req ,res , function(){
console.log("authenticate successful");
res.send({
_id: createdUser._id,
user: createdUser.name,
email: createdUser.username,
isAdmin: createdUser.isAdmin,
token: generateToken(user)
})
})
}
})
userModel.js
import mongoose from "mongoose";
import passport from "passport";
import passportLocalMongoose from "passport-local-mongoose";
const userSchema = new mongoose.Schema(
{
name: {type: String, required: true},
username: {type: String, required: true, unique: true},
password: {type: String, required: true},
isAdmin: {type: Boolean, default: false, required: true},
},
{
timestamps: true,
}
);
userSchema.plugin(passportLocalMongoose);
const User = mongoose.model("User", userSchema);
export default User;
RegisterScreen.jsx
import React,{ useState, useEffect } from "react";
import { Link } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";
import { register } from "../actions/userActions.js"
export default function RegisterScreen(props){
const[email,setEmail] = useState("");
const[name,setName] = useState("");
const[password, setPassword] = useState("");
const[confirmPassword, setConfirmPassword] = useState("");
const redirect = props.location.search? props.location.search.split("=")[1] : "/";
const userRegister = useSelector((state) => state.userRegister);
const {userInfo , loading, error} = userRegister;
const dispatch = useDispatch();
const submitHandler = (e) =>{
e.preventDefault();//prevents refresh
if(password !== confirmPassword){
alert("Password and confirm password do not match")
}else{
dispatch(register(name, email, password));
}
};
useEffect(() =>{
if(userInfo){
props.history.push(redirect);
}
}, [userInfo, redirect, props.history]);
return (
<div className="container mt-5">
<h1>Register</h1>
{
loading && <h2>Loading</h2>
}
{ error && <h1>{error}</h1>}
<div className="row">
<div className="col-sm-8">
<div className="card">
<div className="card-body">
<form onSubmit={submitHandler}>
<div className="form-group">
<label htmlFor="email">Name</label>
<input
type="text"
id="name"
className="form-control"
placeholder="Enter name"
required
onChange={(e) => setName(e.target.value)}
></input>
</div>
<div className="form-group">
<label htmlFor="email">Email</label>
<input
type="email"
id="email"
className="form-control"
placeholder="Enter email"
required
onChange={(e) => setEmail(e.target.value)}
></input>
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input
type="password"
id="password"
placeholder="Enter password"
className="form-control"
required
onChange={(e) => setPassword(e.target.value)}
></input>
</div>
<div className="form-group">
<label htmlFor="confirmPassword">Confirm Password</label>
<input
type="password"
id="confirmPassword"
placeholder="Enter password"
className="form-control"
required
onChange={(e) => setConfirmPassword(e.target.value)}
></input>
</div>
<button type="submit" className="btn btn-dark">Register</button>
<div>
Already have an account? {' '}
<Link to={`/login?redirect=${redirect}`}>Log into your account</Link>
</div>
</form>
</div>
</div>
</div>
<div className="col-sm-4">
<div className="card">
<div className="card-body">
<a className="btn btn-block btn-social btn-google" href="/auth/google" role="button">
<i className="fab fa-google"></i>
Sign In with Google
</a>
</div>
</div>
</div>
</div>
</div>)
}
从模型中删除 password
字段。护照负责
我正在尝试注册用户,然后使用 passport-local-mongoose 对他们进行身份验证。当我尝试注册用户时出现错误 'User validation failed: password: Path password
is required.' 我通过将 [=34= 中 userSchema 的 password
字段的 required
更改为 false
来消除错误] 但我认为必须输入密码。有谁知道更好的解决方案?
而且我不明白为什么当我为用户包含密码字段时它会给我错误。
当我将 required
更改为 false
以获取 password
时,注册有效,我最终将用户存储在用户集合中,但随后身份验证给了我一个 Status 400 Bad Request 错误,我完全被卡住了。
这是我的server.js
import express from "express";
import mongoose from "mongoose";
import userRouter from "./routers/userRouter.js";
import session from "express-session";
import bodyParser from "body-parser";
import passport from "passport";
import User from "./models/userModel.js";
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true}));
app.use(session({
secret: "some secret sentence",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
mongoose.connect(process.env.MONGODB_URL || "mongodb://localhost/somedatabase", {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
});
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use("/api/users", userRouter);
app.get("/", (req, res) => {
res.send("Server is ready");
});
app.use((err, req, res, next) => {
res.status(500).send({ message: err.message });
});
app.listen(5000, () => {
console.log("Serve at http://localhost:5000");
});
userRouter.js:
import express from "express";
import expressAsyncHandler from "express-async-handler";
import User from "../models/userModel.js";
import passport from "passport";
import passportLocalMongoose from "passport-local-mongoose";
userRouter.post("/register", expressAsyncHandler(async (req, res) =>{
const createdUser = new User({name: req.body.name, username: req.body.email})
User.register(createdUser, req.body.password, async function(err, user){
if(err){
console.log(err);
res.redirect("http://localhost:3000/register");
}else{
passport.authenticate("local")(req ,res , function(){
console.log("authenticate successful");
res.send({
_id: createdUser._id,
user: createdUser.name,
email: createdUser.username,
isAdmin: createdUser.isAdmin,
token: generateToken(user)
})
})
}
})
userModel.js
import mongoose from "mongoose";
import passport from "passport";
import passportLocalMongoose from "passport-local-mongoose";
const userSchema = new mongoose.Schema(
{
name: {type: String, required: true},
username: {type: String, required: true, unique: true},
password: {type: String, required: true},
isAdmin: {type: Boolean, default: false, required: true},
},
{
timestamps: true,
}
);
userSchema.plugin(passportLocalMongoose);
const User = mongoose.model("User", userSchema);
export default User;
RegisterScreen.jsx
import React,{ useState, useEffect } from "react";
import { Link } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";
import { register } from "../actions/userActions.js"
export default function RegisterScreen(props){
const[email,setEmail] = useState("");
const[name,setName] = useState("");
const[password, setPassword] = useState("");
const[confirmPassword, setConfirmPassword] = useState("");
const redirect = props.location.search? props.location.search.split("=")[1] : "/";
const userRegister = useSelector((state) => state.userRegister);
const {userInfo , loading, error} = userRegister;
const dispatch = useDispatch();
const submitHandler = (e) =>{
e.preventDefault();//prevents refresh
if(password !== confirmPassword){
alert("Password and confirm password do not match")
}else{
dispatch(register(name, email, password));
}
};
useEffect(() =>{
if(userInfo){
props.history.push(redirect);
}
}, [userInfo, redirect, props.history]);
return (
<div className="container mt-5">
<h1>Register</h1>
{
loading && <h2>Loading</h2>
}
{ error && <h1>{error}</h1>}
<div className="row">
<div className="col-sm-8">
<div className="card">
<div className="card-body">
<form onSubmit={submitHandler}>
<div className="form-group">
<label htmlFor="email">Name</label>
<input
type="text"
id="name"
className="form-control"
placeholder="Enter name"
required
onChange={(e) => setName(e.target.value)}
></input>
</div>
<div className="form-group">
<label htmlFor="email">Email</label>
<input
type="email"
id="email"
className="form-control"
placeholder="Enter email"
required
onChange={(e) => setEmail(e.target.value)}
></input>
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input
type="password"
id="password"
placeholder="Enter password"
className="form-control"
required
onChange={(e) => setPassword(e.target.value)}
></input>
</div>
<div className="form-group">
<label htmlFor="confirmPassword">Confirm Password</label>
<input
type="password"
id="confirmPassword"
placeholder="Enter password"
className="form-control"
required
onChange={(e) => setConfirmPassword(e.target.value)}
></input>
</div>
<button type="submit" className="btn btn-dark">Register</button>
<div>
Already have an account? {' '}
<Link to={`/login?redirect=${redirect}`}>Log into your account</Link>
</div>
</form>
</div>
</div>
</div>
<div className="col-sm-4">
<div className="card">
<div className="card-body">
<a className="btn btn-block btn-social btn-google" href="/auth/google" role="button">
<i className="fab fa-google"></i>
Sign In with Google
</a>
</div>
</div>
</div>
</div>
</div>)
}
从模型中删除 password
字段。护照负责