-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from jjikky/feature/auth
Feature/auth : 로컬 로그인 / 카카오 로그인
- Loading branch information
Showing
18 changed files
with
827 additions
and
29 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,13 @@ | ||
const SucesssMessage = Object.freeze({ | ||
// USER | ||
// USER - 회원가입 | ||
REGISTER_SUCCESSS: '회원가입 성공', | ||
AVAILABLE_NICKNAME: '사용 가능한 닉네임입니다.', | ||
AVAILABLE_EMAIL: '사용 가능한 이메일입니다.', | ||
|
||
// USER - 로그인 | ||
LOGIN_SUCCESSS: '로그인 성공', | ||
|
||
GET_PROFILE_SUCCESS: '유저 정보 조회 성공', | ||
}); | ||
|
||
module.exports = SucesssMessage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const passport = require('passport'); | ||
const localStrategy = require('./localStrategy'); | ||
const jwtStrategy = require('./jwtStrategy'); | ||
const User = require('../../routes/user/user.model'); | ||
|
||
module.exports = () => { | ||
passport.serializeUser((user, done) => { | ||
done(null, user.id); | ||
}); | ||
|
||
passport.deserializeUser(async (id, done) => { | ||
try { | ||
const user = await User.findById(id); | ||
done(null, user); | ||
} catch (err) { | ||
done(err); | ||
} | ||
}); | ||
|
||
// 초기화 | ||
localStrategy(); | ||
jwtStrategy(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { Strategy: JwtStrategy, ExtractJwt } = require('passport-jwt'); | ||
const passport = require('passport'); | ||
const User = require('../../routes/user/user.model'); | ||
const config = require('../config'); // 비밀 키를 저장한 파일 | ||
|
||
const opts = { | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: config.jwtSecret, | ||
}; | ||
|
||
module.exports = () => { | ||
passport.use( | ||
new JwtStrategy(opts, async (jwtPayload, done) => { | ||
try { | ||
const user = await User.findById(jwtPayload.id); | ||
if (user) { | ||
return done(null, user); | ||
} else { | ||
return done(null, false); | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
return done(error, false); | ||
} | ||
}) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const passport = require('passport'); | ||
const LocalStrategy = require('passport-local').Strategy; | ||
const User = require('../../routes/user/user.model'); | ||
|
||
module.exports = () => { | ||
passport.use( | ||
new LocalStrategy( | ||
{ | ||
usernameField: 'email', | ||
passwordField: 'password', | ||
}, | ||
async (email, password, done) => { | ||
try { | ||
const user = await User.findOne({ email }); | ||
if (!user) { | ||
return done(null, false, { message: '가입되지 않은 회원입니다.' }); | ||
} | ||
const isMatch = await user.comparePassword(password); | ||
if (!isMatch) { | ||
return done(null, false, { message: '비밀번호가 일치하지 않습니다.' }); | ||
} | ||
return done(null, user); | ||
} catch (error) { | ||
console.error(error); | ||
return done(error); | ||
} | ||
} | ||
) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const passport = require('passport'); | ||
const config = require('../config'); | ||
|
||
exports.generateToken = (user) => { | ||
return jwt.sign( | ||
{ | ||
id: user._id, | ||
email: user.email, | ||
nickname: user.nickname, | ||
}, | ||
config.jwtSecret, | ||
{ expiresIn: '24h' } | ||
); | ||
}; | ||
|
||
exports.verifyToken = (token) => { | ||
return jwt.verify(token, config.jwtSecret); | ||
}; | ||
|
||
exports.isAuthenticated = (req, res, next) => { | ||
passport.authenticate('jwt', { session: false }, (err, user, info) => { | ||
if (err) { | ||
return res.status(500).json({ message: '서버 오류' }); | ||
} | ||
if (!user) { | ||
return res.status(401).json({ message: '인증되지 않은 사용자' }); | ||
} | ||
req.user = user; | ||
next(); | ||
})(req, res, next); | ||
}; | ||
|
||
exports.isLoggedIn = (req, res, next) => { | ||
passport.authenticate('jwt', { session: false }, (err, user, info) => { | ||
if (err) { | ||
return res.status(500).json({ message: '서버 오류' }); | ||
} | ||
if (!user) { | ||
return res.status(401).json({ message: '로그인이 필요합니다.' }); | ||
} | ||
req.user = user; | ||
next(); | ||
})(req, res, next); | ||
}; | ||
|
||
exports.isNotLoggedIn = (req, res, next) => { | ||
passport.authenticate('jwt', { session: false }, (err, user, info) => { | ||
if (err) { | ||
return res.status(500).json({ message: '서버 오류' }); | ||
} | ||
if (user) { | ||
return res.status(403).json({ message: '이미 로그인된 상태입니다.' }); | ||
} | ||
next(); | ||
})(req, res, next); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const axios = require('axios'); | ||
const conf = require('../../common/config'); | ||
|
||
const header = { | ||
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8', | ||
Authorization: 'Bearer ', | ||
}; | ||
exports.getKakaoToken = async (code) => { | ||
const data = { | ||
grant_type: 'authorization_code', | ||
client_id: conf.kakaoRestApiKey, | ||
code, | ||
}; | ||
|
||
const queryString = Object.keys(data) | ||
.map((k) => encodeURIComponent(k) + '=' + encodeURIComponent(data[k])) | ||
.join('&'); | ||
|
||
const token = await axios.post('https://kauth.kakao.com/oauth/token', queryString, { headers: header }); | ||
return { accessToken: token.data.access_token }; | ||
}; | ||
exports.getUserInfo = async (accessToken) => { | ||
// Authorization: 'Bearer access_token' | ||
header.Authorization += accessToken; | ||
|
||
// 카카오 사용자 정보 조회 | ||
const get = await axios.get('https://kapi.kakao.com/v2/user/me', { headers: header }); | ||
const result = get.data; | ||
|
||
return { | ||
snsId: result.id, | ||
email: result.kakao_account.email ? result.kakao_account.email : `${result.id}@no.agreement`, | ||
// NOTE: 닉네임 10글자 제한 때문에, 임시 처리 | ||
// kakao 닉네임 규정은 20글자. result.id는 10글자로 추정 | ||
nickname: result.id, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.