BoilerPlate Code 08 - Auth Route

Ji Yeon Parkยท2021๋…„ 1์›” 9์ผ
0

Create Boilerplate Code

๋ชฉ๋ก ๋ณด๊ธฐ
9/10
post-thumbnail
post-custom-banner

โœ๐Ÿป Auth Route๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

*๏ธโƒฃ Auth๋ž€?

Auth๋Š” ์ธ์ฆ(Authentication), ๊ถŒํ•œ๋ถ€์—ฌ(Authorization) ๋“ฑ์˜ ์ค„์ž„๋ง๋กœ ์ธ์ฆ(authentication)์€ ์ž์‹ ์ด ๋ˆ„๊ตฌ๋ผ๊ณ  ์ฃผ์žฅํ•˜๋Š” ์‚ฌ๋žŒ์„ ํ™•์ธํ•˜๋Š” ์ ˆ์ฐจ์ด๋ฉฐ ๊ถŒํ•œ๋ถ€์—ฌ(authorization)๋Š” ๊ฐ€๊ณ  ์‹ถ์€ ๊ณณ์œผ๋กœ ๊ฐ€๋„๋ก ํ˜น์€ ์›ํ•˜๋Š” ์ •๋ณด๋ฅผ ์–ป๋„๋ก ํ—ˆ์šฉํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.

์ฆ‰, Auth Route๋ž€ ์‰ฝ๊ฒŒ ๋งํ•ด ์ธ์ฆ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

*๏ธโƒฃ Auth Route๋Š” ์™œ ํ•„์š”ํ• ๊นŒ?

ํŽ˜์ด์ง€๋งˆ๋‹ค ๋กœ๊ทธ์ธ์ด ๋˜์–ด์žˆ๋Š”์ง€ ์•ˆ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ํŽ˜์ด์ง€์— ๊ถŒํ•œ์ด ์žˆ๋Š” ๊ด€๋ฆฌ์ž ์œ ์ €์ธ์ง€ ๋“ฑ์„ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด

*๏ธโƒฃ Auth Route ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•


ํ† ํฐ์„ ๋งŒ๋“ค๋ฉด์„œ Client์—๋Š” Cookie, Server์—๋Š” DB์— ์ €์žฅ์„ ํ•ด์คฌ๋‹ค.
๊ทธ๋ž˜์„œ ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ์— ์žˆ๋Š” ๋‘๊ฐ€์ง€ ํ† ํฐ์ด ์ผ์น˜ํ•˜๋Š”๊ฐ€๋ฅผ ๊ณ„์†์ ์œผ๋กœ ์ฒดํฌํ•ด์„œ ์ธ์ฆ ๊ถŒํ•œ์„ ํ™•์ธํ•ด์ค€๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด AํŽ˜์ด์ง€์—์„œ BํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐˆ ๋•Œ,
User๊ฐ€ BํŽ˜์ด์ง€์—๋„ ๊ถŒํ•œ์ด ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด
Client์—์„œ Server๋กœ ์ฟ ํ‚ค์— ๋‹ด๊ฒจ์ ธ ์žˆ๋Š” Token์„ ์ „๋‹ฌํ•œ๋‹ค.

Client์—์„œ Server๋กœ ๋„˜์–ด๊ฐ€๋Š” ํ† ํฐ์€ ์ฟ ํ‚ค์•ˆ์— encode ๋˜์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— JWT๋กœ ๋””์ฝ”๋“œํ•ด์„œ ๋‚˜์˜จ User ID๋ฅผ ์„œ๋ฒ„์— ๋„˜๊ฒจ์ฃผ๊ฒŒ ๋œ๋‹ค.
๊ทธ UserID๋ฅผ ๊ฐ€์ง„ User ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— Token์ด ์žˆ๋‹ค๋ฉด ์ผ์น˜, Token์ด ์—†๋‹ค๋ฉด ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ฒŒ ๋งŒ๋“ค์–ด์ค€๋‹ค.


โœ… Auth Route ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿท Auth Route ๊ตฌํ˜„์ˆœ์„œ

