Update access login and register to supply required data to the client
parent
7c8bb78f56
commit
c97e1b01a9
|
|
@ -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
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue