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();