diff --git a/src/pages/api/auth/[...nextauth].ts b/src/pages/api/auth/[...nextauth].ts index e818a66..e5a18ef 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, @@ -65,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, @@ -106,9 +109,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 +132,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/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/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 ( <> diff --git a/src/utils/api/accounts.ts b/src/utils/api/accounts.ts index bd4ba95..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 @@ -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; };