Showing
2 changed files
with
45 additions
and
0 deletions
passport/index.js
0 → 100644
1 | +const local = require('./localStrategy'); | ||
2 | +const { User } = require('../models'); | ||
3 | + | ||
4 | +module.exports = (passport) => { | ||
5 | + passport.serializeUser((user, done) => { | ||
6 | + done(null, user.id); | ||
7 | + }); | ||
8 | + | ||
9 | + passport.deserializeUser((id, done) => { | ||
10 | + User.find({ where: { id } }) | ||
11 | + .then(user => done(null, user)) | ||
12 | + .catch(err => done(err)); | ||
13 | + }); | ||
14 | + | ||
15 | + local(passport); | ||
16 | +}; |
passport/localStrategy.js
0 → 100644
1 | +const LocalStrategy = require('passport-local').Strategy; | ||
2 | +const bcrypt = require('bcrypt'); | ||
3 | + | ||
4 | +const { User } = require('../models'); | ||
5 | + | ||
6 | +module.exports = (passport) => { | ||
7 | + passport.use(new LocalStrategy({ | ||
8 | + usernameField: 'email', | ||
9 | + passwordField: 'password', | ||
10 | + }, async (email, password, done) => { | ||
11 | + try { | ||
12 | + const exUser = await User.find({ where: { email } }); | ||
13 | + if (exUser) { | ||
14 | + const result = await bcrypt.compare(password, exUser.password); | ||
15 | + if (result) { | ||
16 | + done(null, exUser); | ||
17 | + } else { | ||
18 | + done(null, false, { message: '비밀번호가 일치하지 않습니다.' }); | ||
19 | + } | ||
20 | + } else { | ||
21 | + done(null, false, { message: '가입되지 않은 회원입니다.' }); | ||
22 | + } | ||
23 | + } catch (error) { | ||
24 | + console.error(error); | ||
25 | + done(error); | ||
26 | + } | ||
27 | + })); | ||
28 | +}; | ||
29 | + |
-
Please register or login to post a comment