Update access login and register to supply required data to the client

main
MiguelMLorente 2025-11-23 15:48:51 +01:00
parent 7c8bb78f56
commit c97e1b01a9
2 changed files with 15 additions and 6 deletions

View File

@ -8,17 +8,23 @@ export class AccessController {
constructor(private readonly userService: UserService) {} constructor(private readonly userService: UserService) {}
@Post('/login') @Post('/login')
public login(name: string, password: string): void { public login(name: string, password: string): {userId: number} {
this.logger.debug("Received login request"); this.logger.debug("Received login request");
const user = this.userService.getUserByName(name); const user = this.userService.getUserByName(name);
if (user.password !== password) { if (user.password !== password) {
throw new UnauthorizedException(); throw new UnauthorizedException();
} }
return {
userId: user.id
};
} }
@Post('/register') @Post('/register')
public new(name: string, password: string): void { public new(name: string, password: string): {userId: number} {
this.logger.debug("Received register request"); this.logger.debug("Received register request");
this.userService.createUser(name, password); const user = this.userService.createUser(name, password);
return {
userId: user.id
};
} }
} }

View File

@ -23,17 +23,20 @@ export class UserService {
return user; return user;
} }
public createUser(name: string, password: string): void { public createUser(name: string, password: string): User {
try { try {
this.getUserByName(name); this.getUserByName(name);
throw new NotFoundException(); throw new NotFoundException();
} catch (e) { } catch (e) {
if (!(e instanceof NotFoundException)) throw e; if (!(e instanceof NotFoundException)) throw e;
} }
this.users.push({
const user: User = {
id: this.users.length, id: this.users.length,
name, name,
password, password,
}); };
this.users.push(user);
return user;
} }
} }