From 9024cfc8ab2b582f4bec970cd972c17caf746c40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=8C=EC=9A=B8=EC=B9=98=ED=82=A8?= <90738604+soulchicken@users.noreply.github.com> Date: Wed, 11 Oct 2023 13:48:02 +0900 Subject: [PATCH 1/4] =?UTF-8?q?Feat:=20refresh=20=ED=86=A0=ED=81=B0?= =?UTF-8?q?=EC=9D=B4=20=EC=96=B4=EB=94=94=EA=B9=8C=EC=A7=80=20=EB=B0=9B?= =?UTF-8?q?=EC=95=84=EC=99=80=EC=A7=80=EB=8A=94=20=EC=A7=80=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=ED=95=98=EA=B8=B0=20=EC=9C=84=ED=95=9C=20=EC=BB=A4?= =?UTF-8?q?=EB=B0=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/api/auth/[...nextauth].ts | 20 ++++++++++++++------ src/utils/api/accounts.ts | 4 ++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/pages/api/auth/[...nextauth].ts b/src/pages/api/auth/[...nextauth].ts index e818a66..616a8b1 100644 --- a/src/pages/api/auth/[...nextauth].ts +++ b/src/pages/api/auth/[...nextauth].ts @@ -48,6 +48,8 @@ export const authOptions: NextAuthOptions = { provider: 'credential', }); + console.log('authorize', data); + if (data.nickname && data.accessToken) { const user = { accessToken: data.accessToken, @@ -106,9 +108,13 @@ export const authOptions: NextAuthOptions = { // accessToken 만료를 검사합니다. if (token.accessToken && isTokenExpired(token.accessToken as string)) { // 만료된 경우 refreshToken으로 새 accessToken을 발급 - const newAccessToken = await refreshAccessToken(token.accessToken as string); - if (newAccessToken) { - token.accessToken = newAccessToken; // 새로운 accessToken으로 업데이트 + const newToken = await refreshAccessToken(token.accessToken as string); + + console.log('newAccessToken -- jwt', newToken); + + if (newToken) { + token.accessToken = newToken.accessToken; // 새로운 accessToken으로 업데이트 + token.refreshToken = newToken.refreshToken; // 새로운 accessToken으로 업데이트 } else { // TODO: refresh token 만료시 추가 처리 // refreshToken도 만료되었거나 문제가 있을 경우 @@ -125,10 +131,12 @@ export const authOptions: NextAuthOptions = { if (session.accessToken && isTokenExpired(session.accessToken)) { // 만료된 경우 refreshToken으로 새 accessToken을 발급받습니다. - const newAccessToken = await refreshAccessToken(session.refreshToken as string); + const newToken = await refreshAccessToken(token.accessToken as string); + console.log('newAccessToken -- Session', newToken); - if (newAccessToken) { - session.accessToken = newAccessToken; + if (newToken) { + token.accessToken = newToken.accessToken; // 새로운 accessToken으로 업데이트 + token.refreshToken = newToken.refreshToken; // 새로운 refreshToken 업데이트 } // TODO: refresh token 만료시 추가 처리 // refreshToken도 만료되었거나 문제가 있을 경우 diff --git a/src/utils/api/accounts.ts b/src/utils/api/accounts.ts index bd4ba95..e4ea62a 100644 --- a/src/utils/api/accounts.ts +++ b/src/utils/api/accounts.ts @@ -63,7 +63,7 @@ export const credentialsSignupAPI = async ({ export const refreshAccessToken = async (refreshToken: string) => { const result = await defaultInstance.post('members/refreshToken', { refreshToken }); - console.log('refresh', result.data.accessToken); + console.log('refresh', result.data); - return result.data.accessToken; + return result.data; }; From bd2744b27908ec6e9a897968fe20c5574d0e5b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=8C=EC=9A=B8=EC=B9=98=ED=82=A8?= <90738604+soulchicken@users.noreply.github.com> Date: Wed, 11 Oct 2023 13:50:19 +0900 Subject: [PATCH 2/4] =?UTF-8?q?Refactor:=20=EC=9E=84=EC=8B=9C=EB=A1=9C=20?= =?UTF-8?q?=EB=A7=8C=EB=93=A0=20test=20API=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/api/users/testapi.ts | 35 ---------------------------------- src/pages/profile/index.tsx | 15 --------------- 2 files changed, 50 deletions(-) delete mode 100644 src/pages/api/users/testapi.ts diff --git a/src/pages/api/users/testapi.ts b/src/pages/api/users/testapi.ts deleted file mode 100644 index 1bf0e69..0000000 --- a/src/pages/api/users/testapi.ts +++ /dev/null @@ -1,35 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from 'next'; - -interface NextApiRequestWithId extends NextApiRequest { - query: { - character_id: string; - }; -} - -type Data = { - friendShipExp: number, - maxFriendShipExp: number, - friendShipLv: number -} | { - error: string -} - -export default function handler( - req: NextApiRequestWithId, - res: NextApiResponse, -) { - // 클라이언트에서 보낸 accessToken을 받습니다. - const accessToken = req.headers.authorization?.split(' ')[1]; - - // accessToken이 없거나 유효하지 않은 경우 에러 응답을 반환합니다. - if (!accessToken) { - return res.status(401).json({ error: 'Invalid or missing access token.' }); - } - - // accessToken이 올바른 경우 요청을 처리합니다. - return res.status(200).json({ - friendShipExp: Math.random() * 200, - maxFriendShipExp: 200, - friendShipLv: 0, - }); -} diff --git a/src/pages/profile/index.tsx b/src/pages/profile/index.tsx index 0e6d29a..df266aa 100644 --- a/src/pages/profile/index.tsx +++ b/src/pages/profile/index.tsx @@ -2,26 +2,11 @@ import { css } from '@emotion/react'; import BottomNavBar from '@/components/common/bottomNavBar/BottomNavBar'; import SEO from '@/components/common/head/SEO'; import { useSession } from 'next-auth/react'; -import { useEffect } from 'react'; const Profile = () => { const { data: session }: any = useSession(); console.log(session); - useEffect(() => { - if (session?.accessToken) { - fetch('/api/users/testapi', { - headers: { - Authorization: `Bearer ${session.accessToken}`, - }, - }) - .then((res) => res.json()) - .then((data) => { - console.log(data); - }); - } - }, [session]); - return ( <> From d73f1683e401c71efc626e2efe02fb07a0cec752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=8C=EC=9A=B8=EC=B9=98=ED=82=A8?= <90738604+soulchicken@users.noreply.github.com> Date: Wed, 11 Oct 2023 13:56:07 +0900 Subject: [PATCH 3/4] =?UTF-8?q?Refactor:=20=EC=9E=84=EC=8B=9C=20=EC=B1=84?= =?UTF-8?q?=ED=8C=85=EB=82=B4=EC=97=AD=20API=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/api/chat/[character_id].tsx | 49 --------------------------- src/utils/api/accounts.ts | 2 +- 2 files changed, 1 insertion(+), 50 deletions(-) delete mode 100644 src/pages/api/chat/[character_id].tsx diff --git a/src/pages/api/chat/[character_id].tsx b/src/pages/api/chat/[character_id].tsx deleted file mode 100644 index 8f5bf87..0000000 --- a/src/pages/api/chat/[character_id].tsx +++ /dev/null @@ -1,49 +0,0 @@ -// Next.js API route support: https://nextjs.org/docs/api-routes/introduction -import type { NextApiRequest, NextApiResponse } from 'next'; - -interface NextApiRequestWithId extends NextApiRequest { - query: { - character_id: string; - }; -} - -type Chat = { - human: string, - timestamp: number, -} | { - bot: string, - timestamp: number, -} - -type Data = { say: string } | { history : Array} - -export default function handler( - req: NextApiRequestWithId, - res: NextApiResponse, -) { - const characterId = req.query.character_id; - - res.status(200).json({ history: history(characterId) }); -} - -const history = (characterId: string) => { - if (characterId === '0') { - return historyList[0]; - } - return historyList[1]; -}; - -const historyList = { - 0: [ - { human: '안녕', timestamp: 123123 }, - { bot: '반갑군. 난 이영준. 세상에서 가장 완벽한 남자', timestamp: 123124 }, - { human: '대단한걸?', timestamp: 123125 }, - { bot: '(자아도취에 빠진다)', timestamp: 123126 }, - ], - 1: [ - { human: '안녕', timestamp: 123123 }, - { bot: '안녕하세요. 저는 김미소입니다.', timestamp: 123124 }, - { human: '무슨 일을 해?', timestamp: 123125 }, - { bot: '조만간 퇴사할 생각이에요.', timestamp: 123126 }, - ], -}; diff --git a/src/utils/api/accounts.ts b/src/utils/api/accounts.ts index e4ea62a..7cfe55c 100644 --- a/src/utils/api/accounts.ts +++ b/src/utils/api/accounts.ts @@ -1,5 +1,5 @@ import defaultInstance from '@/utils/axiosInstance/defaultInstance'; -import clientInstance from '../axiosInstance/clientInstance'; +import clientInstance from '@/utils/axiosInstance/clientInstance'; interface Credentials { email: string, password: string, provider: string From 7f3939470d722e094de3394ac4d42287ce329d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=86=8C=EC=9A=B8=EC=B9=98=ED=82=A8?= <90738604+soulchicken@users.noreply.github.com> Date: Wed, 11 Oct 2023 14:51:02 +0900 Subject: [PATCH 4/4] =?UTF-8?q?Feat:=20next-session=20expire=20=EC=8B=9C?= =?UTF-8?q?=EA=B0=84=20=EC=A1=B0=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/api/auth/[...nextauth].ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/pages/api/auth/[...nextauth].ts b/src/pages/api/auth/[...nextauth].ts index 616a8b1..e5a18ef 100644 --- a/src/pages/api/auth/[...nextauth].ts +++ b/src/pages/api/auth/[...nextauth].ts @@ -67,6 +67,7 @@ export const authOptions: NextAuthOptions = { ], session: { strategy: 'jwt', + maxAge: 7 * 24 * 60 * 60, // 7 days }, jwt: { secret: process.env.NEXT_PUBLIC_SECRET_KEY,