Skip to content

Commit

Permalink
[feat] app router 전환에 따른 세션토큰 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Im-younique committed Jan 16, 2024
1 parent d9fd872 commit f43748c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
76 changes: 76 additions & 0 deletions client/src/app/api/token/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// next, react
import { type NextRequest } from 'next/server';

// services
import { axios_get } from '@/service/base/api';

export const runtime = 'nodejs';

export async function GET(req: NextRequest) {
try {
const token = req.cookies.get('token');
if (token) {
const suburl = '/auth/token';
const headers = {
Authorization: `Bearer ${token.value}`,
};
try {
const result = await axios_get({ suburl, headers });
const { data } = result;
const { accessToken } = data as { accessToken: string };

return Response.json({
message: '토큰발급 성공',
data: { accessToken },
});
} catch {
return new Response('토큰이 만료됐습니다.', {
status: 401,
headers: {
'Set-Cookie': `token=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure`,
},
});
}
} else {
return Response.json({ accessToken: null });
}
} catch (error) {
return new Response('토큰이 만료됐습니다.', {
status: 500,
headers: {
'Set-Cookie': `token=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure`,
},
});
}
}

export async function POST(req: NextRequest) {
try {
const data = await req.json();
const { refreshToken }: { refreshToken: string } = data;

return new Response('쿠키설정 성공', {
status: 200,
headers: {
'Set-Cookie': `token=${refreshToken}; Path=/; HttpOnly; Secure`,
},
});
} catch (error) {
return new Response('쿠키설정 실패', { status: 500 });
}
}

export async function DELETE() {
try {
return new Response('토큰삭제 성공', {
status: 200,
headers: {
'Set-Cookie': `token=; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT; HttpOnly; Secure`,
},
});
} catch (error) {
return new Response('토큰삭제 실패', {
status: 500,
});
}
}
2 changes: 1 addition & 1 deletion client/src/components/AuthComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function AuthComponent({
};

const getAccessToken = async () => {
const response = await fetch('/api/getToken', { method: 'GET' });
const response = await fetch('/api/token', { method: 'GET' });
const { message, data } = await response.json();
if (!response.ok) {
dispatch(logOut());
Expand Down
2 changes: 1 addition & 1 deletion client/src/service/user/logOutUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import axios from 'axios';

export default async function logoutUser() {
try {
await fetch('/api/deleteToken', {
await fetch('/api/token', {
method: 'DELETE',
});
} catch {
Expand Down
2 changes: 1 addition & 1 deletion client/src/service/user/loginUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default async function loginUser({ data }: PropType) {

// 쿠키 설정
try {
await fetch('/api/setToken', {
await fetch('/api/token', {
method: 'POST',
body: JSON.stringify({ refreshToken }),
});
Expand Down

0 comments on commit f43748c

Please sign in to comment.