护照认证问题

问题描述 投票:0回答:1

我正在使用 NodeJS 并学习如何对用户进行身份验证和进行会话。我正在使用以下软件包:express-session、passport、passport-local、passport-local-mongoose

我经历过的过程:

onst app = express();

app.use(express.static("public"));
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));

app.use(session({
    secret: process.env.SECRET,
    resave: false,
    saveUninitialized: true
}));

app.use(passport.initialize());
app.use(passport.session());

// connect to MongoDB
mongoose.set('strictQuery', true);
mongoose.connect('mongodb://localhost:27017/secretsDB');

const userSchema = new mongoose.Schema ({
    username: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    }
});

userSchema.plugin(passportLocalMongoose);
const User = new mongoose.model('User', userSchema);

// PASSPORT - serializer and deserializer
passport.use(User.createStrategy());

passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.post("/register", function(req, res){
    console.log(req.body.username, req.body.password);

    User.register({username: req.body.username}, req.body.password, function(err, user) {
        if (err) {
            console.log(err);
            res.redirect("/register")
        } else {
            console.log("successfully registered user")

            const authenticate = User.authenticate();
            authenticate(req.body.username, req.body.password, function(err, result) {
                if (err) {
                    console.log(err);
                } else {
                    res.redirect("/secrets")
                }
            });
        }
    });
});

但是它一直在终端上显示这个错误:

即使测试密码请求是否正确,也会根据请求正确记录用户名和密码

Server started on PORT 3000
user@test 1234
Error: User validation failed: password: Path `password` is required.
    at ValidationError.inspect (/home/akpet/udemy/Secrets - Starting Code/node_modules/mongoose/lib/error/validation.js:50:26)
    at internal/per_context/primordials.js:23:32
    at formatValue (internal/util/inspect.js:783:19)
    at inspect (internal/util/inspect.js:337:10)
    at formatWithOptionsInternal (internal/util/inspect.js:2017:40)
    at formatWithOptions (internal/util/inspect.js:1899:10)
    at console.value (internal/console/constructor.js:323:14)
    at console.log (internal/console/constructor.js:358:61)
    at /home/akpet/udemy/Secrets - Starting Code/app.js:79:21
    at /home/akpet/udemy/Secrets - Starting Code/node_modules/passport-local-mongoose/index.js:247:63 {
  errors: {
    password: ValidatorError: Path `password` is required.
        at validate (/home/akpet/udemy/Secrets - Starting Code/node_modules/mongoose/lib/schematype.js:1346:13)
        at SchemaString.SchemaType.doValidate (/home/akpet/udemy/Secrets - Starting Code/node_modules/mongoose/lib/schematype.js:1330:7)
        at /home/akpet/udemy/Secrets - Starting Code/node_modules/mongoose/lib/document.js:2905:18
        at processTicksAndRejections (internal/process/task_queues.js:77:11) {
      properties: [Object],
      kind: 'required',
      path: 'password',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'User validation failed'
}

我把整个代码一遍又一遍,但我一直无法创建新用户,按照我正在使用的教程视频再次进行,仍然没有区别

node.js express-session passport-local passport-local-mongoose
1个回答
0
投票

你对以下行有问题

// you're attaching the password incorrectly.
User.register({username: req.body.username}, req.body.password, ...

应该是

User.register({username: req.body.username, password: req.body.password} ...

希望对您有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.