elearning/frontend_management/services/auth.service.ts

215 lines
6.4 KiB
TypeScript
Raw Normal View History

2026-01-14 13:58:25 +07:00
export interface LoginRequest {
email: string;
password: string;
}
// API Response structure (from backend)
export interface ApiLoginResponse {
token: string;
refreshToken: string;
user: {
id: number;
username: string;
email: string;
updated_at: string;
created_at: string;
role: {
code: string;
name: {
en: string;
th: string;
};
};
profile: {
prefix: {
en: string;
th: string;
};
first_name: string;
last_name: string;
phone: string | null;
avatar_url: string | null;
birth_date: string | null;
};
};
}
// Frontend User structure
export interface LoginResponse {
token: string;
refreshToken: string;
user: {
id: string;
email: string;
firstName: string;
lastName: string;
2026-01-14 13:58:25 +07:00
role: string;
avatarUrl?: string | null;
2026-01-14 13:58:25 +07:00
};
2026-02-02 09:31:22 +07:00
message?: string;
}
export interface ApiResponse<T> {
code: number;
message: string;
data: T;
2026-01-14 13:58:25 +07:00
}
export const authService = {
async login(email: string, password: string): Promise<LoginResponse> {
const config = useRuntimeConfig();
try {
const response = await $fetch<ApiResponse<ApiLoginResponse>>('/api/auth/login', {
2026-01-14 13:58:25 +07:00
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
body: {
email,
password
}
});
const loginData = response.data;
// Check if user role is STUDENT - block login
if (loginData.user.role.code === 'STUDENT') {
throw new Error('ไม่สามารถเข้าสู่ระบบได้ ระบบนี้สำหรับผู้สอนและผู้ดูแลระบบเท่านั้น');
}
2026-01-14 13:58:25 +07:00
// Transform API response to frontend format
return {
token: loginData.token,
refreshToken: loginData.refreshToken,
2026-01-14 13:58:25 +07:00
user: {
id: loginData.user.id.toString(),
email: loginData.user.email,
firstName: loginData.user.profile.first_name,
lastName: loginData.user.profile.last_name,
role: loginData.user.role.code,
avatarUrl: loginData.user.profile.avatar_url
2026-02-02 09:31:22 +07:00
},
message: response.message || 'เข้าสู่ระบบสำเร็จ'
2026-01-14 13:58:25 +07:00
};
} catch (error: any) {
// Re-throw custom errors (like STUDENT role block)
if (error.message && !error.response) {
throw error;
}
2026-02-02 09:31:22 +07:00
2026-01-14 13:58:25 +07:00
// Handle API errors
2026-02-02 09:31:22 +07:00
const apiError = error.data?.error || error.data;
const errorMessage = apiError?.message || error.message;
if (errorMessage) {
throw new Error(errorMessage);
}
2026-01-14 13:58:25 +07:00
if (error.response?.status === 401) {
throw new Error('อีเมลหรือรหัสผ่านไม่ถูกต้อง');
2026-01-14 13:58:25 +07:00
}
throw new Error('เกิดข้อผิดพลาดในการเข้าสู่ระบบ');
2026-01-14 13:58:25 +07:00
}
},
async logout(): Promise<void> {
// Clear cookies
const tokenCookie = useCookie('token');
const refreshTokenCookie = useCookie('refreshToken');
const userCookie = useCookie('user');
tokenCookie.value = null;
refreshTokenCookie.value = null;
userCookie.value = null;
},
2026-02-02 09:31:22 +07:00
async forgotPassword(email: string): Promise<ApiResponse<void>> {
const config = useRuntimeConfig();
2026-02-02 09:31:22 +07:00
const response = await $fetch<ApiResponse<void>>('/api/auth/reset-request', {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
body: { email }
});
2026-02-02 09:31:22 +07:00
return response;
},
async resetPassword(token: string, password: string): Promise<void> {
const config = useRuntimeConfig();
await $fetch('/api/auth/reset-password', {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
body: { token, password }
});
},
async registerInstructor(data: RegisterInstructorRequest): Promise<void> {
const config = useRuntimeConfig();
await $fetch('/api/auth/register-instructor', {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
body: data
});
},
async refreshToken(currentRefreshToken: string): Promise<{ token: string; refreshToken: string }> {
const config = useRuntimeConfig();
if (!currentRefreshToken) {
throw new Error('No refresh token available');
}
const response = await $fetch<{ token: string; refreshToken: string }>('/api/auth/refresh', {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
body: { refreshToken: currentRefreshToken }
});
return response;
},
async sendVerifyEmail(): Promise<ApiResponse<void>> {
const config = useRuntimeConfig();
const token = useCookie('token').value;
const response = await $fetch<ApiResponse<void>>('/api/user/send-verify-email', {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
headers: {
Authorization: `Bearer ${token}`
}
});
return response;
},
async verifyEmail(verificationToken: string): Promise<ApiResponse<void>> {
const config = useRuntimeConfig();
const token = useCookie('token').value;
const response = await $fetch<ApiResponse<void>>('/api/user/verify-email', {
method: 'POST',
baseURL: config.public.apiBaseUrl as string,
headers: {
Authorization: `Bearer ${token}`
},
body: { token: verificationToken }
});
return response;
2026-01-14 13:58:25 +07:00
}
};
// Register Instructor Request
export interface RegisterInstructorRequest {
username: string;
email: string;
password: string;
first_name: string;
last_name: string;
prefix: {
en: string;
th: string;
};
phone: string;
}