-
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.
feat : Recoil을 활용하여 전역에서 사용자의 로그인 유무, 닉네임의 정보를 상태를 관리할 수 있게끔 로직 수정
- Loading branch information
Showing
3 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
frontend/src/app/provider/RouterProvider/RouterProvider.tsx
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,38 @@ | ||
import { RouterProvider, createRouter } from "@tanstack/react-router"; | ||
import { routeTree } from "@/routeTree.gen"; | ||
import { Auth, AuthState } from "./lib/auth"; | ||
import { GlobalErrorComponent } from "@/shared/components/Error/GlobalError"; | ||
import { QueryClient } from "@tanstack/react-query"; | ||
import { RecoilState } from "recoil"; | ||
|
||
type RouterContext = { | ||
auth: RecoilState<AuthState>; | ||
queryClient: QueryClient; | ||
}; | ||
|
||
const createGlobalRouter = (queryClient: QueryClient) => | ||
createRouter({ | ||
routeTree, | ||
context: { | ||
auth: Auth, | ||
queryClient, | ||
} as RouterContext, | ||
defaultPreload: "intent", | ||
defaultErrorComponent: ({ error }) => ( | ||
<GlobalErrorComponent error={error} to={"/"} /> | ||
), | ||
}); | ||
|
||
function GlobalRouter({ queryClient }: { queryClient: QueryClient }) { | ||
const router = createGlobalRouter(queryClient); | ||
return <RouterProvider router={router} />; | ||
} | ||
|
||
declare module "@tanstack/react-router" { | ||
interface Register { | ||
router: ReturnType<typeof createGlobalRouter>; | ||
} | ||
} | ||
|
||
export { GlobalRouter }; | ||
export type { RouterContext }; |
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,2 @@ | ||
export { GlobalRouter } from "./RouterProvider"; | ||
export type { RouterContext } from "./RouterProvider"; |
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,16 @@ | ||
import { atom } from "recoil"; | ||
|
||
export type AuthState = { | ||
isAuthenticated: boolean; | ||
nickname?: string; | ||
roomId?: string; | ||
}; | ||
|
||
export const Auth = atom<AuthState>({ | ||
key: "auth", | ||
default: { | ||
isAuthenticated: false, | ||
nickname: undefined, | ||
roomId: undefined, | ||
}, | ||
}); |