简体   繁体   English

允许用户使用电子邮件或用户名使用 Passport、LocalStrategy 登录

[英]Allowing users to use either email or username for login using Passport, LocalStrategy

I am relatively new to web development, and I am creating a website with NodeJs and Express that would ask for a username and email when registering.我对 Web 开发比较陌生,我正在使用 NodeJs 和 Express 创建一个网站,在注册时会要求输入用户名和电子邮件。 Currently, it only accepts username for login.目前,它只接受用户名登录。 I am using Passport and LocalStrategy.我正在使用 Passport 和 LocalStrategy。 I want users to be able to provide either the username or email in the same input field, How can I do that?我希望用户能够在同一输入字段中提供用户名或电子邮件,我该怎么做? I have searched the internet and found similar questions, but I haven't been able to implement their answers successfully.我在互联网上搜索并发现了类似的问题,但我无法成功实施他们的答案。 Here are the relevant parts of my app.js这是我的 app.js 的相关部分

var express    = require("express"),
    mongoose   = require("mongoose"),
    passport = require("passport"),
    LocalStrategy = require("passport-local"),
    User = require("./models/user");

// PASSPORT CONFIGURATION
app.use(require("express-session")({
    secret: "Once again Rusty wins cutest dog!",
    resave: false,
    saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

// Register
app.get("/register", function(req, res) {
    res.render("register");
});
app.post("/register", function(req, res){
    var newUser = new User({email: req.body.email, username: req.body.username});
    console.log(newUser);
    User.register(newUser, req.body.password, function(err, user){
        if(err) {
            console.log(err);
            return res.render("register");
        }
        passport.authenticate("local")(req, res, function(){
           res.redirect("/");
        });
    });
});

// Login
app.get("/login" , function(req, res) {
    res.render("login");
});
app.post("/login", passport.authenticate("local", 
    {
        successRedirect: "/campgrounds",
        failureRedirect: "/login"
    }), function(req, res) {
});

This is my user model这是我的用户模型

var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");

var UserSchema = new mongoose.Schema({
    email: String,
    username: String,
    password: String
});

UserSchema.plugin(passportLocalMongoose);

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

Thanks!谢谢!

I was diving deeper into the docs of Passport Local Mongoose and found a way to do this.我正在深入研究 Passport Local Mongoose 的文档,并找到了一种方法。 I'll leave the answer here in case someone finds it useful.如果有人觉得它有用,我会在这里留下答案。 When doing the plugin to the user model one can specify options:在对用户模型执行插件时,可以指定选项:

User.plugin(passportLocalMongoose, options);

With the option usernameQueryFields one can provide other fields to look for the username (eg. email).使用 usernameQueryFields 选项可以提供其他字段来查找用户名(例如电子邮件)。 So I changed my user.js like this:所以我像这样改变了我的 user.js:

UserSchema.plugin(passportLocalMongoose, {usernameQueryFields: ["email"]});

And now it users can provide either email or username in the login form现在用户可以在登录表单中提供电子邮件或用户名

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

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