Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ”จ fix: ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ ๋กœ์ง ๋ณ€๊ฒฝ #229

Merged
merged 8 commits into from
Oct 28, 2024

Conversation

rkdcodus
Copy link
Collaborator

@rkdcodus rkdcodus commented Oct 22, 2024

โœ… ์ฒดํฌ๋ฆฌ์ŠคํŠธ

  • 5์ดˆ๋งˆ๋‹ค memberId ์š”์ฒญ ๋Œ€์‹  ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ ํƒ€์ด๋จธ ๊ตฌํ˜„ํ•˜๊ธฐ

๐Ÿ“ ์ž‘์—… ์ƒ์„ธ ๋‚ด์šฉ

๊ธฐ์กด ์ฝ”๋“œ๋Š” memberId๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” useQuery๋ฅผ 5์ดˆ๋งˆ๋‹ค ํ˜ธ์ถœํ•ด์„œ ๋กœ๊ทธ์ธ์ด ๋งŒ๋ฃŒ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.
setInterval ํ•จ์ˆ˜๋กœ ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ ํƒ€์ด๋จธ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ถˆํ•„์š”ํ•œ api ์š”์ฒญ์„ ์ค„์ด๋ฉด์„œ ๋กœ๊ทธ์ธ์ด ๋งŒ๋ฃŒ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๋„๋ก ๋ณ€๊ฒฝํ–ˆ์Šต๋‹ˆ๋‹ค.

authStore์— ์ƒˆ๋กœ์šด state ์ถ”๊ฐ€

  • expDate: ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ ์‹œ๊ฐ ์ €์žฅ
  • setExpDate: expDate ์—…๋ฐ์ดํŠธ ํ•จ์ˆ˜

useExpDate ํ›… ์ƒ์„ฑ

const useExpDate = () => {
  const accessToken = localStorage.getItem('accessToken');
  const { setExpDate } = useAuthActions();

  const handleExpDate = () => {
    if (accessToken) {
      try {
        const decode = jwtDecode(accessToken);
        if (!decode.exp) return;
        const date = new Date(0);
        date.setUTCSeconds(decode.exp);
        setExpDate(date);
      } catch (error) {
        console.error('Failed to decode token:', error);
      }
    }
  };

  return { handleExpDate };
};
  • handleExpDateํ•จ์ˆ˜๋Š” ๋กœ๊ทธ์ธ์ด ๋งŒ๋ฃŒ๋˜๋Š” ์‹œ๊ฐ์„ ๊ตฌํ•ด expDate ์ƒํƒœ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
  • ์—‘์„ธ์Šค ํ† ํฐ์„ ๋””์ฝ”๋”ฉํ•˜๋ฉด ๋งŒ๋ฃŒ ์‹œ๊ฐ์„ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์—‘์„ธ์Šค ํ† ํฐ์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ jwt-decode ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    ์ฐธ๊ณ : ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๋Š” jwt-decode ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

Header ์ปดํฌ๋„ŒํŠธ์— ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ ํƒ€์ด๋จธ ๊ตฌํ˜„

