generated from ooooorobo/react-mobile-web-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from wanted-fork-fork/feature/40-add-with-auth…
…-hoc [Feature] 인증 여부 확인을 위한 withAuthenticated HOC 추가
- Loading branch information
Showing
7 changed files
with
59 additions
and
16 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
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,9 @@ | ||
function SafeHydrateHoc({ children }) { | ||
return ( | ||
<div suppressHydrationWarning> | ||
{typeof window === "undefined" ? null : children} | ||
</div> | ||
); | ||
} | ||
|
||
export default SafeHydrateHoc; |
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 { useStores } from "@src/store/root.store"; | ||
import { useEffect, useMemo } from "react"; | ||
import GuestMainTemplate from "@src/templates/GuestMain.template"; | ||
import { useRouter } from "next/router"; | ||
|
||
const allowedOnlyToGuest = ["/", "/login", "/signup"]; | ||
|
||
function WithAuthenticationHoc({ children }) { | ||
const { userStore } = useStores(); | ||
const router = useRouter(); | ||
|
||
useEffect(() => userStore.isAuthenticated()); | ||
|
||
const component = useMemo(() => { | ||
const unauthenticated = !userStore.authenticated; | ||
const isAllowedForGuest = allowedOnlyToGuest.find( | ||
(x) => x === router.pathname, | ||
); | ||
if (isAllowedForGuest) { | ||
if (unauthenticated) return children; | ||
|
||
router.push("/"); | ||
return children; | ||
} | ||
return unauthenticated ? <GuestMainTemplate /> : children; | ||
}, [userStore.authenticated, children, router]); | ||
|
||
return component; | ||
} | ||
|
||
export default WithAuthenticationHoc; |
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 |
---|---|---|
@@ -1,15 +1,7 @@ | ||
import { useStores } from "@src/store/root.store"; | ||
import { useMemo } from "react"; | ||
import GuestMainTemplate from "@src/templates/GuestMain.template"; | ||
import UserMainPage from "@src/pages/main"; | ||
|
||
function MainPage() { | ||
const { userStore } = useStores(); | ||
|
||
// TODO :: authenticated 확인을 HOC로 빼내기 | ||
const authenticated = useMemo(() => userStore.isAuthenticated(), [userStore]); | ||
|
||
return authenticated ? <UserMainPage /> : <GuestMainTemplate />; | ||
return <UserMainPage />; | ||
} | ||
|
||
export default MainPage; |
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