-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: middleware 오동작으로 생긴 배포 이슈 수정 및 리팩토링 (2차) (#39)
* add NextResponse.next() * remove middleware for deploy test * add middleware with runtime config * change to edge not experimental * remove next() * add edge runtime on next config * version up nextjs for deploy test * remove experimental runtime * Revert "version up nextjs for deploy test" This reverts commit 13d5ce9. * remove middleware for fixing deploy * move redirection logic to app comp and refactor * replace getStaticProps to needProtected attribute * set isLoading default value to true * change name for readability
- Loading branch information
Showing
8 changed files
with
45 additions
and
47 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { useUser } from '@/store/useUser'; | ||
import { useRouter } from 'next/router'; | ||
import { useEffect } from 'react'; | ||
|
||
export const useProtectedRoute = (isProtectedPage = false, redirectUrl = '/') => { | ||
const router = useRouter(); | ||
const { user, isLoading, login } = useUser(); | ||
|
||
// @note: | ||
// 1. 로그인 전에 로딩 페이지를 보여주는 이유는 | ||
// 로그인 되기 전에 supabase를 통해 api 호출하면 signIn이 되어있지 않아 아무값을 못 얻게되기 때문이다. | ||
// 2. protected 페이지는 유저 정보가 없을떄 지속적으로 로딩 페이지를 노출하는 이유는 | ||
// redirectUrl로 이동하기 때문이다. | ||
const showLoadingPage = isProtectedPage ? isLoading || !user : isLoading; | ||
|
||
useEffect(() => { | ||
// @note: | ||
// getServerSideProps에서 edge function을 호출해도 그 응답에 접근할 수 없기에 | ||
// 로그인 여부를 client side에서 최초 mount된 시점에 체크합니다. | ||
// https://nextjs.org/docs/api-routes/edge-api-routes#differences-between-api-routes | ||
login().catch(() => null); | ||
}, [login]); | ||
|
||
useEffect(() => { | ||
if (isProtectedPage && !isLoading && !user) { | ||
router.replace(redirectUrl); | ||
} | ||
}, [isLoading, isProtectedPage, redirectUrl, router, user]); | ||
|
||
return { showLoadingPage }; | ||
}; |
This file was deleted.
Oops, something went wrong.
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
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
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 |
---|---|---|
|
@@ -34,4 +34,6 @@ MyPage.getLayout = (page) => ( | |
</MainLayout> | ||
); | ||
|
||
MyPage.isProtectedPage = true; | ||
|
||
export default MyPage; |
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