improve login and add migration script

This commit is contained in:
Anika Raemer 2025-09-21 20:01:33 +02:00
parent 1fce467571
commit 099ffb74a1
5 changed files with 106 additions and 9 deletions

View file

@ -1,8 +1,9 @@
import { UserRepository } from "../repositories/UserRepository";
import { encrypt } from "../utils/encryptionUtils";
import { ValidationError, UnauthorizedError } from "../errors/httpErrors";
import { UserDto } from "../dtos/UserDto";
import { UserDtoEntityMapper } from "../mappers/UserDtoEntityMapper";
import { LoginResponseDto } from "../dtos/LoginResponseDto";
import { LoginRequestDto } from "../dtos/LoginRequestDto";
export class AuthController {
constructor(
@ -10,7 +11,9 @@ export class AuthController {
private mapper: UserDtoEntityMapper
) {}
async login(userName: string, password: string): Promise<{ token: string; user: UserDto }> {
async login(loginRequest : LoginRequestDto): Promise<LoginResponseDto> {
const userName :string = loginRequest.userName;
const password :string = loginRequest.password;
if (!userName || !password) {
throw new ValidationError("Username and password are required");
}
@ -30,9 +33,9 @@ export class AuthController {
role: user.role,
});
return {
token,
user: this.mapper.toDto(user),
};
const responseDto = new LoginResponseDto();
responseDto.userData = this.mapper.toDto(user);
responseDto.token = token;
return responseDto;
}
}