30 lines
813 B
TypeScript
30 lines
813 B
TypeScript
import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common';
|
|
import * as bcrypt from 'bcrypt';
|
|
import { Request } from 'express';
|
|
import { CreateUserDto } from 'src/user/dtos/CreateUserDto';
|
|
import { UserService } from 'src/user/user.service';
|
|
import { AuthService } from './auth.service';
|
|
import { LocalAuthGuard } from './local-auth.guard';
|
|
|
|
@Controller('auth')
|
|
export class AuthController {
|
|
|
|
constructor(
|
|
private userService: UserService,
|
|
private authService: AuthService
|
|
) {}
|
|
|
|
@UseGuards(LocalAuthGuard)
|
|
@Post('login')
|
|
async login(@Req() req: Request) {
|
|
return this.authService.authenticate(req.user);
|
|
}
|
|
|
|
@Post('register')
|
|
async register(@Body() user: CreateUserDto) {
|
|
user.password = await bcrypt.hash(user.password, 10);
|
|
this.userService.create(user);
|
|
}
|
|
|
|
}
|