PaysplitAPI/src/controller/access.controller.ts

43 lines
1000 B
TypeScript

import {
Body,
Controller,
Logger,
Post,
UnauthorizedException,
} from '@nestjs/common';
import { UserService } from '../service/user.service';
@Controller('/access')
export class AccessController {
private readonly logger = new Logger(AccessController.name);
constructor(private readonly userService: UserService) {}
@Post('/login')
public login(
@Body('name') name: string,
@Body('password') 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(
@Body('name') name: string,
@Body('password') password: string,
): { userId: number } {
this.logger.debug('Received register request');
const user = this.userService.createUser(name, password);
return {
userId: user.id,
};
}
}