const hasExpired = (expirationDate: Date) => {
  if (!expirationDate) return false;
  const currentDate = new Date();
  const timeDiff = expirationDate.getTime() - currentDate.getTime();
  return timeDiff <= 0 ? true : false;
};
  • hasExpired ํ•จ์ˆ˜๋Š” ์—‘์„ธ์Šค ํ† ํฐ์ด ๋งŒ๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด true, ์•„๋‹ˆ๋ผ๋ฉด false๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
  • ์ธ์ˆ˜๋กœ ๋ฐ›์€ ๋งŒ๋ฃŒ ์‹œ๊ฐ๊ณผ ํ˜„์žฌ ์‹œ๊ฐ์˜ ์ฐจ์ด๋ฅผ ๊ตฌํ•ด 0๋ณด๋‹ค ์ž‘๋‹ค๋ฉด true๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
  useEffect(() => {
    let expireInterval: NodeJS.Timeout; 
    if (!expDate) handleExpDate(); // ๋งŒ๋ฃŒ ์‹œ๊ฐ์ด null์ด๋ผ๋ฉด ๊ตฌํ•ฉ๋‹ˆ๋‹ค.
    
    if (expDate) {
      expireInterval = setInterval(() => {
        if (hasExpired(expDate)) { // ์—‘์„ธ์Šค ํ† ํฐ์ด ๋งŒ๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด
          const setting = {
            title: MODAL.authentication_error.title,
            content: MODAL.authentication_error.content,
          };

          const button1 = {
            active: true,
            text: MODAL.confirm,
            color: semantic.light.accent.solid.hero,
            interaction: INTERACTION.accent.subtle(),
            clickHandler: () => (window.location.href = '/'),
          };

          clearAuth(true); // ๋กœ๊ทธ์•„์›ƒ์‹œํ‚ค๊ธฐ
          clearInterval(expireInterval); // setInterval ์‹คํ–‰ ์ •์ง€
          modal(setting, button1); // ๋ชจ๋‹ฌ ๋„์šฐ๊ธฐ
        }
      }, 60000);
    }

    return () => {
      if (expireInterval) clearInterval(expireInterval);
    };
  }, [memberId, expDate]);
  • 1๋ถ„(60000ms)๋งˆ๋‹ค ๋งŒ๋ฃŒ ์‹œ๊ฐ๊ณผ ํ˜„์žฌ ์‹œ๊ฐ์„ ๋น„๊ตํ•˜์—ฌ ๋งŒ๋ฃŒ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.
  • ๋งŒ๋ฃŒ๋˜์—ˆ๋‹ค๋ฉด ๋กœ๊ทธ์•„์›ƒ์„ ์‹œํ‚ค๊ณ  ๋ชจ๋‹ฌ์„ ๋„์›๋‹ˆ๋‹ค.
  • ๋ชจ๋‹ฌ์˜ ํ™•์ธ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์†Œ๊ฐœํŽ˜์ด์ง€๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
    • ์ด๋ฅผ ์œ„ํ•ด useLogout์„ ์กฐ๊ธˆ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.
    • clearAuth()์— isExp ์ธ์ˆ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ์—๋Š” ์†Œ๊ฐœํŽ˜์ด์ง€ ์ด๋™ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.

setInterval์€ 1๋ถ„๋งˆ๋‹ค ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ๋ฅผ ํ™•์ธํ•˜๋Š”๋ฐ ์ด๋•Œ ํ™•์ธ๋˜๋Š” ๋งŒ๋ฃŒ์— ์˜ํ•ด ๋ชจ๋‹ฌ์ด ๋œฐ ๊ฒฝ์šฐ, ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ ์‹œ๊ฐ„ 30๋ถ„์ด ์ง€๋‚˜\n์ž๋™์œผ๋กœ ๋กœ๊ทธ์•„์›ƒ ๋˜์—ˆ์–ด์š” ๋ฉ”์‹œ์ง€๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ setInterval ํ•จ์ˆ˜๊ฐ€ ๋จผ์ € ๋งŒ๋ฃŒ๋ฅผ ํ™•์ธํ•˜์ง€ ๋ชปํ•œ ์ƒํƒœ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ํŽ˜์ด์ง€ ์ด๋™/์ƒˆ๋กœ๊ณ ์นจ์œผ๋กœ ์—‘์„ธ์Šค ํ† ํฐ์ด ๋งŒ๋ฃŒ๋œ ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ์„ ๊ฒฝ์šฐ์—๋Š” useUser ๋‚ด๋ถ€์˜ 401์—๋Ÿฌ ๋ชจ๋‹ฌ์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ ์•ก์„ธ์Šค ํ† ํฐ ์ถ”์ถœ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋กœ๊ทธ์ธ ํ•ด์ฃผ์„ธ์š” ๋ฉ”์‹œ์ง€๊ฐ€ ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค.

๐Ÿšจ ๋ฒ„๊ทธ ๋ฐœ์ƒ ์ด์œ  (์„ ํƒ ์‚ฌํ•ญ)

๐Ÿ”Ž ํ›„์† ์ž‘์—… (์„ ํƒ ์‚ฌํ•ญ)

๐Ÿค” ์งˆ๋ฌธ ์‚ฌํ•ญ (์„ ํƒ ์‚ฌํ•ญ)

