From d7f824f353412fc3b8f80b4843ffeca04ceb5a50 Mon Sep 17 00:00:00 2001 From: JakkrapartXD Date: Fri, 30 Jan 2026 17:54:43 +0700 Subject: [PATCH] feat: standardize login response format with code, message, and data wrapper Update login endpoint to return consistent API response structure with code, message, and data fields. Wrap token, refreshToken, and user data inside a data object to match the standardized response format used across other endpoints. --- Backend/src/controllers/AuthController.ts | 52 ++++++++++++----------- Backend/src/services/auth.service.ts | 10 +++-- Backend/src/types/auth.types.ts | 10 +++-- 3 files changed, 42 insertions(+), 30 deletions(-) diff --git a/Backend/src/controllers/AuthController.ts b/Backend/src/controllers/AuthController.ts index 163ab568..47f61722 100644 --- a/Backend/src/controllers/AuthController.ts +++ b/Backend/src/controllers/AuthController.ts @@ -28,31 +28,35 @@ export class AuthController { @SuccessResponse('200', 'Login successful') @Response('401', 'Invalid credentials') @Example({ - token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...', - refreshToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...', - user: { - id: 1, - username: 'admin', - email: 'admin@elearning.local', - updated_at: new Date('2024-01-01T00:00:00Z'), - created_at: new Date('2024-01-01T00:00:00Z'), - role: { - code: 'ADMIN', - name: { - th: 'ผู้ดูแลระบบ', - en: 'Administrator' - } - }, - profile: { - prefix: { - th: 'นาย', - en: 'Mr.' + code: 200, + message: 'Login successful', + data: { + token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...', + refreshToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...', + user: { + id: 1, + username: 'admin', + email: 'admin@elearning.local', + updated_at: new Date('2024-01-01T00:00:00Z'), + created_at: new Date('2024-01-01T00:00:00Z'), + role: { + code: 'ADMIN', + name: { + th: 'ผู้ดูแลระบบ', + en: 'Administrator' + } }, - first_name: 'Admin', - last_name: 'User', - phone: null, - avatar_url: null, - birth_date: null + profile: { + prefix: { + th: 'นาย', + en: 'Mr.' + }, + first_name: 'Admin', + last_name: 'User', + phone: null, + avatar_url: null, + birth_date: null + } } } }) diff --git a/Backend/src/services/auth.service.ts b/Backend/src/services/auth.service.ts index ad565c93..1860b601 100644 --- a/Backend/src/services/auth.service.ts +++ b/Backend/src/services/auth.service.ts @@ -58,9 +58,13 @@ export class AuthService { logger.info('User logged in successfully', { userId: user.id, email: user.email }); return { - token, - refreshToken, - user: await this.formatUserResponse(user) + code: 200, + message: 'Login successful', + data: { + token, + refreshToken, + user: await this.formatUserResponse(user) + } }; } diff --git a/Backend/src/types/auth.types.ts b/Backend/src/types/auth.types.ts index d7d016e5..be1fe625 100644 --- a/Backend/src/types/auth.types.ts +++ b/Backend/src/types/auth.types.ts @@ -23,9 +23,13 @@ export interface RegisterRequest { } export interface LoginResponse { - token: string; - refreshToken: string; - user: UserResponse; + code: number; + message: string; + data: { + token: string; + refreshToken: string; + user: UserResponse; + }; } export interface RegisterResponse {