-
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.
Browse files
Browse the repository at this point in the history
[feature/#3/Store] 전역 상태 관리 초기 설정
- Loading branch information
Showing
6 changed files
with
133 additions
and
30 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,19 @@ | ||
import { User } from "api-models" | ||
|
||
export const AUTH_USER_INITIAL_DATA: User = { | ||
id: "", | ||
nickname: "", | ||
provider: "", | ||
email: "", | ||
password: "", | ||
profile_image_url: "", | ||
introduction: "", | ||
job: "", | ||
career: "", | ||
github_url: "", | ||
blog_url: "", | ||
created_at: "", | ||
updated_at: "", | ||
deleted_at: "", | ||
is_deleted: false, | ||
} |
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,5 +1,5 @@ | ||
const TestPage = () => { | ||
return <div>TestPage</div> | ||
return <div>Testpage</div> | ||
} | ||
|
||
export default TestPage |
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,41 @@ | ||
const { VITE_AUTH_JWT_TOKEN_STORAGE_KEY } = import.meta.env | ||
class AuthToken { | ||
private token: string | ||
private KEY: string | ||
|
||
constructor() { | ||
this.token = "" | ||
this.KEY = VITE_AUTH_JWT_TOKEN_STORAGE_KEY | ||
} | ||
|
||
setToken(newToken: string) { | ||
try { | ||
const stringifiedData = JSON.stringify(newToken) | ||
localStorage.setItem(this.KEY, stringifiedData) | ||
this.token = newToken | ||
} catch (e) { | ||
this.token = "" | ||
} | ||
} | ||
|
||
getToken() { | ||
try { | ||
const res = localStorage.getItem(this.KEY) | ||
if (!res) { | ||
return this.token | ||
} | ||
this.token = JSON.parse(res) | ||
return this.token | ||
} catch (e) { | ||
return this.token | ||
} | ||
} | ||
|
||
removeToken() { | ||
localStorage.removeItem(this.KEY) | ||
} | ||
} | ||
|
||
const authToken = new AuthToken() | ||
|
||
export default authToken |
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,39 @@ | ||
import { User } from "api-models" | ||
import { create } from "zustand" | ||
|
||
import { AUTH_USER_INITIAL_DATA } from "@/constants/user" | ||
|
||
import authToken from "./authToken" | ||
|
||
interface AuthUserStore { | ||
user: User | ||
setLogin: (user: User, token: string) => void | ||
setLogout: () => void | ||
updateUser: (user: User) => void | ||
} | ||
|
||
const useAuthStore = create<AuthUserStore>()((set) => ({ | ||
user: AUTH_USER_INITIAL_DATA, | ||
|
||
setLogin: (user, token) => { | ||
authToken.setToken(token) | ||
set(() => ({ | ||
user: user, | ||
})) | ||
}, | ||
|
||
setLogout: () => { | ||
authToken.removeToken() | ||
set(() => ({ | ||
user: AUTH_USER_INITIAL_DATA, | ||
})) | ||
}, | ||
|
||
updateUser: (user: User) => { | ||
set(() => ({ | ||
user: user, | ||
})) | ||
}, | ||
})) | ||
|
||
export default useAuthStore |
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 +1,5 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
interface ImportMetaEnv { | ||
readonly VITE_AUTH_JWT_TOKEN_STORAGE_KEY: string | ||
} |