1๏ธโƒฃ Cookie์—์„œ ์ €์žฅ๋œ Token์„ Server์—์„œ ๊ฐ€์ ธ์™€์„œ ๋ณตํ˜ธํ™”๋ฅผ ํ•œ๋‹ค.
2๏ธโƒฃ ๋ณตํ˜ธํ™”๋ฅผ ํ•˜๋ฉด UserID๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ ๊ทธ User ID๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค User Collection์— ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.
3๏ธโƒฃ ์œ ์ €๋ฅผ ์ฐพ์€ ํ›„ ์ฟ ํ‚ค์—์„œ ๋ฐ›์•„์˜จ Token์„ ์œ ์ €๋„ ๊ฐ–๊ณ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.


๐Ÿท Auth Route ์ƒ์„ฑ

*๏ธโƒฃ Get์„ ์ด์šฉํ•ด์„œ Auth Route ์ฝ”๋“œ ์ž‘์„ฑ

app.get('/api/users/auth', auth , (req, res) => {})

'์—”๋“œํฌ์ธํŠธ' ๊ทธ๋ฆฌ๊ณ  (req, res) ์ค‘๊ฐ„์— auth๋ฏธ๋“œ์›จ์–ด๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

โœ”๏ธ ๋ฏธ๋“œ์›จ์–ด๋ž€?
์—”๋“œํฌ์ธํŠธ์—์„œ ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ๋ฐ›์€ ํ›„, ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ์ค‘๊ฐ„์—์„œ ๋จผ์ € ์‹คํ–‰๋˜๋Š” ๊ฒƒ.

*๏ธโƒฃ ๋ฏธ๋“œ์›จ์–ด๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด middleware ํด๋”์™€ auth.jsํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

*๏ธโƒฃ auth.js ํŒŒ์ผ ์•ˆ์— ์ธ์ฆ์ฒ˜๋ฆฌ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค๊ธฐ

let auth =(req, res, next) => {
    // ์ธ์ฆ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ณณ
    // 1. Client Cookie์—์„œ Token์„ ๊ฐ€์ ธ์˜จ๋‹ค.
    // 2. Token์„ ๋ณตํ˜ธํ™” ํ•œ ํ›„ ์œ ์ €๋ฅผ ์ฐพ๋Š”๋‹ค.
    // 3. ์œ ์ €๊ฐ€ ์žˆ์œผ๋ฉด ์ธ์ฆ Okay
    // 4. ์œ ์ €๊ฐ€ ์—†์œผ๋ฉด ์ธ์ฆ No!
}
module.exports = { auth };

โœ”๏ธ let token = req.cookie.x_auth ๋ถ€๋ถ„์€ Login Route์—์„œ cookie์— ์ €์žฅํ•ด์ค€ x_auth๋ฅผ ๊ฐ€์ ธ์˜จ๊ฒƒ.

โœ”๏ธ ๊ฐ€์ ธ์˜จ Token์œผ๋กœ User๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด User๋ชจ๋ธ์— findByToken ํ•จ์ˆ˜ ์ƒ์„ฑํ•˜๊ธฐ

โœ”๏ธ ๋ฏธ๋“œ์›จ์–ด ํŒŒ์ผ ์ฝ”๋“œ ์™„์„ฑํ•˜๊ธฐ

*๏ธโƒฃ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ •๋ณด ์ „๋‹ฌํ•˜๊ธฐ

โœ”๏ธ isAdmin ์ฝ”๋“œ๋Š” User๋ชจ๋ธ์— role 0์„ ์ผ๋ฐ˜์œ ์ €๋กœ, ์ง€์ •ํ–ˆ๊ธฐ๋•Œ๋ฌธ์— 0์ด ์•„๋‹ˆ๋ฉด ๊ด€๋ฆฌ์ž์ด๋‹ค.
์˜ˆ๋ฅผ๋“ค์–ด role1 = admin์ด๊ณ  role2 = ํŠน์ •๋ถ€์„œ admin์ด๋ผ๋ฉด ์„ค์ •ํ•œ๊ฑฐ์— ๋”ฐ๋ผ isAdmin ์ฝ”๋“œ๊ฐ€ ๋‹ฌ๋ผ ์งˆ ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ“š Reference

์ถœ์ฒ˜:
https://www.inflearn.com/course/%EB%94%B0%EB%9D%BC%ED%95%98%EB%A9%B0-%EB%B0%B0%EC%9A%B0%EB%8A%94-%EB%85%B8%EB%93%9C-%EB%A6%AC%EC%95%A1%ED%8A%B8-%EA%B8%B0%EB%B3%B8/lecture/37075?tab=curriculum

profile
Frontend Developer
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€