edit: response.message
This commit is contained in:
parent
d7f824f353
commit
7de5457170
21 changed files with 227 additions and 127 deletions
|
|
@ -46,6 +46,13 @@ export interface LoginResponse {
|
|||
role: string;
|
||||
avatarUrl?: string | null;
|
||||
};
|
||||
message?: string;
|
||||
}
|
||||
|
||||
export interface ApiResponse<T> {
|
||||
code: number;
|
||||
message: string;
|
||||
data: T;
|
||||
}
|
||||
|
||||
// Mock data for development
|
||||
|
|
@ -135,14 +142,23 @@ export const authService = {
|
|||
lastName: response.user.profile.last_name,
|
||||
role: response.user.role.code,
|
||||
avatarUrl: response.user.profile.avatar_url
|
||||
}
|
||||
},
|
||||
message: 'เข้าสู่ระบบสำเร็จ' // Note: Backend usually returns message too, but we can default it or use backend's
|
||||
};
|
||||
} catch (error: any) {
|
||||
// Re-throw custom errors (like STUDENT role block)
|
||||
if (error.message && !error.response) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
// Handle API errors
|
||||
const apiError = error.data?.error || error.data;
|
||||
const errorMessage = apiError?.message || error.message;
|
||||
|
||||
if (errorMessage) {
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
|
||||
if (error.response?.status === 401) {
|
||||
throw new Error('อีเมลหรือรหัสผ่านไม่ถูกต้อง');
|
||||
}
|
||||
|
|
@ -161,22 +177,27 @@ export const authService = {
|
|||
userCookie.value = null;
|
||||
},
|
||||
|
||||
async forgotPassword(email: string): Promise<void> {
|
||||
async forgotPassword(email: string): Promise<ApiResponse<void>> {
|
||||
const config = useRuntimeConfig();
|
||||
const useMockData = config.public.useMockData as boolean;
|
||||
|
||||
if (useMockData) {
|
||||
// Mock: simulate sending email
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
return;
|
||||
return {
|
||||
code: 200,
|
||||
message: 'ส่งลิงก์รีเซ็ตรหัสผ่านไปยังอีเมลของคุณแล้ว (Mock)',
|
||||
data: undefined
|
||||
};
|
||||
}
|
||||
|
||||
// Real API
|
||||
await $fetch('/api/auth/reset-request', {
|
||||
const response = await $fetch<ApiResponse<void>>('/api/auth/reset-request', {
|
||||
method: 'POST',
|
||||
baseURL: config.public.apiBaseUrl as string,
|
||||
body: { email }
|
||||
});
|
||||
return response;
|
||||
},
|
||||
|
||||
async resetPassword(token: string, password: string): Promise<void> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue