test new login
This commit is contained in:
+3
-7
@@ -1,4 +1,5 @@
|
||||
import axios from "axios";
|
||||
import { getToken } from "./func";
|
||||
// import { Pages } from "./Pages";
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
@@ -8,18 +9,13 @@ const axiosInstance = axios.create({
|
||||
axiosInstance.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
// if (error.response.status === 401) {
|
||||
// localStorage.removeItem(import.meta.env.VITE_TOKEN_NAME);
|
||||
// window.location.href = Pages.auth.login;
|
||||
// }
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
axiosInstance.interceptors.request.use(async function (config) {
|
||||
const token = localStorage.getItem(import.meta.env.VITE_TOKEN_NAME)
|
||||
? localStorage.getItem(import.meta.env.VITE_TOKEN_NAME)
|
||||
: "";
|
||||
const tokenValue = await getToken();
|
||||
const token = tokenValue ? tokenValue : "";
|
||||
|
||||
config.headers.Authorization = "Bearer " + token;
|
||||
return config;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import Cookie from "js-cookie";
|
||||
|
||||
export function isEmail(input: string): boolean {
|
||||
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
|
||||
return emailRegex.test(input);
|
||||
@@ -29,3 +31,43 @@ export const timeAgo = (date: string | Date): string => {
|
||||
|
||||
return "همین الان";
|
||||
};
|
||||
|
||||
export const getToken = () => {
|
||||
return Cookie.get(import.meta.env.VITE_TOKEN_NAME);
|
||||
};
|
||||
|
||||
export const getRefreshToken = () => {
|
||||
return Cookie.get(import.meta.env.VITE_REFRESH_TOKEN_NAME);
|
||||
};
|
||||
|
||||
export const setToken = (token: string) => {
|
||||
Cookie.set(import.meta.env.VITE_TOKEN_NAME, token, {
|
||||
domain: ".danakcorp.com",
|
||||
path: "/",
|
||||
secure: true, // فقط روی HTTPS کار میکنه
|
||||
sameSite: "Strict", // جلوگیری از حملات CSRF
|
||||
});
|
||||
};
|
||||
|
||||
export const setRefreshToken = (refreshToken: string) => {
|
||||
Cookie.set(import.meta.env.VITE_REFRESH_TOKEN_NAME, refreshToken, {
|
||||
domain: ".danakcorp.com",
|
||||
path: "/",
|
||||
secure: true,
|
||||
sameSite: "Strict",
|
||||
});
|
||||
};
|
||||
|
||||
export const removeToken = () => {
|
||||
Cookie.remove(import.meta.env.VITE_TOKEN_NAME, {
|
||||
domain: ".danakcorp.com",
|
||||
path: "/",
|
||||
});
|
||||
};
|
||||
|
||||
export const removeRefreshToken = () => {
|
||||
Cookie.remove(import.meta.env.VITE_REFRESH_TOKEN_NAME, {
|
||||
domain: ".danakcorp.com",
|
||||
path: "/",
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user