Skip to content

Commit

Permalink
Merge pull request #187 from depromeet/bugfix/middleware
Browse files Browse the repository at this point in the history
middleware 수정
  • Loading branch information
YOOJS1205 authored Jan 30, 2024
2 parents 185f325 + 7a8b0c2 commit 195f3c4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
19 changes: 10 additions & 9 deletions src/api/api-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,17 @@ axiosInstance.interceptors.response.use(
config.sent = true;
const refreshToken = Cookies.get('refreshToken');

// NOTE: 401 에러인데, refreshToken이 존재하지 않으면 로그아웃
// NOTE: 토큰 재발급 요청
if (
config.url !== TOKEN_REFRESH_URL &&
response.data.code === 401 &&
refreshToken
) {
const { data } = await getTokenRefresh();
Cookies.set('accessToken', data.accessToken);
config.headers['Authorization'] = data.accessToken;
return axios(config);
if (config.url !== TOKEN_REFRESH_URL && response.data.code === 401) {
if (refreshToken) {
const { data } = await getTokenRefresh();
Cookies.set('accessToken', data.accessToken);
config.headers['Authorization'] = data.accessToken;
return axios(config);
} else {
logout();
}
}

// NOTE: 토큰 재발급 요청이 유효하지 않으면, 쿠키의 토큰을 삭제하고 로그아웃 처리. 로그인 페이지로 이동
Expand Down
9 changes: 8 additions & 1 deletion src/app/mypage/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ interface LayoutProps {
export default function Layout({ children }: LayoutProps) {
const router = useRouter();

const handleClickBackButton = () => {
router.push('/');
};

const handleClickSettingButton = () => router.push('/settings');

return (
<main className='bg-[url("/assets/image/mypage/background.png")] bg-cover w-full max-w-[480px] h-[100dvh]'>
<Header className=" [&>*>*]:fill-white justify-between">
<Header
onClick={handleClickBackButton}
className=" [&>*>*]:fill-white justify-between"
>
<SettingIcon onClick={handleClickSettingButton} />
</Header>
{children}
Expand Down
2 changes: 1 addition & 1 deletion src/app/review/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default function Page() {

return (
<div className="bg-gray-100">
<Header className="w-full bg-gray-100">
<Header className="w-full bg-gray-100 z-header">
<p className="body-16-bold">로그 작성</p>
</Header>
<div className="h-[100dvh] pt-[56px] pb-[104px] overflow-y-scroll px-[16px]">
Expand Down
13 changes: 11 additions & 2 deletions src/components/common/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,24 @@ import { HTMLAttributes } from 'react';
import cn from '@utils/cn';
import LeftArrow from 'public/assets/icon24/left_arrow_24.svg';

interface HeaderProps extends HTMLAttributes<HTMLButtonElement> {
onClick?: () => void;
}

export default function Header({
className,
children,
onClick,
...restProps
}: HTMLAttributes<HTMLHeadElement>) {
}: HeaderProps) {
const { back } = useRouter();

const handleClickBackButton = () => {
back();
if (onClick) {
onClick();
} else {
back();
}
};

return (
Expand Down
26 changes: 10 additions & 16 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,29 @@ export function middleware(request: NextRequest) {
const isFirstLogin = request.nextUrl.searchParams.get('isFirst') as string;
const isFromApp = request.nextUrl.searchParams.get('fromApp');

const url = request.nextUrl.clone();

// NOTE: splash 화면으로 넘어왔을 때 쿠키 내의 토큰 여부에 따른 리다이렉트 로직
if (isFromApp === 'true') {
const url = request.nextUrl.clone();

console.log(accessToken, refreshToken);
if (accessToken && refreshToken) {
url.pathname = '/';
} else {
url.pathname = '/login';
}

url.search = '';

const response = NextResponse.redirect(url);

return response;
}

// 웹
const url = request.nextUrl.clone();

if (isFirstLogin === 'true') {
url.pathname = '/terms';
} else {
url.pathname = '/';
// 웹
if (isFirstLogin === 'True') {
url.pathname = '/terms';
} else {
url.pathname = '/';
}
}

url.search = '';

const response = NextResponse.redirect(url);

if (accessToken && refreshToken) {
response.cookies.set('accessToken', accessToken, { path: '/' });
response.cookies.set('refreshToken', refreshToken, { path: '/' });
Expand Down

0 comments on commit 195f3c4

Please sign in to comment.