get me api
This commit is contained in:
parent
815e8aeaf0
commit
d8d3dff2e7
8 changed files with 1719 additions and 575 deletions
2014
Backend/package-lock.json
generated
2014
Backend/package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -35,6 +35,8 @@ export class AuthController {
|
|||
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: {
|
||||
|
|
@ -49,7 +51,9 @@ export class AuthController {
|
|||
},
|
||||
first_name: 'Admin',
|
||||
last_name: 'User',
|
||||
avatar_url: undefined
|
||||
phone: null,
|
||||
avatar_url: null,
|
||||
birth_date: null
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
@ -79,6 +83,8 @@ export class AuthController {
|
|||
id: 4,
|
||||
username: 'newstudent',
|
||||
email: 'student@example.com',
|
||||
updated_at: new Date('2024-01-01T00:00:00Z'),
|
||||
created_at: new Date('2024-01-01T00:00:00Z'),
|
||||
role: {
|
||||
code: 'STUDENT',
|
||||
name: {
|
||||
|
|
@ -92,7 +98,10 @@ export class AuthController {
|
|||
en: 'Mr.'
|
||||
},
|
||||
first_name: 'John',
|
||||
last_name: 'Doe'
|
||||
last_name: 'Doe',
|
||||
phone: null,
|
||||
avatar_url: null,
|
||||
birth_date: null
|
||||
}
|
||||
},
|
||||
message: 'Registration successful'
|
||||
|
|
|
|||
37
Backend/src/controllers/UserController.ts
Normal file
37
Backend/src/controllers/UserController.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { Get, Body, Post, Route, Tags, SuccessResponse, Response, Example, Controller, Security, Request } from 'tsoa';
|
||||
import { UserService } from '../services/user.service';
|
||||
import {
|
||||
UserResponse,
|
||||
ProfileResponse,
|
||||
ProfileUpdate,
|
||||
ProfileUpdateResponse,
|
||||
ChangePasswordRequest,
|
||||
ChangePasswordResponse
|
||||
} from '../types/user.types';
|
||||
import { profileUpdateSchema, changePasswordSchema } from "../validators/user.validator";
|
||||
import { ValidationError } from '../middleware/errorHandler';
|
||||
|
||||
@Route('api/user')
|
||||
@Tags('Usermanagement')
|
||||
export class UserController {
|
||||
private userService = new UserService();
|
||||
|
||||
/**
|
||||
* Get current user profile
|
||||
* @summary Retrieve authenticated user's profile information
|
||||
* @param request Express request object with JWT token in Authorization header
|
||||
*/
|
||||
@Get('me')
|
||||
@SuccessResponse('200', 'User found')
|
||||
@Response('404', 'User not found')
|
||||
@Response('401', 'Invalid or expired token')
|
||||
@Security('jwt')
|
||||
public async getMe(@Request() request: any): Promise<UserResponse> {
|
||||
// Extract token from Authorization header
|
||||
const token = request.headers.authorization?.replace('Bearer ', '');
|
||||
if (!token) {
|
||||
throw new ValidationError('No token provided');
|
||||
}
|
||||
return await this.userService.getUserProfile(token);
|
||||
}
|
||||
}
|
||||
|
|
@ -10,11 +10,11 @@ import {
|
|||
LoginResponse,
|
||||
RegisterResponse,
|
||||
RefreshTokenResponse,
|
||||
UserResponse,
|
||||
ResetPasswordResponse,
|
||||
ChangePasswordResponse,
|
||||
ResetRequestResponse
|
||||
} from '../types/auth.types';
|
||||
import { UserResponse } from '../types/user.types';
|
||||
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
|
||||
import nodemailer from 'nodemailer';
|
||||
|
||||
|
|
@ -323,6 +323,8 @@ export class AuthService {
|
|||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
updated_at: user.updated_at,
|
||||
created_at: user.created_at,
|
||||
role: {
|
||||
code: user.role.code,
|
||||
name: user.role.name
|
||||
|
|
@ -331,7 +333,9 @@ export class AuthService {
|
|||
prefix: user.profile.prefix,
|
||||
first_name: user.profile.first_name,
|
||||
last_name: user.profile.last_name,
|
||||
avatar_url: user.profile.avatar_url
|
||||
phone: user.profile.phone,
|
||||
avatar_url: user.profile.avatar_url,
|
||||
birth_date: user.profile.birth_date
|
||||
} : undefined
|
||||
};
|
||||
}
|
||||
|
|
|
|||
91
Backend/src/services/user.service.ts
Normal file
91
Backend/src/services/user.service.ts
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
import { prisma } from '../config/database';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { config } from '../config';
|
||||
import { logger } from '../config/logger';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import {
|
||||
UserResponse,
|
||||
ProfileResponse,
|
||||
ProfileUpdate,
|
||||
ProfileUpdateResponse,
|
||||
ChangePasswordRequest,
|
||||
ChangePasswordResponse
|
||||
} from '../types/user.types';
|
||||
import { UnauthorizedError, ValidationError, ForbiddenError } from '../middleware/errorHandler';
|
||||
|
||||
export class UserService {
|
||||
async getUserProfile(token: string): Promise<UserResponse> {
|
||||
try {
|
||||
// Decode JWT token to get user ID
|
||||
const decoded = jwt.verify(token, config.jwt.secret) as { id: number; username: string; email: string; roleCode: string };
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: decoded.id
|
||||
},
|
||||
include: {
|
||||
profile: true,
|
||||
role: true
|
||||
}
|
||||
});
|
||||
|
||||
if (!user) throw new UnauthorizedError("User not found");
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
updated_at: user.updated_at,
|
||||
created_at: user.created_at,
|
||||
role: {
|
||||
code: user.role.code,
|
||||
name: user.role.name as { th: string; en: string }
|
||||
},
|
||||
profile: user.profile ? {
|
||||
prefix: user.profile.prefix as { th?: string; en?: string } | undefined,
|
||||
first_name: user.profile.first_name,
|
||||
last_name: user.profile.last_name,
|
||||
avatar_url: user.profile.avatar_url,
|
||||
birth_date: user.profile.birth_date,
|
||||
phone: user.profile.phone
|
||||
} : undefined
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof jwt.JsonWebTokenError) {
|
||||
logger.error('Invalid JWT token:', error);
|
||||
throw new UnauthorizedError('Invalid token');
|
||||
}
|
||||
if (error instanceof jwt.TokenExpiredError) {
|
||||
logger.error('JWT token expired:', error);
|
||||
throw new UnauthorizedError('Token expired');
|
||||
}
|
||||
logger.error('Error fetching user profile:', error);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
/**
|
||||
* Format user response
|
||||
*/
|
||||
private formatUserResponse(user: any): UserResponse {
|
||||
return {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
updated_at: user.updated_at,
|
||||
created_at: user.created_at,
|
||||
role: {
|
||||
code: user.role.code,
|
||||
name: user.role.name
|
||||
},
|
||||
profile: user.profile ? {
|
||||
prefix: user.profile.prefix,
|
||||
first_name: user.profile.first_name,
|
||||
last_name: user.profile.last_name,
|
||||
avatar_url: user.profile.avatar_url,
|
||||
phone: user.profile.phone,
|
||||
birth_date: user.profile.birth_date
|
||||
} : undefined
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2,6 +2,8 @@
|
|||
* Authentication Request/Response Types
|
||||
*/
|
||||
|
||||
import { UserResponse } from './user.types';
|
||||
|
||||
export interface LoginRequest {
|
||||
email: string;
|
||||
password: string;
|
||||
|
|
@ -40,28 +42,6 @@ export interface RefreshTokenResponse {
|
|||
refreshToken: string;
|
||||
}
|
||||
|
||||
export interface UserResponse {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
role: {
|
||||
code: string;
|
||||
name: {
|
||||
th: string;
|
||||
en: string;
|
||||
};
|
||||
};
|
||||
profile?: {
|
||||
prefix?: {
|
||||
th?: string;
|
||||
en?: string;
|
||||
};
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
avatar_url?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface ResetRequest {
|
||||
email: string;
|
||||
|
||||
|
|
|
|||
71
Backend/src/types/user.types.ts
Normal file
71
Backend/src/types/user.types.ts
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/**
|
||||
* User response type
|
||||
*/
|
||||
export interface UserResponse {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
updated_at: Date | null;
|
||||
created_at: Date | null;
|
||||
role: {
|
||||
code: string;
|
||||
name: {
|
||||
th: string;
|
||||
en: string;
|
||||
};
|
||||
};
|
||||
profile?: ProfileResponse;
|
||||
}
|
||||
|
||||
export interface ProfileResponse {
|
||||
prefix?: {
|
||||
th?: string;
|
||||
en?: string;
|
||||
};
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
phone: string | null;
|
||||
avatar_url: string | null;
|
||||
birth_date: Date | null;
|
||||
}
|
||||
|
||||
export interface ProfileUpdate {
|
||||
prefix?: {
|
||||
th?: string;
|
||||
en?: string;
|
||||
};
|
||||
first_name?: string;
|
||||
last_name?: string;
|
||||
phone?: string | null;
|
||||
avatar_url?: string | null;
|
||||
birth_date?: Date | null;
|
||||
};
|
||||
|
||||
export interface ProfileUpdateResponse {
|
||||
code: number;
|
||||
message: string;
|
||||
data: {
|
||||
id: number;
|
||||
prefix?: {
|
||||
th?: string;
|
||||
en?: string;
|
||||
};
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
phone: string | null;
|
||||
avatar_url: string | null;
|
||||
birth_date: Date | null;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
export interface ChangePasswordRequest {
|
||||
old_password: string;
|
||||
new_password: string;
|
||||
};
|
||||
|
||||
|
||||
export interface ChangePasswordResponse {
|
||||
code: number;
|
||||
message: string;
|
||||
};
|
||||
36
Backend/src/validators/user.validator.ts
Normal file
36
Backend/src/validators/user.validator.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import Joi from 'joi';
|
||||
|
||||
export const profileUpdateSchema = Joi.object({
|
||||
prefix: Joi.object({
|
||||
th: Joi.string().optional(),
|
||||
en: Joi.string().optional()
|
||||
}).optional(),
|
||||
first_name: Joi.string()
|
||||
.min(1)
|
||||
.max(100)
|
||||
.optional(),
|
||||
last_name: Joi.string()
|
||||
.min(1)
|
||||
.max(100)
|
||||
.optional(),
|
||||
phone: Joi.string()
|
||||
.min(10)
|
||||
.max(15)
|
||||
.optional(),
|
||||
avatar_url: Joi.string().optional(),
|
||||
birthday: Joi.date().optional()
|
||||
});
|
||||
|
||||
export const changePasswordSchema = Joi.object({
|
||||
old_password: Joi.string()
|
||||
.required()
|
||||
.messages({
|
||||
'any.required': 'Old password is required'
|
||||
}),
|
||||
new_password: Joi.string()
|
||||
.required()
|
||||
.messages({
|
||||
'any.required': 'New password is required'
|
||||
})
|
||||
});
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue