feat: Basic setup with auth
This commit is contained in:
@@ -1,4 +1,55 @@
|
||||
import { Controller } from '@nestjs/common';
|
||||
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';
|
||||
|
||||
@Controller('auth')
|
||||
export class AuthController {}
|
||||
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() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user