From d8c1efdb6b562ecce9385dfdf35695264d24cdf3 Mon Sep 17 00:00:00 2001 From: Jeongmin Lee Date: Sun, 13 Oct 2024 16:16:37 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20refresh=20token=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95=20#65?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auth.ts | 44 ++++++++++--------- .../core/Button/BasicButton/BasicButton.tsx | 23 +++++----- src/lib/jwt.ts | 6 ++- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/auth.ts b/auth.ts index 28917a1..bcee232 100644 --- a/auth.ts +++ b/auth.ts @@ -26,6 +26,7 @@ export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({ async signIn() { return true; }, + redirect: async ({ url, baseUrl }) => { if (url.startsWith("/")) return `${baseUrl}${url}`; if (url) { @@ -40,27 +41,6 @@ export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({ return baseUrl; }, async jwt({ token, session, user, trigger, account }) { - if (!!token.accessToken) { - const decodedJWT = decodeToken(token.accessToken); - - if ( - !!token.refreshToken && - !!decodedJWT?.exp && - decodedJWT?.exp * 1000 < Date.now() - ) { - console.log("토큰 재발급"); - const { accessToken, refreshToken } = await getRefreshToken( - token.refreshToken, - ); - - const decodedJWT = decodeToken(accessToken); - - token.accessToken = accessToken; - token.refreshToken = refreshToken; - token.exp = decodedJWT?.exp; - } - } - if (trigger === "update") { token.user = { ...session.user, @@ -93,6 +73,28 @@ export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({ } } + if (!token.accessToken) { + return null; + } + + const decodedJWT = decodeToken(token.accessToken); + if ( + !!token.refreshToken && + !!decodedJWT?.exp && + decodedJWT?.exp * 1000 < Date.now() + ) { + console.log("토큰 재발급"); + const { accessToken, refreshToken } = await getRefreshToken( + token.refreshToken, + ); + + const decodedJWT = decodeToken(accessToken); + + token.accessToken = accessToken; + token.refreshToken = refreshToken; + token.exp = decodedJWT?.exp; + } + return token; }, diff --git a/src/components/core/Button/BasicButton/BasicButton.tsx b/src/components/core/Button/BasicButton/BasicButton.tsx index 2a600f5..272c9d2 100644 --- a/src/components/core/Button/BasicButton/BasicButton.tsx +++ b/src/components/core/Button/BasicButton/BasicButton.tsx @@ -1,19 +1,22 @@ -import { ButtonHTMLAttributes, FC } from "react"; +import { ComponentPropsWithoutRef, ElementType } from "react"; import { cn } from "@/utils/cn"; -interface Props - extends Omit< - ButtonHTMLAttributes, - "children" | "label" | "className" - > { +interface Props { + as?: T; label: string; className?: string; } -const BasicButton: FC = ({ className, label, ...props }) => { +const BasicButton = ({ + as, + className, + label, + ...props +}: Props & Omit, keyof Props>) => { + const Component = as || "button"; return ( - +
{label}
+ ); }; diff --git a/src/lib/jwt.ts b/src/lib/jwt.ts index 3f9b025..e59f58c 100644 --- a/src/lib/jwt.ts +++ b/src/lib/jwt.ts @@ -1,6 +1,10 @@ import { decodeJwt } from "jose"; -export const decodeToken = (token: string) => { +export const decodeToken = (token?: string) => { + if (!token) { + return null; + } + try { const decoded = decodeJwt(token); return decoded;