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) {}
|
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
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue