Notice
Recent Posts
Recent Comments
Link
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

Front-end Developer

<Nextjs> RouteGuard ์ปดํฌ๋„ŒํŠธ (2023/04/09) ๋ณธ๋ฌธ

React

<Nextjs> RouteGuard ์ปดํฌ๋„ŒํŠธ (2023/04/09)

Brad Daeho Lee 2023. 10. 12. 14:33

๐Ÿ’ก RouteGuard๋Š” page route๊ฐ„์— auth checkํ•˜๋Š” middleware ์ปดํฌ๋„ŒํŠธ์ž…๋‹ˆ๋‹ค.

๋„์ž…ํ•˜๊ฒŒ๋œ ์ด์œ โ“


  1. ๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•œ ํŽ˜์ด์ง€๋ฅผ ๋กœ๊ทธ์ธ ํ•˜์ง€ ์•Š๊ฑฐ๋‚˜ JWT ํ† ํฐ์ด ๋งŒ๋ฃŒ๋œ ๊ฒฝ์šฐ์— ๋ผ์šฐํŒ…์„ ์‹œ๋„ํ• ๊ฒฝ์šฐ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ redirectํ•˜๊ธฐ ์œ„ํ•ด์„œ
  2. ๋กœ๊ทธ์ธ ํ•œ ์ƒํƒœ์—์„œ ํŽ˜์ด์ง€๋ฅผ reloadํ•  ๊ฒฝ์šฐ JWT ํ† ํฐ์œผ๋กœ ๋‚ ์•„๊ฐ„ ์œ ์ €์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ

์ปดํฌ๋„ŒํŠธ ๋กœ์ง ์ •๋ฆฌ ๐Ÿ’ก


MyApp ์ปดํฌ๋„ŒํŠธ๋Š” Next.js app์˜ root component์ž…๋‹ˆ๋‹ค. <RouteGuard>๋Š” ํ˜„์žฌ ๋ Œ๋”๋ง๋˜๋Š” page Component <Component {…pageProps} />๋ฅผ children์œผ๋กœ ๊ฐ์‹ธ์„œ auth ๋กœ์ง์„ ์‹คํ–‰์‹œํ‚ค๋Š” middleware์˜ ์—ญํ• ์„ํ•œ๋‹ค.

