149 lines
4.5 KiB
TypeScript
149 lines
4.5 KiB
TypeScript
import api from '../../../services/api';
|
|
import type { User, LoginCredentials, Role } from '../composables/useAuth';
|
|
|
|
export interface LoginResponse {
|
|
status: string;
|
|
data: {
|
|
user: User;
|
|
userRoles: Role[];
|
|
token: string;
|
|
};
|
|
}
|
|
|
|
export interface RegisterData {
|
|
name: string;
|
|
email: string;
|
|
password: string;
|
|
password_confirmation: string;
|
|
}
|
|
|
|
class AuthService {
|
|
/**
|
|
* Iniciar sesión
|
|
*/
|
|
async login(credentials: LoginCredentials): Promise<{ user: User; token: string }> {
|
|
try {
|
|
const response = await api.post<LoginResponse>('/api/auth/login', {
|
|
email: credentials.email,
|
|
password: credentials.password
|
|
});
|
|
|
|
// Retornar solo user y token para mantener compatibilidad con useAuth
|
|
return {
|
|
user: response.data.data.user,
|
|
token: response.data.data.token
|
|
};
|
|
} catch (error: any) {
|
|
console.error('Error en login:', error);
|
|
|
|
// Manejar errores de validación (422)
|
|
if (error.response?.status === 422 && error.response?.data?.errors) {
|
|
const errors = error.response.data.errors;
|
|
const firstError = Object.values(errors)[0] as string[];
|
|
throw new Error(firstError[0] || error.response.data.message);
|
|
}
|
|
|
|
// Manejar otros errores
|
|
throw new Error(error.response?.data?.message || 'Error al iniciar sesión');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Registrar nuevo usuario
|
|
*/
|
|
async register(data: RegisterData): Promise<LoginResponse> {
|
|
try {
|
|
const response = await api.post<LoginResponse>('/auth/register', data);
|
|
return response.data;
|
|
} catch (error: any) {
|
|
throw new Error(error.response?.data?.message || 'Error al registrar usuario');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cerrar sesión
|
|
*/
|
|
async logout(): Promise<void> {
|
|
try {
|
|
const response = await api.post<{
|
|
status: string;
|
|
data: {
|
|
is_revoked: boolean;
|
|
};
|
|
}>('/api/auth/logout');
|
|
|
|
console.log('Respuesta logout:', response.data);
|
|
|
|
if (response.data.status === 'success' && response.data.data.is_revoked) {
|
|
console.log('Token revocado exitosamente');
|
|
}
|
|
} catch (error: any) {
|
|
console.error('Error al cerrar sesión:', error);
|
|
// No lanzar error para que el logout local continúe aunque falle el backend
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Obtener usuario actual
|
|
*/
|
|
async getCurrentUser(): Promise<User> {
|
|
try {
|
|
const response = await api.get<User>('/auth/me');
|
|
return response.data;
|
|
} catch (error: any) {
|
|
throw new Error(error.response?.data?.message || 'Error al obtener usuario');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Actualizar perfil de usuario
|
|
*/
|
|
async updateProfile(data: Partial<User>): Promise<User> {
|
|
try {
|
|
const response = await api.put<User>('/auth/profile', data);
|
|
return response.data;
|
|
} catch (error: any) {
|
|
throw new Error(error.response?.data?.message || 'Error al actualizar perfil');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Cambiar contraseña
|
|
*/
|
|
async changePassword(currentPassword: string, newPassword: string): Promise<void> {
|
|
try {
|
|
await api.post('/auth/change-password', {
|
|
current_password: currentPassword,
|
|
new_password: newPassword
|
|
});
|
|
} catch (error: any) {
|
|
throw new Error(error.response?.data?.message || 'Error al cambiar contraseña');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Solicitar recuperación de contraseña
|
|
*/
|
|
async forgotPassword(email: string): Promise<void> {
|
|
try {
|
|
await api.post('/auth/forgot-password', { email });
|
|
} catch (error: any) {
|
|
throw new Error(error.response?.data?.message || 'Error al solicitar recuperación');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Resetear contraseña con token
|
|
*/
|
|
async resetPassword(token: string, password: string): Promise<void> {
|
|
try {
|
|
await api.post('/auth/reset-password', { token, password });
|
|
} catch (error: any) {
|
|
throw new Error(error.response?.data?.message || 'Error al resetear contraseña');
|
|
}
|
|
}
|
|
}
|
|
|
|
export const authService = new AuthService();
|
|
export default authService;
|