From 6370a118fe2ad9810957caf153c81fc8c6659c0d Mon Sep 17 00:00:00 2001 From: Samandar Turgunboyev Date: Mon, 29 Dec 2025 11:45:17 +0500 Subject: [PATCH] refresh token qoshildi --- src/features/auth/ui/AuthLogin.tsx | 3 +- src/shared/config/api/URLs.ts | 1 + src/shared/config/api/httpClient.ts | 54 ++++++++++++++++++++++++++--- src/shared/lib/cookie.ts | 15 +++++++- 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/features/auth/ui/AuthLogin.tsx b/src/features/auth/ui/AuthLogin.tsx index 9be1988..5aeda68 100644 --- a/src/features/auth/ui/AuthLogin.tsx +++ b/src/features/auth/ui/AuthLogin.tsx @@ -1,5 +1,5 @@ import { auth_pai } from "@/features/auth/lib/api"; -import { saveToken } from "@/shared/lib/cookie"; +import { saveRefToken, saveToken } from "@/shared/lib/cookie"; import { Button } from "@/shared/ui/button"; import { Card, @@ -41,6 +41,7 @@ const AuthLogin = () => { }); saveToken(res.data.data.access); + saveRefToken(res.data.data.refresh); navigate("dashboard"); }, onError: (err: AxiosError) => { diff --git a/src/shared/config/api/URLs.ts b/src/shared/config/api/URLs.ts index 5d27988..f76bcf4 100644 --- a/src/shared/config/api/URLs.ts +++ b/src/shared/config/api/URLs.ts @@ -33,4 +33,5 @@ export const API_URLS = { DeleteProdut: (id: string) => `${API_V}admin/product/${id}/delete/`, QuestionnaireList: `${API_V}admin/questionnaire/list/`, Import_User: `${API_V}admin/user/import_users/`, + Refresh_Token: `${API_V}accounts/refresh/token/`, }; diff --git a/src/shared/config/api/httpClient.ts b/src/shared/config/api/httpClient.ts index 3c70a47..4e04972 100644 --- a/src/shared/config/api/httpClient.ts +++ b/src/shared/config/api/httpClient.ts @@ -1,6 +1,13 @@ import i18n from "@/shared/config/i18n"; -import { getToken } from "@/shared/lib/cookie"; -import axios from "axios"; +import { + getRefToken, + getToken, + removeRefToken, + removeToken, + saveRefToken, + saveToken, +} from "@/shared/lib/cookie"; +import axios, { AxiosError } from "axios"; import { API_URLS } from "./URLs"; const httpClient = axios.create({ @@ -24,9 +31,48 @@ httpClient.interceptors.request.use( ); httpClient.interceptors.response.use( - (response) => response, - (error) => { + async (response) => response, + async (error) => { console.error("API error:", error); + if ((error as AxiosError)?.status === 403) { + try { + const refToken = getRefToken(); + if (!refToken) { + removeToken(); + removeRefToken(); + window.location.href = "/"; + } + const { data } = await axios.post( + `${API_URLS.BASE_URL}${API_URLS.Refresh_Token}`, + { refresh: refToken }, + ); + saveToken(data.access); + saveRefToken(data.refresh); + } catch { + removeToken(); + removeRefToken(); + window.location.href = "/"; + } + } else if ((error as AxiosError)?.status === 401) { + try { + const refToken = getRefToken(); + if (!refToken) { + removeToken(); + removeRefToken(); + window.location.href = "/"; + } + const { data } = await axios.post( + `${API_URLS.BASE_URL}${API_URLS.Refresh_Token}`, + { refresh: refToken }, + ); + saveToken(data.access); + saveRefToken(data.refresh); + } catch { + removeToken(); + removeRefToken(); + window.location.href = "/"; + } + } return Promise.reject(error); }, ); diff --git a/src/shared/lib/cookie.ts b/src/shared/lib/cookie.ts index 3d244ae..a7bffc0 100644 --- a/src/shared/lib/cookie.ts +++ b/src/shared/lib/cookie.ts @@ -1,6 +1,7 @@ import cookie from "js-cookie"; -const token = "access_meridyn_admin"; +const token = "gastro_admin_token"; +const refToken = "gastro_admin_refresh_token"; export const saveToken = (value: string) => { cookie.set(token, value); @@ -13,3 +14,15 @@ export const getToken = () => { export const removeToken = () => { cookie.remove(token); }; + +export const saveRefToken = (value: string) => { + cookie.set(refToken, value); +}; + +export const getRefToken = () => { + return cookie.get(refToken); +}; + +export const removeRefToken = () => { + cookie.remove(refToken); +};