-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP: auth * feat: zustand store 적용 (#54) --------- Co-authored-by: David Kim <[email protected]>
- Loading branch information
1 parent
97a6283
commit 3e0d195
Showing
5 changed files
with
65 additions
and
40 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
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,20 @@ | ||
import { useEffect } from 'react'; | ||
import authStore from '@/stores/auth'; | ||
import { useNavigate, Outlet } from 'react-router-dom'; | ||
|
||
export default function Index() { | ||
const { isAuth } = authStore(); | ||
const nav = useNavigate(); | ||
|
||
useEffect(() => { | ||
if (!isAuth) { | ||
nav('/', { replace: true }); | ||
} | ||
}, [isAuth]); | ||
|
||
return ( | ||
<main> | ||
<Outlet /> | ||
</main> | ||
); | ||
} |
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,24 @@ | ||
import { create } from 'zustand'; | ||
|
||
type Auth = { | ||
isAuth: boolean | ||
login: () => void | ||
logout: () => void | ||
} | ||
|
||
const authStore = create<Auth>((set) => ({ | ||
isAuth: localStorage.getItem('isAuth') === 'true' || false, | ||
|
||
login: () => set(() => { | ||
localStorage.setItem('isAuth', 'true'); | ||
return { isAuth: true }; | ||
}), | ||
|
||
logout: () => set(() => { | ||
localStorage.removeItem('isAuth'); | ||
return { isAuth: false }; | ||
}), | ||
|
||
})); | ||
|
||
export default authStore; |