๐Ÿ“š ์ฐธ๊ณ  ์ž๋ฃŒ (์„ ํƒ ์‚ฌํ•ญ)

์ถ”๊ฐ€์ ์ธ ์ฐธ๊ณ  ์‚ฌํ•ญ์ด๋‚˜ ๊ด€๋ จ๋œ ๋ฌธ์„œ, ๋งํฌ ๋“ฑ์„ ์ œ๊ณตํ•ด์ฃผ์„ธ์š”.

๐Ÿ“ธ ์Šคํฌ๋ฆฐ์ƒท (์„ ํƒ ์‚ฌํ•ญ)

๋ณ€๊ฒฝ ์‚ฌํ•ญ์— ๋Œ€ํ•œ ์Šคํฌ๋ฆฐ์ƒท์ด ์žˆ๋‹ค๋ฉด ์ฒจ๋ถ€ํ•ด์ฃผ์„ธ์š”.

โœ… ์…€ํ”„ ์ฒดํฌ๋ฆฌ์ŠคํŠธ

  • ๋ธŒ๋žœ์น˜ ํ™•์ธํ•˜๊ธฐ
  • ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ€์ง€ ์•Š์•˜๋Š”์ง€ ์žฌํ™•์ธํ•˜๊ธฐ
  • issue ๋‹ซ๊ธฐ
  • reiewers, assignees, Lables ๋“ฑ๋ก ํ™•์ธํ•˜๊ธฐ

์ด์Šˆ ๋ฒˆํ˜ธ: Close #227

5์ดˆ๋งˆ๋‹ค memberId api ์š”์ฒญ ์ฝ”๋“œ ๋Œ€์ฒด
after:
1. ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ ๋ฐœ์ƒ (5์ดˆ๋งˆ๋‹ค ํ™•์ธ)
2.  memberId ๊ฐ€์ ธ์˜ค๋Š” useQuery๋ฅผ refetch
3. 401 ์—๋Ÿฌ ๋ฐœ์ƒ
4. ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ๋‹ด์€ ๋ชจ๋‹ฌ ์‹คํ–‰
5. ํ™•์ธ ๋ฒ„ํŠผ์œผ๋กœ ์†Œ๊ฐœํŽ˜์ด์ง€ ์ด๋™

๋ฌธ์ œ์  : 5์ดˆ๋ผ๋Š” ์งง์€ ์ฃผ๊ธฐ๋กœ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค์ด ๋Š๋ ค์งˆ ์ˆ˜ ์žˆ์Œ. ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ ๋ฉ”์‹œ์ง€๊ฐ€ ์•„๋‹˜.

before:
1. ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ ๋ฐœ์ƒ (1๋ถ„๋งˆ๋‹ค ํ™•์ธ)
2. ๋กœ๊ทธ์•„์›ƒ ์‹œํ‚ค๊ธฐ
3. ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ ๋ฉ”์‹œ์ง€ ๋‹ด์€ ๋ชจ๋‹ฌ ์‹คํ–‰
4. ํ™•์ธ ๋ฒ„ํŠผ์œผ๋กœ ์†Œ๊ฐœํŽ˜์ด์ง€ ์ด๋™
expDate, setExpDate ์ถ”๊ฐ€
๊ธฐ์กด์˜ getTokenExpirationDate ํ•จ์ˆ˜๋ฅผ ๋ฆฌํŒฉํ† ๋ง.
์ธ์ˆ˜๊ฐ€ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ๊ฒƒ์€ ์•„๋‹ˆ๋ฉฐ ์žˆ์„ ๊ฒฝ์šฐ ์†Œ๊ฐœํŽ˜์ด์ง€ ์ด๋™ ์ฝ”๋“œ ์‹คํ–‰๋˜์ง€ ์•Š์Œ.
@rkdcodus rkdcodus added โœจfeature ๊ตฌํ˜„, ๊ฐœ์„  ์‚ฌํ•ญ ๊ด€๋ จ ๋ถ€๋ถ„ ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ปfrontend ํ”„๋ก ํŠธ์—”๋“œ ์ž‘์—… ๐Ÿช„refactor ๋ฆฌํŒฉํ† ๋ง labels Oct 22, 2024
@rkdcodus rkdcodus requested review from KKYHH and ccconac October 22, 2024 11:02
@rkdcodus rkdcodus self-assigned this Oct 22, 2024
Copy link
Member

