Skip to content

Commit

Permalink
✨ feat: refresh token 로직 수정 #65
Browse files Browse the repository at this point in the history
  • Loading branch information
froggy1014 committed Oct 13, 2024
1 parent f2906e6 commit d8c1efd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
44 changes: 23 additions & 21 deletions auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down Expand Up @@ -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;
},

Expand Down
23 changes: 13 additions & 10 deletions src/components/core/Button/BasicButton/BasicButton.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { ButtonHTMLAttributes, FC } from "react";
import { ComponentPropsWithoutRef, ElementType } from "react";

import { cn } from "@/utils/cn";

interface Props
extends Omit<
ButtonHTMLAttributes<HTMLButtonElement>,
"children" | "label" | "className"
> {
interface Props<T extends ElementType> {
as?: T;
label: string;
className?: string;
}

const BasicButton: FC<Props> = ({ className, label, ...props }) => {
const BasicButton = <T extends ElementType = "button">({
as,
className,
label,
...props
}: Props<T> & Omit<ComponentPropsWithoutRef<T>, keyof Props<T>>) => {
const Component = as || "button";
return (
<button
<Component
className={cn(
"w-full h-[48px] duration-300 rounded-[12px] py-[8px] px-[12px]",
"flex flex-col items-center justify-center gap-[12px]",
Expand All @@ -24,8 +27,8 @@ const BasicButton: FC<Props> = ({ className, label, ...props }) => {
)}
{...props}
>
<div className="w-[90%] truncate">{label}</div>
</button>
<div className="max-w-[90%] truncate">{label}</div>
</Component>
);
};

Expand Down
6 changes: 5 additions & 1 deletion src/lib/jwt.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit d8c1efd

Please sign in to comment.