Skip to content

Commit

Permalink
feat: restore namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
walt-it committed Oct 15, 2024
1 parent 85fdd29 commit a7c71cc
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/api/interceptors/authHeader.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
8 changes: 4 additions & 4 deletions src/api/interceptors/refreshToken.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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;
};
Expand Down
4 changes: 2 additions & 2 deletions src/domains/guest/api/login.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,5 +27,5 @@ export const googleLogin = async ({
};

export const logout = () => {
setToken(null);
authStore.setToken(null);
};
4 changes: 2 additions & 2 deletions src/router/components/RouterWrapper/RouterWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Navigate to='/404' />;
Expand Down
2 changes: 1 addition & 1 deletion src/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./useAuthStore";
export * as authStore from "./useAuthStore";
export * as exampleStore from "./useExampleStore";
6 changes: 5 additions & 1 deletion src/stores/useAuthStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type AuthStoreState = {
token: string | null;
};

export const useAuthStore = create<AuthStoreState>()(
const useAuthStore = create<AuthStoreState>()(
persist(
(_) => ({
token: null,
Expand All @@ -23,5 +23,9 @@ export const useAuthStore = create<AuthStoreState>()(
),
);

export const getState = () => useAuthStore.getState();

export const getToken = () => useAuthStore((state) => state.token);

export const setToken = (token: string | null) =>
useAuthStore.setState(() => ({ token }));
18 changes: 11 additions & 7 deletions src/stores/useExampleStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ export type ExampleStoreState = {
thirdValue: number | null;
};

export const useStore = create<ExampleStoreState>()(() => ({
const useStore = create<ExampleStoreState>(() => ({
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 }));

0 comments on commit a7c71cc

Please sign in to comment.