-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feat/#228-suspense
- Loading branch information
Showing
14 changed files
with
105 additions
and
85 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
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
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,44 +1,34 @@ | ||
import axios from "axios"; | ||
|
||
const real = axios.create({ | ||
export const axiosInstance = axios.create({ | ||
baseURL: import.meta.env.VITE_BASE_URL, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
// withCredentials: true, | ||
}); | ||
|
||
real.interceptors.request.use((config) => { | ||
if (localStorage.getItem("piickle-token") === null) return { ...config }; | ||
|
||
const headers = { | ||
...config.headers, | ||
"x-auth-token": `Bearer ${localStorage.getItem("piickle-token")}`, | ||
}; | ||
return { ...config, headers }; | ||
}); | ||
|
||
export const realReq = { | ||
GET_SWR(path: string) { | ||
return real.get(path); | ||
return axiosInstance.get(path); | ||
}, | ||
|
||
async GET<T>(path: string, option?: { params: string }) { | ||
const data = await real.get<T>(path, option); | ||
const data = await axiosInstance.get<T>(path, option); | ||
return data.data; | ||
}, | ||
|
||
async POST<T>(path: string, body: T) { | ||
const data = await real.post(path, body); | ||
const data = await axiosInstance.post(path, body); | ||
return data.data; | ||
}, | ||
|
||
async PUT<T>(path: string, body: T) { | ||
const data = await real.put(path, body); | ||
const data = await axiosInstance.put(path, body); | ||
return data.data; | ||
}, | ||
|
||
async PATCH<T>(path: string, body: T) { | ||
await real.patch(path, body); | ||
await axiosInstance.patch(path, body); | ||
}, | ||
}; |
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 |
---|---|---|
|
@@ -13,3 +13,5 @@ export const PATH = { | |
NOTICES: "/notices", | ||
MEDLEY: "/medley", | ||
}; | ||
|
||
export const USER_TOKEN = "piickle-token"; |
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,27 @@ | ||
import { atom } from "recoil"; | ||
|
||
import { axiosInstance } from "../api/common/axios"; | ||
import { USER_TOKEN } from "../api/common/constants"; | ||
|
||
enum StateType { | ||
USER_TOKEN_ATOM = "usertokenatom", | ||
} | ||
|
||
export const userTokenAtom = atom<string | null>({ | ||
key: StateType.USER_TOKEN_ATOM, | ||
default: null, | ||
effects: [ | ||
({ onSet }) => { | ||
onSet((newToken, _, isReset) => { | ||
if (isReset || newToken === null) { | ||
localStorage.removeItem(USER_TOKEN); | ||
axiosInstance.defaults.headers.common["x-auth-token"] = false; | ||
return; | ||
} | ||
|
||
localStorage.setItem(USER_TOKEN, newToken); | ||
axiosInstance.defaults.headers.common["x-auth-token"] = `Bearer ${newToken}`; | ||
}); | ||
}, | ||
], | ||
}); |
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,18 @@ | ||
import { useRecoilState, useResetRecoilState } from "recoil"; | ||
|
||
import { userTokenAtom } from "../atom/auth"; | ||
|
||
export default function useAuth() { | ||
const resetUserToken = useResetRecoilState(userTokenAtom); | ||
const [userToken, setUserToken] = useRecoilState(userTokenAtom); | ||
|
||
return { | ||
isLogin: !!userToken, | ||
login: (token: string) => { | ||
setUserToken(token); | ||
}, | ||
logout: () => { | ||
resetUserToken(); | ||
}, | ||
}; | ||
} |
Oops, something went wrong.