32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { Body, Controller, HttpCode, HttpStatus, Post, UseGuards } from '@nestjs/common';
|
|
import { ApiBearerAuth } from '@nestjs/swagger';
|
|
import { AccountsEntity } from '../database/connections/ebitemp-api/entities';
|
|
import { LoginDto, LoginResDto } from './auth.dto';
|
|
import { AuthService } from './auth.service';
|
|
import { AuthenticatedUser } from './authenticated-user.decorator';
|
|
import { Public } from './strategies/jwt/jwt-auth.guard';
|
|
import { JwtRefreshGuard } from './strategies/jwt/jwt-refresh.guard';
|
|
|
|
@ApiBearerAuth()
|
|
@Controller('auth')
|
|
export class AuthController {
|
|
constructor(private readonly authService: AuthService) {}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Public()
|
|
@Post('login')
|
|
async logIn(@Body() body: LoginDto): Promise<LoginResDto> {
|
|
const { username, password } = body;
|
|
const user = await this.authService.getAuthenticatedUser(username, password);
|
|
return await this.authService.signJwts(user);
|
|
}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@UseGuards(JwtRefreshGuard)
|
|
@Public(true)
|
|
@Post('refresh')
|
|
async refresh(@AuthenticatedUser() user: AccountsEntity): Promise<LoginResDto> {
|
|
return await this.authService.signJwts(user);
|
|
}
|
|
}
|