add me point to load user data of current user

This commit is contained in:
Anika Raemer 2025-09-27 07:47:26 +02:00
parent fac606cf97
commit e5b5d7e67d
12 changed files with 215 additions and 67 deletions

View file

@ -5,12 +5,21 @@ import { UserDtoEntityMapper } from "../mappers/UserDtoEntityMapper.js";
import { LoginResponseDto } from "../dtos/LoginResponseDto.js";
import { LoginRequestDto } from "../dtos/LoginRequestDto.js";
/**
* Controller responsible for authentication, e.g., login or issueing a token with extended
* lifetime
*/
export class AuthController {
constructor(
private userRepository: UserRepository,
private mapper: UserDtoEntityMapper
) {}
/**
* Login: Check user and password and generate token
* @param loginRequest LoginRequestDto containing userName and password for login
* @returns LoginResponse containing token and user data for the user who just logged in
*/
async login(loginRequest : LoginRequestDto): Promise<LoginResponseDto> {
const userName :string|undefined = loginRequest.userName;
const password :string|undefined = loginRequest.password;
@ -25,6 +34,11 @@ export class AuthController {
if(!user){
throw new UnauthorizedError("Invalid username or password");
}
// ensure user has an id - required to generate token
const userId = user.id;
if(user.id == undefined){
throw new UnauthorizedError("Invalid username or password");
}
// Compare password
const passwordMatches = encrypt.comparepassword(password, user.password);
if (!passwordMatches) {
@ -33,7 +47,7 @@ export class AuthController {
// Create JWT
const tokenInfo = encrypt.generateToken({
id: user.id,
id: userId!, // ! to indicate that we've definitely checked for userId being defined
});
const responseDto = new LoginResponseDto();

View file

@ -1,18 +1,26 @@
import { ValidationError, ConflictError } from "../errors/httpErrors.js";
import { ValidationError, ConflictError, NotFoundError } from "../errors/httpErrors.js";
import { CreateUserRequestDto } from "../dtos/CreateUserRequestDto.js";
import { UserDto } from "../dtos/UserDto.js";
import { encrypt } from "../utils/encryptionUtils.js";
import { UserRepository } from "../repositories/UserRepository.js";
import { UserDtoEntityMapper } from "../mappers/UserDtoEntityMapper.js";
import { UUID } from "crypto";
/**
* Controls all user specific actions
*/
export class UserController {
constructor(
private userRepository: UserRepository,
private mapper: UserDtoEntityMapper
) {}
/**
* Create a new user
* @param dto CreateUserRequestDto containing data for the user to add
* @returns UserDto Data of the user as stored in the database
*/
async createUser(dto: CreateUserRequestDto): Promise<UserDto> {
// @todo make authorized! Create initial user!
// check mandatory fields
if(!dto.userData){
throw new ValidationError("User data is required")
@ -43,4 +51,21 @@ export class UserController {
return this.mapper.toDto(savedUser);
}
/**
* Load data of a specific user
* @param userId Id of user to load
* @returns UserDto containing the user's data
*/
async getUserById(userId: UUID|string|undefined): Promise<UserDto> {
if(!userId){
throw new ValidationError("userId is required");
}
const userEntity = await this.userRepository.findById(userId);
if(!userEntity){
throw new NotFoundError("user with id" + userId + "not found!")
}
return this.mapper.toDto(userEntity);
}
}