CHZZK-Study / Grass-Diary-Client

์ทจ์ง€์ง 2ํŒ€ ํ”„๋กœ์ ํŠธ Grass Diary
1 stars 3 forks source link

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

Open rkdcodus opened 1 day ago

rkdcodus commented 1 day ago

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

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

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

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

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 };
};

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

const hasExpired = (expirationDate: Date) => {
  if (!expirationDate) return false;
  const currentDate = new Date();
  const timeDiff = expirationDate.getTime() - currentDate.getTime();
  return timeDiff <= 0 ? true : false;
};
  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]);

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

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

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

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

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

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

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

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

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

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