localStrategy.js 1.2 KB
const LocalStrategy = require('passport-local').Strategy;
const models = require('../models/index');
const bcrypt = require("bcrypt");

module.exports = (passport) => {
    passport.use(new LocalStrategy({
            usernameField: 'email',
            passwordField: 'password'
        }, async (email, password, done) => {
            try {
                let user = await models.User.findOne({
                    where: {email}
                });
                if (!user) {
                    return done(null, false, {message: "유저 데이터가 존재하지 않습니다."});
                }

                let resultOfPasswordCheck = await bcrypt.compare(password, user.hashedPassword);
                if (!resultOfPasswordCheck) {
                    return done(null, false, {message: '비밀번호 에러입니다'});
                }
                user = await models.User.findOne({
                    where:{email},
                    attributes: ['id', 'email', 'nickName']
                });
                return done(null, user);
            } catch (e) {
                console.error(e);
                return done(e);
            }
        })
    );
};