From a7c71cc92e4009e4d2f502246d07ccceb7ca10d2 Mon Sep 17 00:00:00 2001 From: walt-it Date: Tue, 15 Oct 2024 18:49:23 -0300 Subject: [PATCH] feat: restore namespace --- src/api/interceptors/authHeader.ts | 4 ++-- src/api/interceptors/refreshToken.ts | 8 ++++---- src/domains/guest/api/login.ts | 4 ++-- .../components/RouterWrapper/RouterWrapper.tsx | 4 ++-- src/stores/index.ts | 2 +- src/stores/useAuthStore.ts | 6 +++++- src/stores/useExampleStore.ts | 18 +++++++++++------- 7 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/api/interceptors/authHeader.ts b/src/api/interceptors/authHeader.ts index 7ac8a21..afea244 100644 --- a/src/api/interceptors/authHeader.ts +++ b/src/api/interceptors/authHeader.ts @@ -1,9 +1,9 @@ import type { InternalAxiosRequestConfig } from "axios"; -import { useAuthStore } from "~/stores"; +import { authStore } from "~/stores"; export const authHeaderInterceptor = (config: InternalAxiosRequestConfig) => { - const { token } = useAuthStore.getState(); + const { token } = authStore.getState(); config.headers.Authorization = token ? `Bearer ${token}` : undefined; diff --git a/src/api/interceptors/refreshToken.ts b/src/api/interceptors/refreshToken.ts index a6f9cc7..ecb7ac8 100644 --- a/src/api/interceptors/refreshToken.ts +++ b/src/api/interceptors/refreshToken.ts @@ -1,6 +1,6 @@ import mem from "mem"; -import { setToken } from "~/stores"; +import { authStore } from "~/stores"; import type { ServiceResponse } from "../api.types"; import { api } from "../axios"; @@ -22,13 +22,13 @@ const refreshToken = async () => { ); const { data: userToken } = response.data; if (!userToken.refresh_token) { - setToken(null); + authStore.setToken(null); } else { refreshWasSuccessful = true; - setToken(userToken.refresh_token); + authStore.setToken(userToken.refresh_token); } } catch (error) { - setToken(null); + authStore.setToken(null); } return refreshWasSuccessful; }; diff --git a/src/domains/guest/api/login.ts b/src/domains/guest/api/login.ts index b266288..021c991 100644 --- a/src/domains/guest/api/login.ts +++ b/src/domains/guest/api/login.ts @@ -1,6 +1,6 @@ import { api } from "~/api"; import type { ServiceResponse } from "~/api"; -import { setToken } from "~/stores"; +import { authStore } from "~/stores"; export type UserToken = { accessToken: string; @@ -27,5 +27,5 @@ export const googleLogin = async ({ }; export const logout = () => { - setToken(null); + authStore.setToken(null); }; diff --git a/src/router/components/RouterWrapper/RouterWrapper.tsx b/src/router/components/RouterWrapper/RouterWrapper.tsx index 0f63535..ff2a166 100644 --- a/src/router/components/RouterWrapper/RouterWrapper.tsx +++ b/src/router/components/RouterWrapper/RouterWrapper.tsx @@ -3,14 +3,14 @@ import React from "react"; import { Navigate, Route, Routes } from "react-router-dom"; import { MainLayout } from "~/router"; -import { useAuthStore } from "~/stores"; +import { authStore } from "~/stores"; type Props = PropsWithChildren & { guest?: boolean; }; export const RouterWrapper = ({ guest = false, children }: Props) => { - const isAuthenticated = useAuthStore((state) => state.token); + const isAuthenticated = authStore.getToken(); if (!guest && !isAuthenticated) { return ; diff --git a/src/stores/index.ts b/src/stores/index.ts index fd73963..61bbc60 100644 --- a/src/stores/index.ts +++ b/src/stores/index.ts @@ -1,2 +1,2 @@ -export * from "./useAuthStore"; +export * as authStore from "./useAuthStore"; export * as exampleStore from "./useExampleStore"; diff --git a/src/stores/useAuthStore.ts b/src/stores/useAuthStore.ts index b0fe71a..bae7983 100644 --- a/src/stores/useAuthStore.ts +++ b/src/stores/useAuthStore.ts @@ -12,7 +12,7 @@ export type AuthStoreState = { token: string | null; }; -export const useAuthStore = create()( +const useAuthStore = create()( persist( (_) => ({ token: null, @@ -23,5 +23,9 @@ export const useAuthStore = create()( ), ); +export const getState = () => useAuthStore.getState(); + +export const getToken = () => useAuthStore((state) => state.token); + export const setToken = (token: string | null) => useAuthStore.setState(() => ({ token })); diff --git a/src/stores/useExampleStore.ts b/src/stores/useExampleStore.ts index 3cb9003..52ce50b 100644 --- a/src/stores/useExampleStore.ts +++ b/src/stores/useExampleStore.ts @@ -14,17 +14,21 @@ export type ExampleStoreState = { thirdValue: number | null; }; -export const useStore = create()(() => ({ +const useStore = create(() => ({ firstValue: null, secondValue: null, thirdValue: null, })); -export const setFirstValue = () => - useStore.setState((state) => ({ firstValue: state.firstValue })); +export const getFirstValue = () => useStore((state) => state.firstValue); +export const getSecondValue = () => useStore((state) => state.secondValue); +export const getThirdValue = () => useStore((state) => state.thirdValue); -export const setSecondValue = () => - useStore.setState((state) => ({ secondValue: state.secondValue })); +export const setFirstValue = (firstValue: ExampleStoreState["firstValue"]) => + useStore.setState(() => ({ firstValue })); -export const setThirdValue = () => - useStore.setState((state) => ({ thirdValue: state.thirdValue })); +export const setSecondValue = (secondValue: ExampleStoreState["secondValue"]) => + useStore.setState(() => ({ secondValue })); + +export const setThirdValue = (thirdValue: ExampleStoreState["thirdValue"]) => + useStore.setState(() => ({ thirdValue }));