-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge : accesstoken으로 로그인
- Loading branch information
Showing
7 changed files
with
87 additions
and
52 deletions.
There are no files selected for viewing
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
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,28 +1,34 @@ | ||
const { User } = require("../models/User"); | ||
const jwt = require("jsonwebtoken"); | ||
// 인증 처리를 하는곳 | ||
let auth = async (req, res, next) => { | ||
// 클라이언트에서 토큰을 가져온다 | ||
let token = req.headers.authorization; | ||
if (token) { | ||
const userdata = await User.findByToken(token); | ||
req.user = userdata; | ||
return next(); | ||
// 클라이언트에서 헤더로 보내준 accesstoken | ||
let accesstoken = req.headers.authorization; | ||
// accesstoken이 있을때 조작된 jwt인지 확인후 미들웨어 종료 | ||
if (accesstoken) { | ||
try { | ||
const userdata = jwt.verify(accesstoken.split(" ")[1], "secretToken"); | ||
req.name = userdata.username; | ||
next(); | ||
} catch (err) { | ||
return res.json({ isAuth: false, error: err }); | ||
} | ||
} else { | ||
// refresh 토큰으로 access 토큰 재발급 | ||
} | ||
try { | ||
// 토큰을 복호화 한후 유저를 찾는다. | ||
const userdata = await User.findByToken(token); | ||
// 유저가 업으면 no | ||
if (!userdata) return res.json({ isAuth: false, error: true }); | ||
// 같은 유저가 있으면 ok | ||
// 미들웨어가 끝나고 req에서 사용할 수 있게 데이터를 보내준다. | ||
req.token = token; | ||
req.user = userdata; | ||
next(); | ||
} catch (err) { | ||
return res.json({ isAuth: false, error: err }); | ||
return res.json({ isAuth: false }); | ||
} | ||
// try { | ||
// // 토큰을 복호화 한후 유저를 찾는다. | ||
// const userdata = await User.findByToken(token); | ||
// // 유저가 업으면 no | ||
// if (!userdata) return res.json({ isAuth: false, error: true }); | ||
// // 같은 유저가 있으면 ok | ||
// // 미들웨어가 끝나고 req에서 사용할 수 있게 데이터를 보내준다. | ||
// req.token = token; | ||
// req.user = userdata; | ||
// next(); | ||
// } catch (err) { | ||
// return res.json({ isAuth: false, error: err }); | ||
// } | ||
}; | ||
|
||
module.exports = { auth }; |
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