NodeJS Passport身份验证问题

ltskdhd1  于 2023-04-11  发布在  Node.js
关注(0)|答案(2)|浏览(139)

我正在使用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'
}

我已经通过整个代码一遍又一遍,但我没有能够创建一个新的用户,按照它再次与教程视频我正在使用,仍然没有什么区别

zlwx9yxi

zlwx9yxi1#

您对以下行有问题

// 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} ...

希望有帮助!

2izufjch

2izufjch2#

我认为问题来自于此:
int getchar();
而不是:
const app = express();

相关问题