Skip to content

Commit

Permalink
Fix: 로그아웃 기능 및 기타 파일명 등 수정 (#86)
Browse files Browse the repository at this point in the history
* Fix: logout 기능 및 기타 변수명 등 수정

* Fix: rebase 후, 파일 명 수정 반영
  • Loading branch information
minchodang authored Sep 28, 2024
1 parent de2ae2f commit 0b81efe
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import { revalidatePath } from 'next/cache';

export async function revalidateHomeVerify() {
export async function renewAllCache() {
return revalidatePath('/');
}
28 changes: 7 additions & 21 deletions src/app/api/auth/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,6 @@ export async function PUT(request: NextRequest) {

const newAccessToken = res.headers.access_token;
const newRefreshToken = res.headers.refresh_token;
// // 새로운 Refresh Token을 HttpOnly 쿠키로 설정
// cookies().set('rtk', newRefreshToken, {
// httpOnly: true,
// secure: process.env.NODE_ENV === 'production',
// maxAge: 60 * 60 * 24 * 14, // 2주
// path: '/',
// sameSite: 'strict',
// });
// cookies().set('atk', newAccessToken, {
// httpOnly: true,
// secure: process.env.NODE_ENV === 'production',
// maxAge: 60 * 30, // 30분
// path: '/',
// sameSite: 'strict',
// });

// 새로운 Access Token을 응답 헤더로 클라이언트에 전달
const response = NextResponse.json({ success: true });
response.headers.set('atk', newAccessToken);
Expand All @@ -94,17 +78,19 @@ export async function PUT(request: NextRequest) {
export async function DELETE(request: NextRequest) {
const refreshToken = cookies().get('rtk')?.value;
const accessToken = cookies().get('atk')?.value;
console.log(accessToken, '어 뜨잖아?');

if (!refreshToken) {
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 });
}

try {
await axios.delete(`${process.env.NEXT_PUBLIC_META_TEST_SERVER_HOST_URL}/auth/token`, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
//TODO 현재 백엔드 로그아웃 기능 미구축으로 서버 통신은 잠시 보류, 추후 재구축 예정.
// await axios.delete(`${process.env.NEXT_PUBLIC_META_TEST_SERVER_HOST_URL}/auth/token`, {
// headers: {
// Authorization: `Bearer ${accessToken}`,
// },
// });

cookies().delete('rtk');
cookies().delete('atk');
Expand Down
4 changes: 2 additions & 2 deletions src/app/auth/components/AuthPageClient.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { revalidateHomeVerify } from '@src/app/actions/revalidateHomeVerify';
import { renewAllCache } from '@src/app/actions/renewAllCache';
import { ToastService } from '@src/service/ToastService';
import axios from 'axios';
import { useRouter } from 'next/navigation';
Expand Down Expand Up @@ -64,7 +64,7 @@ const AuthPageClient = ({ code }: AuthPageClientProps) => {

if (data) {
toastService.addToast('로그인 되었습니다.');
revalidateHomeVerify();
renewAllCache();
router.replace(loginPath ?? '/');
} else {
toastService.addToast('로그인에 실패하였습니다.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ import { ToastService } from '@src/service/ToastService';
import { useQueryClient } from '@tanstack/react-query';
import { useRouter } from 'next/navigation';
import { useCookies } from 'react-cookie';
import axios from 'axios';
import { renewAllCache } from '@src/app/actions/renewAllCache';

interface MyPageHeaderSectionProps {
isLogin: boolean;
}

const MyPageHeaderSection: FC<MyPageHeaderSectionProps> = ({ isLogin }) => {
const [, , removeCookies] = useCookies(['refreshToken']);
const queryClient = useQueryClient();
const toastService = ToastService.getInstance();
const { replace } = useRouter();
const onClickHome = () => {
replace('/');
};
const onClickLogout = async () => {
await removeCookies('refreshToken');
await axios.delete(`${process.env.NEXT_PUBLIC_META_TEST_WEB_HOST_URL}/api/auth`);
await renewAllCache();
await queryClient.removeQueries({
queryKey: [API_GET_USER_PROFILE],
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/server/auth/checkToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface LoginState {
const reissueToken = async (): Promise<LoginState> => {
try {
const { headers } = await axios.put(
`${process.env.NEXT_PUBLIC_MATE_TEST_WEB_HOST_URL}/api/auth`,
`${process.env.NEXT_PUBLIC_META_TEST_WEB_HOST_URL}/api/auth`,
{
headers: {
refreshToken: cookies().get('rtk')?.value,
Expand Down
5 changes: 1 addition & 4 deletions src/provider/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// AuthProvider.tsx (클라이언트 컴포넌트)
'use client';

import { FC, useEffect, useState } from 'react';
import { AuthService } from '@src/service/AuthService';
import { cookies } from 'next/headers';
import { setAuthCookie } from '@src/app/actions/setAuthCookie';
import defaultRequest from '@src/lib/axios/defaultRequest';
import { FC, useEffect } from 'react';

interface IAuthProviderProps {
accessToken?: string;
Expand Down

0 comments on commit 0b81efe

Please sign in to comment.