feat : add signin ,signout, forgot password features
This commit is contained in:
@@ -1,43 +1,95 @@
|
||||
import axios from 'axios';
|
||||
|
||||
// Create axios instance
|
||||
import { useAuthStore } from "../store/authStore";
|
||||
|
||||
const BASE_URL = import.meta.env.DEV ? "/" : "https://khalijpay-core.qaserver.ir";
|
||||
|
||||
// ساخت instance از axios
|
||||
const api = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_URL || 'http://localhost:3001/api',
|
||||
timeout: 10000,
|
||||
baseURL: BASE_URL,
|
||||
withCredentials: true, // ارسال و دریافت cookie/session
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
// Request interceptor to add JWT token
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
// -----------------------------
|
||||
// Interceptor پاسخها
|
||||
// -----------------------------
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
if (error.response?.status === 401) {
|
||||
// session منقضی شده → هدایت به login
|
||||
const setLoggedIn = useAuthStore.getState().setLoggedIn;
|
||||
setLoggedIn(false);
|
||||
window.location.href = "/";
|
||||
}
|
||||
return Promise.reject(error.response?.data || error);
|
||||
}
|
||||
);
|
||||
|
||||
// Response interceptor to handle token expiration
|
||||
api.interceptors.response.use(
|
||||
(response) => {
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
// Token expired or invalid
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('user');
|
||||
window.location.href = '/login';
|
||||
}
|
||||
return Promise.reject(error);
|
||||
// -----------------------------
|
||||
// توابع API
|
||||
// -----------------------------
|
||||
|
||||
// Login
|
||||
export async function login(username, password) {
|
||||
try {
|
||||
const res = await api.post("/api/v1/Auth/SignIn", {
|
||||
// include common variants to satisfy different backends
|
||||
userName: username,
|
||||
username: username,
|
||||
email: username,
|
||||
password: password,
|
||||
});
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// خروج از سیستم
|
||||
export async function signOut() {
|
||||
try {
|
||||
const res = await api.post("/api/v1/Auth/SignOut");
|
||||
// پاک کردن وضعیت login در Zustand
|
||||
const setLoggedIn = useAuthStore.getState().setLoggedIn;
|
||||
setLoggedIn(false);
|
||||
window.location.href = "/";
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// فراموشی رمز عبور
|
||||
export async function forgotPassword(email) {
|
||||
try {
|
||||
const res = await api.post("/api/v1/Auth/ForgotPassword", { email });
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// گرفتن دادههای محافظتشده
|
||||
export async function fetchProtectedData(endpoint) {
|
||||
try {
|
||||
const res = await api.get(endpoint);
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Mock data for development
|
||||
const mockData = {
|
||||
@@ -99,62 +151,12 @@ const mockData = {
|
||||
]
|
||||
};
|
||||
|
||||
// API functions
|
||||
export const authAPI = {
|
||||
login: async (email, password) => {
|
||||
try {
|
||||
const response = await api.post('/auth/login', { email, password });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
// Return mock data for demo
|
||||
return {
|
||||
token: 'mock-jwt-token-' + Date.now(),
|
||||
user: {
|
||||
id: 1,
|
||||
email: email,
|
||||
name: 'Admin User',
|
||||
role: 'admin'
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Expose mock payment API for dashboard until real endpoints are integrated
|
||||
export const paymentsAPI = {
|
||||
getStats: async () => {
|
||||
try {
|
||||
const response = await api.get('/payments/stats');
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
// Return mock data for demo
|
||||
return mockData.stats;
|
||||
}
|
||||
async getStats() {
|
||||
return mockData.stats;
|
||||
},
|
||||
|
||||
getPayments: async (page = 1, limit = 10) => {
|
||||
try {
|
||||
const response = await api.get(`/payments?page=${page}&limit=${limit}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
// Return mock data for demo
|
||||
return {
|
||||
data: mockData.payments,
|
||||
total: mockData.payments.length,
|
||||
page,
|
||||
limit
|
||||
};
|
||||
}
|
||||
async getChartData() {
|
||||
return mockData.chartData;
|
||||
},
|
||||
|
||||
getChartData: async () => {
|
||||
try {
|
||||
const response = await api.get('/payments/chart-data');
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
// Return mock data for demo
|
||||
return mockData.chartData;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default api;
|
||||
|
||||
Reference in New Issue
Block a user