58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import {
|
|
Body,
|
|
Controller,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Post,
|
|
Res,
|
|
} from '@nestjs/common';
|
|
import { AuthService } from './auth.service';
|
|
import { ApiOperation } from '@nestjs/swagger';
|
|
import {
|
|
LoginUserRequestDTO,
|
|
LoginUserResponseDTO,
|
|
RegisterUserRequestDTO,
|
|
} from './dto';
|
|
import { Response } from 'express';
|
|
import { DataResponse } from 'common/http';
|
|
import { Public } from './decorators';
|
|
|
|
@Controller('auth')
|
|
@Public()
|
|
export class AuthController {
|
|
constructor(private readonly authService: AuthService) {}
|
|
|
|
@ApiOperation({ summary: 'User login' })
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('/login')
|
|
async login(
|
|
@Body() body: LoginUserRequestDTO,
|
|
@Res({ passthrough: true }) response: Response,
|
|
): Promise<DataResponse<LoginUserResponseDTO>> {
|
|
const { accessToken, refreshToken, user } =
|
|
await this.authService.login(body);
|
|
|
|
response.cookie('accessToken', accessToken);
|
|
|
|
return new DataResponse(
|
|
new LoginUserResponseDTO(user, accessToken, refreshToken),
|
|
'Login successfull',
|
|
);
|
|
}
|
|
|
|
@ApiOperation({ summary: 'User register' })
|
|
@HttpCode(HttpStatus.CREATED)
|
|
@Post('/register')
|
|
async register(@Body() body: RegisterUserRequestDTO): Promise<string> {
|
|
await this.authService.register(body);
|
|
|
|
return 'Registered successfully. Login to continue.';
|
|
}
|
|
|
|
logout() {}
|
|
|
|
forgotPassword() {}
|
|
|
|
regenTokens() {}
|
|
}
|