90 lines
2.0 KiB
JavaScript
90 lines
2.0 KiB
JavaScript
// frontend/src/repositories/AuthRepository.js
|
|
import { directusApi } from '../services/directus';
|
|
|
|
export class AuthRepository {
|
|
constructor() {
|
|
this.api = directusApi;
|
|
}
|
|
|
|
async login(email, password) {
|
|
try {
|
|
const response = await this.api.post('/auth/login', {
|
|
email,
|
|
password,
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
throw this.handleError(error);
|
|
}
|
|
}
|
|
|
|
async logout() {
|
|
try {
|
|
const response = await this.api.post('/auth/logout');
|
|
return response.data;
|
|
} catch (error) {
|
|
throw this.handleError(error);
|
|
}
|
|
}
|
|
|
|
async refresh() {
|
|
try {
|
|
const refreshToken = localStorage.getItem('directus_refresh_token');
|
|
const response = await this.api.post('/auth/refresh', {
|
|
refresh_token: refreshToken,
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
throw this.handleError(error);
|
|
}
|
|
}
|
|
|
|
async getMe() {
|
|
try {
|
|
const response = await this.api.get('/users/me');
|
|
return response.data;
|
|
} catch (error) {
|
|
throw this.handleError(error);
|
|
}
|
|
}
|
|
|
|
async updatePassword(currentPassword, newPassword) {
|
|
try {
|
|
const response = await this.api.patch('/users/me', {
|
|
password: newPassword,
|
|
current_password: currentPassword,
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
throw this.handleError(error);
|
|
}
|
|
}
|
|
|
|
async requestPasswordReset(email) {
|
|
try {
|
|
const response = await this.api.post('/auth/password/request', {
|
|
email,
|
|
});
|
|
return response.data;
|
|
} catch (error) {
|
|
throw this.handleError(error);
|
|
}
|
|
}
|
|
|
|
handleError(error) {
|
|
console.error('Auth Repository Error:', error);
|
|
|
|
if (error.response?.data?.errors) {
|
|
const message =
|
|
error.response.data.errors[0]?.message || 'Authentication failed';
|
|
return new Error(message);
|
|
}
|
|
|
|
if (error.response?.status === 401) {
|
|
return new Error('Invalid email or password');
|
|
}
|
|
|
|
return new Error(error.message || 'Authentication error occurred');
|
|
}
|
|
}
|