javascript—未知身份验证策略passport js本地策略错误的原因是什么?

brc7rcf0  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(147)

以下是 passport-local.js 文件-

'use strict';

const passport = require('passport');
const User = require('../models/user');
const LocalStrategy = require('passport-local');

passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  User.findById(id, (err, user) => {
    done(err, user);
  });
});

passport.use(
  'local.signup',
  new LocalStrategy(
    {
      usernameField: 'email',
      passwordField: 'password',
      passreqTocallback: true,
    },
    (req, email, password, done) => {
      User.findOne({ email: email }, (err, user) => {
        if (err) {
          return done(err);
        }
        if (user) {
          return done(
            null,
            false,
            req.flash('error', 'User with the email already exists')
          );
        }
        const newUser = new User();
        newUser.local.username = req.body.username;
        newUser.local.fullname = req.body.username;
        newUser.local.email = req.body.email;
        newUser.local.password = newUser.encryptPassword(req.body.password);

        newUser.save((err) => {
          done(null, newUser);
        });
      });
    }
  )
);

当我跑的时候 nodemon server ,一切正常,但当我输入凭据并单击“注册”时,终端中出现以下错误-

(node:21567) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
node:events:371
      throw er; // Unhandled 'error' event
      ^

TypeError: Cannot read property 'username' of undefined
    at /home/prithvi/Desktop/Ideal-table-talk/passport/passport-local.js:38:43
    at /home/prithvi/Desktop/Ideal-table-talk/node_modules/mongoose/lib/model.js:5065:18
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
Emitted 'error' event on Function instance at:
    at /home/prithvi/Desktop/Ideal-table-talk/node_modules/mongoose/lib/model.js:5067:15
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

浏览器中显示错误-

Confirm Form Resubmission.
This web page requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed.
Press the reload button to resubmit the data needed to load the page.
ERR_CACHE_MISS

我如何解决这个问题?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题