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

Feat: 로그인 시 토큰 저장 및 middleware 설정 #40

Merged
merged 7 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
display: flex;
flex-direction: column;
gap: 17px;
padding-top: 72px;
padding-top: calc(54px + 72px);
h2 {
font-size: 24px;
span {
Expand Down
48 changes: 48 additions & 0 deletions middleware.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주석 덕분에 코드가 잘읽히네요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { NextResponse } from 'next/server';
import { NextRequest } from 'next/server';

const PROTECTED_PAGES = ['/start-family'];
const PUBLIC_PAGES = ['/login'];

export default function middleware(request: NextRequest) {
const { cookies, nextUrl } = request;
const path = nextUrl.pathname;
const isProtectedPage = PROTECTED_PAGES.includes(path);
const isPublicPage = PUBLIC_PAGES.includes(path);

// 로그인 상태 확인
const hasCookie = cookies.has('accessToken');

// 쿼리 파라미터에서 token 값 확인
const hasQuery = nextUrl.search.includes('token');

// /start-family 페이지로 접근하는데 token이 있을 경우 : 페이지 접근
// /start-family 페이지로 접근하는데 token이 없을 경우 : 리다이렉션
// /start-family 페이지로 접근하는데 token이 없지만 쿠키가 있을 경우 : 페이지 접근
// 추후 배포됐을 때 수정.
if (path === '/start-family' && hasQuery) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분은 고민을 더 해보는게 좋아보이네요. 이렇게 if문이 많이 생기면 가독성이 확 떨어집니다.
객체 맵핑을 사용해도 좋고, 아니면 더 줄일 수 있는 방법을 고민해봐야할듯 해요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 수정하겠습니다!

return NextResponse.next();
} else if (path === '/start-family' && !hasQuery) {
if (hasCookie) {
return NextResponse.next();
} else {
return NextResponse.redirect(new URL('/login', request.nextUrl));
}
}

// 비로그인 상태일 때 로그인이 필요한 페이지에 접근 시 리다이렉션
if (!hasCookie && isProtectedPage) {
return NextResponse.redirect(new URL('/login', request.nextUrl));
}

// 로그인 상태일 때 로그인 페이지에 접근 시 리다이렉션
if (hasCookie && isPublicPage) {
return NextResponse.redirect(new URL('/', request.nextUrl.origin));
}

return NextResponse.next();
}

export const config = {
matcher: ['/login', '/start-family'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

matcher: [ ...PROTECTED_PAGES, ...PUBLIC_PAGES ]
여길 이렇게 바꿀 수 있을까 생각해봤습니다

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오... 어떻게 넣을까 고민했었는데 스프레드가 있었군요
감사합니다 변경하겠습니다!

};
15 changes: 15 additions & 0 deletions pages/start-family/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import LinkList from '@/components/start-family/link-list/link-list';
import IntroduceText from '@/components/start-family/introduce-text/introduce-text';
import styles from './index.module.scss';
import { useEffect } from 'react';
import { useRouter } from 'next/router';
import { setCookie } from 'cookies-next';

export default function StartFamily() {
const {
query: { token },
isReady,
} = useRouter();

useEffect(() => {
if (!isReady) return;
if (token) {
setCookie('accessToken', token);
}
}, [isReady]);

return (
<article className={styles.article}>
<IntroduceText />
Expand Down
Loading