-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a099d44
[#24] Feat: 로그인 성공시 token을 쿠키에 저장
LDH98 4509756
[#39] Feat: middleware 설정
LDH98 da17a8c
[#39] Feat: middleware 조건 추가
LDH98 443e7ba
[#24] fix: 텍스트 스타일 수정
LDH98 7cf4bc8
[#39] Fix: 코드 가독성 완화
LDH98 fb89bec
[#39] Fix: matcher 값 상수로 변경
LDH98 ee7902e
Merge branch 'develop' of https://github.com/Donggrina/Frontend into …
LDH98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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') { | ||
if (hasQuery || 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: [...PUBLIC_PAGES, ...PROTECTED_PAGES], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
주석 덕분에 코드가 잘읽히네요!