Passport.js-通过仅检查用户名进行身份验证的用户

时间:2018-08-13 08:54:03

标签: node.js passport.js passport-local

我正在尝试仅使用存储在PostGreSQL数据库中的用户名来认证用户。目前,它正在返回一个错误,提示“缺少凭据”。我假设我可能需要使用一个我既没有也不希望使用的密码。我正在学习passport.js,因为我最终会希望合并单一登录。我已经按照文档进行了调整,并根据需要进行了调整。我输入了存储在数据库中的用户名,但不喜欢它。有什么原因吗?如果需要,我可以提供指向我的GitHub的链接。

user.js

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

const Staff = require('../models/Staff');

module.exports = router;

router.get('/login', function (req, res) {
    res.render('login');
});

passport.use(new LocalStrategy(
    function (username, password, done) {
        Staff.findOne({
            username: username
        }, function (err, user) {
            if (err) {
                return done(err);
            }
            if (!user) {
                return done(null, false, {
                    message: 'Incorrect username.'
                });
            }
            return done(null, user);
        });
    }
));

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

passport.deserializeUser(function (id, done) {
    Staff.findById(id, function (err, user) {
        done(err, user);
    });
});

router.post('/login/post',
    passport.authenticate('local', {
        successRedirect: '/',
        failureRedirect: '/login',
        failureFlash: true
    })
);

staff.js-模型

const Sequelize = require('sequelize');
const db = require('../db');

// Staff table
const Staff = db.define('staff', {
    userName: {
        type: Sequelize.STRING
    },
    firstName: {
        type: Sequelize.STRING
    },
    lastName: {
        type: Sequelize.STRING
    },
    email: {
        type: Sequelize.STRING
    }
});


module.exports = Staff;

index.js

const router = require('express').Router();

const Staff = require('../models/Staff');
const Course = require('../models/Course');
const Occurrence = require('../models/Occurrence');
const Moderation = require('../models/Moderation');

module.exports = router;

// Index
router.get('/', ensureAuthenticated, function (req, res) {
    Staff.findOne({
        where: {
            userName: userName
        },
        include: [{
            model: Occurrence,
            include: [Course]
        }]
    }).then(staff => {
        Moderation.findAll({
            where: {
                doneById: staff.id,
                status: 'submitted'
            },
            include: [{
                model: Staff,
                as: 'doneFor'
            }]
        }).then(forMe => {
            res.render('index', {
                staff,
                forMe
            });
        });
    });
});

function ensureAuthenticated(req, res, next) {
    if (req.isAuthenticated()) {
        return next();
    } else {
        req.flash('danger', 'You are currently not logged in.');
        res.redirect('/login');
    }
}

login.pug

body.bg-dark
    .container
        .login-content
            .login-logo
                h1#login-header Course Manager Admin
            .login-form  
                form(action='/login/post', method='POST')
                    != messages('message', locals) 
                    .form-group
                        label.control-label.mb-1(for='username') Username *
                        input#userName.form-control(name='username', type='text')
                    //- .form-group
                    //-     label.control-label.mb-1(for='password') Password *
                    //-     input#password.form-control(name='password', type='password')
                    button.btn.btn-success.btn-flat.m-b-30.m-t-30(type='submit') Sign in

1 个答案:

答案 0 :(得分:0)

默认情况下,LocalStrategy需要用户名和密码,但是您可以轻松地覆盖它:

    passport.use(new LocalStrategy({
        usernameField: 'username',
        passwordField: 'username',
      },
      function(username, password, done) {
        // ...
      }
    ));

基本上,username字段用于用户名和密码。只需忽略密码并使用用户名即可。