mirror of
https://github.com/lorsanstand/Aether.git
synced 2026-09-18 14:18:20 +03:00
add frontend and change password
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import axios from 'axios';
|
||||
|
||||
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8080/api/v1';
|
||||
|
||||
const apiClient = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
withCredentials: true,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// Interceptor для обработки ошибок
|
||||
apiClient.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
// Redirect to login if unauthorized, but not if already on public pages
|
||||
const publicPaths = ['/auth', '/verify-email', '/reset-password'];
|
||||
const currentPath = window.location.pathname;
|
||||
const isPublicPage = publicPaths.some(path => currentPath.startsWith(path));
|
||||
|
||||
if (!isPublicPage) {
|
||||
window.location.href = '/auth';
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default apiClient;
|
||||
@@ -0,0 +1,54 @@
|
||||
import apiClient from './api';
|
||||
|
||||
export interface LoginData {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface RegisterData {
|
||||
email: string;
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export const authService = {
|
||||
login: async (data: LoginData) => {
|
||||
const formData = new URLSearchParams();
|
||||
formData.append('username', data.username);
|
||||
formData.append('password', data.password);
|
||||
|
||||
const response = await apiClient.post('/auth/login', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
|
||||
register: async (data: RegisterData) => {
|
||||
const response = await apiClient.post('/auth/register', data);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
logout: async () => {
|
||||
const response = await apiClient.post('/auth/logout');
|
||||
return response.data;
|
||||
},
|
||||
|
||||
verifyEmail: async (token: string) => {
|
||||
const response = await apiClient.post(`/auth/email/verify/${token}`);
|
||||
return response.data;
|
||||
},
|
||||
|
||||
resetPassword: async (token: string, newPassword: string) => {
|
||||
const response = await apiClient.post(`/auth/password/reset/${token}`, null, {
|
||||
params: { new_password: newPassword }
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
|
||||
getCurrentUser: async () => {
|
||||
const response = await apiClient.get('/users/me');
|
||||
return response.data;
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user