Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

middleware 수정 #187

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading