繁体   English   中英

使用Passport js和mongoDB的用户登录

[英]User login with Passport js and mongoDB

我已经看到这个问题了,很抱歉,它似乎是重复的。 但是我已经阅读了很多答案,但并没有帮助。 无论我使用登录表单做什么,我都不断收到“ MISSING CREDENTIALS”错误。 我正在尝试使用电子邮件和密码来实现用户登录。

我的路线文件

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

passport.use(new LocalStrategy((email, password, done) => {
  User.getUserByEmail(email, (err, user) => {
    if(err) throw err;
    if(!user) {
      return done(null, false, {message: 'this account does not exist'});
    }

    User.comparePassword(password, user.password, (err, isMatch) => {
      if(err) throw err;
      if(isMatch) {
        return done(null, user);
      }
      else {
        return done(null, false, {message: 'oops! wrong password! try again'});
      }
    });
  });
}));

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

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

router.post('/', (req, res, next) => {
  passport.authenticate('local', {
    successRedirect: '/',
    failureRedirect: '/toSignInPage',
    failureFlash: true
  })(req, res, next);
});

module.exports = router;

user.js文件

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const bcrypt = require('bcryptjs');

const UserSchema = new Schema({
  email: {
    type: String,
    required: true,
    trim: true
  },
  name: {
    type: String,
    required: true,
    unique: true,
    trim: true
  },
  password: {
    type: String,
    required: true,
    trim: true
  },
  profilePhoto: {
    originalemail: String,
    imagePath: String
  },
  token: String
});

const User = module.exports = mongoose.model('User', UserSchema);

module.exports.registerUser = function(newUser, callback) {
  bcrypt.genSalt(10, (err, salt) => {
    bcrypt.hash(newUser.password, salt, (err, hash) => {
      if(err) {
        console.log(err);
      }
      newUser.password = hash;
      newUser.save(callback);
    });
  });
};

module.exports.getUserByEmail = function(email, callback) {
  const query = {
    email: email
  };
  User.findOne(query, callback);
};

module.exports.getUserById = function(id, callback) {
  User.findById(id, callback);
};

module.exports.comparePassword = function(candidatePassword, hash, callback) {
  bcrypt.compare( candidatePassword, hash, (err, isMatch) => {
    if(err) throw err;
    callback(null, isMatch);
  });
};

我已经在我的app.js文件中初始化了护照-会话选项以及所有这些不错的东西。

还有我的表单的html(handlebars)

       <form method="post" action="/signInPage">


          <label for="mail"> Email </label>
          <input type="email" id="mail" name="email" />
          <label for="password"> Password</label>
          <input type="password" id="password" name="password"/>

          <button type="submit">Sign in</button>

          <p class="corp"> <a href="forgot.html">Forgot password?</a> </p>

        </form>

任何帮助表示赞赏。 我调试了一段时间,似乎无法弄清楚我做错了什么。 如果我没有以易于理解的格式正确设置问题的格式,请告诉我。

我很感激的人发布了该解决方案的链接; 但我找不到评论,所以我只添加解决方案。

Passport js默认使用用户名登录用户

passport.use(new LocalStrategy((username, password, done) => {

}));

所以我想用电子邮件

passport.use(new LocalStrategy((email, password, done) => {

}));

为了使用电子邮件登录用户,我必须这样做

passport.use(new LocalStrategy((email, password, done) => {
  {
    usernameField : 'email',
    passwordField : 'password'
}
}));

这将覆盖默认使用用户名发送电子邮件的方式...

这是灵魂的链接:

https://github.com/scotch-io/easy-node-authentication/blob/master/config/passport.js#L58

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM