diff --git a/src/features/auth/model/stores/authStore/authStore.ts b/src/features/auth/model/stores/authStore/authStore.ts index 2ea3846..d6aba35 100644 --- a/src/features/auth/model/stores/authStore/authStore.ts +++ b/src/features/auth/model/stores/authStore/authStore.ts @@ -2,12 +2,12 @@ import { create } from "zustand"; import type { AuthStore } from "../../types/store"; import { login, logout, register } from "../../../api"; import { callApi } from "shared/utils"; +import { UNEXPECTED_ERROR_MESSAGE } from "shared/config"; export const useAuthStore = create()((set) => ({ user: undefined, status: "idle", - setUser: (user) => set({ user }), - setStatus: (status) => set({ status }), + error: null, login: async (loginData) => { set({ status: "loading" }); @@ -15,17 +15,21 @@ export const useAuthStore = create()((set) => ({ const [responseData, loginError] = await callApi(() => login(loginData)); if (loginError) { - set({ status: "unauthenticated" }); + set({ status: "unauthenticated", error: loginError }); return; } set({ status: "authenticated", user: responseData?.user, + error: null, }); } catch (err) { - console.warn(err); - set({ status: "idle" }); + console.error(err); + set({ + status: "unauthenticated", + error: new Error(UNEXPECTED_ERROR_MESSAGE), + }); } }, register: async (registerData) => { @@ -35,17 +39,21 @@ export const useAuthStore = create()((set) => ({ ); if (registerError) { - set({ status: "unauthenticated" }); + set({ status: "unauthenticated", error: registerError }); return; } set({ status: "authenticated", user: responseData?.user, + error: null, }); } catch (err) { - console.warn(err); - set({ status: "idle" }); + console.error(err); + set({ + status: "unauthenticated", + error: new Error(UNEXPECTED_ERROR_MESSAGE), + }); } }, logout: async () => { @@ -54,13 +62,18 @@ export const useAuthStore = create()((set) => ({ const [, logoutError] = await callApi(() => logout()); if (logoutError) { - set({ status: "authenticated" }); + set({ error: logoutError }); + return; } - set({ status: "unauthenticated", user: undefined }); + set({ + status: "unauthenticated", + user: undefined, + error: null, + }); } catch (err) { - console.warn(err); - set({ status: "idle" }); + console.error(err); + set({ error: new Error(UNEXPECTED_ERROR_MESSAGE) }); } }, })); diff --git a/src/features/auth/model/types/store.ts b/src/features/auth/model/types/store.ts index 9d23616..5952dc8 100644 --- a/src/features/auth/model/types/store.ts +++ b/src/features/auth/model/types/store.ts @@ -1,8 +1,8 @@ import type { User } from "entities/User"; import type { AuthData, AuthStatus } from "./service"; +import type { ApiError } from "shared/utils"; export interface AuthStoreState { - /** * User's credentials */ @@ -11,6 +11,10 @@ export interface AuthStoreState { * Authentication status */ status: AuthStatus; + /** + * Error data + */ + error: ApiError | Error | null; } export type LoginAction = (data: AuthData) => void; @@ -18,9 +22,6 @@ export type RegisterAction = (data: AuthData) => void; export type LogoutAction = () => void; export interface AuthStoreActions { - setUser: (user: AuthStoreState["user"] | undefined) => void; - setStatus: (status: AuthStoreState["status"]) => void; - // Async actions login: LoginAction; register: RegisterAction;