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) {}
@Post('/login')
public login(name: string, password: string): void {
public login(name: string, password: string): {userId: number} {
this.logger.debug("Received login request");
const user = this.userService.getUserByName(name);
if (user.password !== password) {
throw new UnauthorizedException();
}
return {
userId: user.id
};
}
@Post('/register')
public new(name: string, password: string): void {
public new(name: string, password: string): {userId: number} {
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;
}
public createUser(name: string, password: string): void {
public createUser(name: string, password: string): User {
try {
this.getUserByName(name);
throw new NotFoundException();
} catch (e) {
if (!(e instanceof NotFoundException)) throw e;
}
this.users.push({
const user: User = {
id: this.users.length,
name,
password,
});
};
this.users.push(user);
return user;
}
}