From b75e805f54051d7f3229203f9eb7041d143f1c10 Mon Sep 17 00:00:00 2001 From: Ilia Mashkov Date: Tue, 17 Mar 2026 14:18:49 +0300 Subject: [PATCH] feat(auth): add refresh action to authStore --- .../auth/model/stores/authStore/authStore.ts | 26 ++++++++++++++++++- src/features/auth/model/types/store.ts | 2 ++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/features/auth/model/stores/authStore/authStore.ts b/src/features/auth/model/stores/authStore/authStore.ts index b46b004..6722972 100644 --- a/src/features/auth/model/stores/authStore/authStore.ts +++ b/src/features/auth/model/stores/authStore/authStore.ts @@ -1,6 +1,6 @@ import { create } from "zustand"; import type { AuthStore, AuthStoreState } from "../../types/store"; -import { login, logout, register } from "../../../api"; +import { login, logout, refresh, register } from "../../../api"; import { callApi } from "shared/utils"; import { UNEXPECTED_ERROR_MESSAGE } from "shared/config"; @@ -83,4 +83,28 @@ export const useAuthStore = create()((set) => ({ set({ error: new Error(UNEXPECTED_ERROR_MESSAGE) }); } }, + refresh: async () => { + set({ status: "loading" }); + try { + const [responseData, refreshError] = await callApi(() => refresh()); + + if (refreshError) { + set({ status: "unauthenticated", error: refreshError }); + return; + } + + set({ + status: "authenticated", + user: responseData?.user, + accessToken: responseData?.accessToken, + error: null, + }); + } catch (err) { + console.error(err); + set({ + status: "unauthenticated", + 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 4692f61..f0faab8 100644 --- a/src/features/auth/model/types/store.ts +++ b/src/features/auth/model/types/store.ts @@ -24,12 +24,14 @@ export interface AuthStoreState { export type LoginAction = (data: AuthData) => void; export type RegisterAction = (data: AuthData) => void; export type LogoutAction = () => void; +export type RefreshAction = () => void; export interface AuthStoreActions { // Async actions login: LoginAction; register: RegisterAction; logout: LogoutAction; + refresh: RefreshAction; } export type AuthStore = AuthStoreState & AuthStoreActions;