@KKYHH KKYHH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ณ ์ƒํ•˜์…จ์Šต๋‹ˆ๋‹ค ์ฑ„์—ฐ ๋‹˜ ๐Ÿซก
์ฑ„์—ฐ ๋‹˜ ๋•์— ๋„คํŠธ์›Œํฌ ํƒญ์ด ์พŒ์ ํ•ด์ง€๊ฒ ๊ตฐ์š” ๐Ÿ‘

const handleExpDate = () => {
if (accessToken) {
try {
const decode = jwtDecode(accessToken);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jwt-decode ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ accessToken์„ ๋””์ฝ”๋”ฉ ํ•ด์„œ ๋งŒ๋ฃŒ ์‹œ๊ฐ„์„ ์ถ”์ถœํ•˜๊ณ  ์ง€์ •ํ•œ ์‹œ๊ฐ„์ด๋ž‘ ๋น„๊ตํ•˜๋Š” ๊ฑฐ๊ตฐ์š”!

์ œ๊ฐ€ ํ”ผ๋“œ๋ฐฑ ๋ฐ›์€ ๋‚ด์šฉ์€ ์ฟ ํ‚ค ์‚ฌ์šฉ์ด์—ˆ๋Š”๋ฐ ์ €ํฌ ๋กœ๊ทธ์ธํ•  ๋•Œ ๋ฐ›์€ accessToken ์œ ํšจ ์‹œ๊ฐ„์„ ์ฟ ํ‚ค์— ์ €์žฅํ•˜๋Š” ๊ฒŒ ์•ˆ๋์—ˆ๋‚˜์š”? ์•„๋‹ˆ๋ฉด ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ ์‚ฌ์šฉ์ด ์•ˆ ๋์—ˆ๋‚˜์š”? ์ฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ์ข€ ๋” ํšจ์œจ์ ์ด๋ผ๋Š”๋ฐ ์ „์— ๋ญ”๊ฐ€ ์•ˆ๋ผ์„œ ํž˜๋“ค์—ˆ๋˜ ๊ฑฐ ๊ฐ™์€๋ฐ..๐Ÿค”

์ฑ„์—ฐ ๋‹˜ ๊ธฐ์–ตํ•˜์‹ค๊นŒ์š”?! ๊ถ๊ธˆํ•ด์ ธ์„œ ์—ฌ์ญค๋ด…๋‹ˆ๋‹ค ๐Ÿ™‡

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊ทธ๋Ÿฌ๊ฒŒ์š”! ์ฟ ํ‚ค์— ์ €์žฅํ•˜์ง€ ์•Š์€ ์ด์œ ๊ฐ€ ์žˆ๋˜๊ฑฐ๊ฐ™์€๋ฐ ๊ธฐ์–ต์ด ์ž˜ ์•ˆ๋‚˜๋„ค์š” ํ ํ  ๐Ÿค”
๋ฆฌํ”„๋ ˆ์‰ฌ ํ† ํฐ์€ ์—†๊ณ  ์—‘์„ธ์Šค ํ† ํฐ์ด๋ผ๋„ ์ฟ ํ‚ค์— ๋„ฃ์–ด๋ณด๋„๋ก ํ•œ ๋ฒˆ ์‹œ๋„ํ•ด๋ณผ๊ฒŒ์š”!

@rkdcodus rkdcodus merged commit a821fc2 into JECT-Study:dev Oct 28, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
โœจfeature ๊ตฌํ˜„, ๊ฐœ์„  ์‚ฌํ•ญ ๊ด€๋ จ ๋ถ€๋ถ„ ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ปfrontend ํ”„๋ก ํŠธ์—”๋“œ ์ž‘์—… ๐Ÿช„refactor ๋ฆฌํŒฉํ† ๋ง
Projects
None yet
Development

Successfully merging this pull request may close these issues.

๐Ÿ”จfix: ๋กœ๊ทธ์ธ ๋งŒ๋ฃŒ ํ™•์ธ ๋กœ์ง ์ˆ˜์ •
2 participants