RouteGuard ์ปดํฌ๋„ŒํŠธ๋Š” ์œ„์—์„œ ๋งํ–ˆ๋“ฏ์ด ํŽ˜์ด์ง€ routing ์ด๋ฒคํŠธ๊ฐ€ ์‹œ์ž‘๋  ๋•Œ auth ์ฒดํฌ ๋กœ์ง์„ ์‹คํ–‰์‹œํ‚ค๋Š” middleware์ž…๋‹ˆ๋‹ค.

  • ๋ฐ‘์— ์ฝ”๋“œ์—๋”ฐ๋ผ ์ˆœ์ฐจ์ ์œผ๋กœ ๋กœ์ง์„ ์„ค๋ช…ํ•˜์ž๋ฉด, setUser(setter ํ•จ์ˆ˜)๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์— ์œ ์ € ์ •๋ณด๋ฅผ ๋„ฃ์–ด์„œ recoil(์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ)๋กœ ๊ด€๋ฆฌํ•˜๋Š” user state ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
  • isAuthenticated๋Š” ํ˜„์žฌ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋กœ๊ทธ์ธํ•œ ์ƒํƒœ์ธ์ง€ ์•„๋‹Œ์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” state์ž…๋‹ˆ๋‹ค. boolean type์˜ ๊ฐ’์„ ๊ฐ€์ง€๊ณ ์žˆ๊ณ  default value๋Š” false์ž…๋‹ˆ๋‹ค. setIsAuthenticated๋Š” isAuthenticated๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” setter ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • routeGuardExceptedPathList๋Š” auth ์ฒดํฌ ๋กœ์ง์„ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•  ํ•„์š”์—†๋Š” ํŽ˜์ด์ง€ path๋ฅผ ์š”์†Œ๋กœ ๊ฐ€์ง€๊ณ ์žˆ๋Š” ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค.
  • isValidPath๋Š” ํ˜„์žฌ ํŽ˜์ด์ง€ path๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์„œ routeGuardExceptedPathList์— ํฌํ•จ๋œ path์ธ์ง€ ํ™•์ธํ•ด์„œ boolean type์„ returnํ•˜๋Š” callback ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. true๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด auth check ํ•จ์ˆ˜ ์‹คํ–‰, false๋ฅผ ๋ฆฌํ„ดํ•˜๋ฉด auth check ํ•จ์ˆ˜ ์‹คํ–‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

  • authCheck ํ•จ์ˆ˜๋Š” ๋กœ์ปฌ์Šคํ† ๋ฆฌ์ง€์— ์ €์žฅ๋œ JWT ํ† ํฐ๊ฐ’์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์˜ ๋กœ๊ทธ์ธ ์ƒํƒœ๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  • jwtToken ๋ณ€์ˆ˜ ๊ฐ’์ด undefined์ผ๊ฒฝ์šฐ ๋กœ๊ทธ์ธ์„ ํ•˜์ง€ ์•Š์•˜๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ณ  ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ redirect ์‹œํ‚ต๋‹ˆ๋‹ค. url query์— returnPath๋ฅผ ์ถ”๊ฐ€ํ•œ ์ด์œ ๋Š” ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐ„๋’ค์— ๋กœ๊ทธ์ธ ์„ฑ๊ณต์„ํ•˜๋ฉด ์›๋ž˜ routingํ•˜๋ ค๊ณ  ํ–ˆ๋˜ ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐ€๊ฒŒํ•˜๊ธฐ ์œ„ํ•ด์„œ url query์— path๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  • jwtToken ๋ณ€์ˆ˜ ๊ฐ’์ด undefined๊ฐ€ ์•„๋‹๊ฒฝ์šฐ JWT ํ† ํฐ์œผ๋กœ ์œ ์ € ์ •๋ณด๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค. then์œผ๋กœ return ํ•  ๊ฒฝ์šฐ ํ† ํฐ์ด ๋งŒ๋ฃŒ๋˜์ง€ ์•Š์•˜๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ณ  return ๋ฐ›์€ ์œ ์ € ์ •๋ณด๋ฅผ setUser์— ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  isAuthenticated ๊ฐ’์„ true๋กœ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ catch๋กœ return ํ•  ๊ฒฝ์šฐ ํ† ํฐ์ด ๋งŒ๋ฃŒ๋˜์—ˆ๋‹ค๊ณ  ํŒ๋‹จํ•˜๊ณ  ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋กœ redirect ์‹œํ‚ต๋‹ˆ๋‹ค.

  • RouteGuard ์ฒซ ๋ Œ๋”๋ง์‹œ์— authCheck ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํŽ˜์ด์ง€ ๋ผ์šฐํŒ…์ด ์‹œ์ž‘ํ•  ๋•Œ auth check๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก event (router.events.on(์ด๋ฒคํŠธ, ์ด๋ฒคํŠธ ์ผ์–ด๋‚ฌ์„ ๋•Œ ๋™์ž‘ํ•˜๋Š” ํ•จ์ˆ˜)๋ฅผ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค. nextPath ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” routing์„ ์‹œ๋„ํ•˜๋Š” path๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • RouteGuard ์ปดํฌ๋„ŒํŠธ๊ฐ€ return ๊ฐ’์˜ ํ˜•ํƒœ๋Š” null ๋˜๋Š” children์ž…๋‹ˆ๋‹ค. isAuthenticated๊ฐ€ false์ด๋ฉด์„œ(๋กœ๊ทธ์ธ์ด ๋‹ค์‹œ ํ•„์š”ํ•œ ์ƒํƒœ) isValidPath๊ฐ€ true์ด๋ฉด(ํ˜„์žฌ ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ) null์„ ๋ฐ˜ํ™˜์„ํ•ด์„œ ํ˜„ ํŽ˜์ด์ง€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ Œ๋”๋งํ•˜์ง€ ์•Š๊ณ  ๋นˆ ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ๊ทธ ์™ธ์— ๊ฒฝ์šฐ์—๋Š” page Component๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ๋กœ๊ทธ์ธ์„ ํ•˜์ง€ ์•Š์•˜๊ฑฐ๋‚˜ JWTํ† ํฐ์ด ๋งŒ๋ฃŒ๋˜์—ˆ๋”๋ผ๋„ ํ˜„์žฌ ํŽ˜์ด์ง€๊ฐ€ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€๋ผ๋ฉด null ๋Œ€์‹ ์— children์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์—์„œ๋Š” ๋กœ๊ทธ์ธ์ด ๋˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ ์ •์ƒ์ ์œผ๋กœ ํ™”๋ฉด์„ ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.


Comments