File

src/modules/auth/controllers/auth.controller.ts

Prefix

Auth

Index

Methods

Methods

Async userLogin
userLogin(userLoginDto: UserLoginDto)
Decorators :
@Post('login')
@HttpCode(HttpStatus.OK)
@ApiOkResponse({status: undefined, type: LoginPayloadDto, description: 'User info with access token'})
Parameters :
Name Type Optional
userLoginDto UserLoginDto No
Async userLogout
userLogout(user: UserEntity)
Decorators :
@Patch('logout')
@HttpCode(HttpStatus.NO_CONTENT)
@ApiNoContentResponse({description: 'Successfully Logout'})
@UseGuards(AuthGuard, RolesGuard)
@UseInterceptors(AuthUserInterceptor)
@ApiBearerAuth()
@Roles(RoleType.USER, RoleType.ADMIN, RoleType.ROOT)
Parameters :
Name Type Optional
user UserEntity No
Returns : Promise<void>
Async userRegister
userRegister(userRegisterDto: UserRegisterDto, authUser: UserEntity)
Decorators :
@Post('register')
@UseGuards(AuthGuard, RolesGuard)
@ApiBearerAuth()
@HttpCode(HttpStatus.OK)
@Roles(RoleType.ADMIN, RoleType.ROOT, RoleType.P_ADMIN)
@ApiOkResponse({status: undefined, type: UserDto, description: 'Successfully Registered'})
Parameters :
Name Type Optional
userRegisterDto UserRegisterDto No
authUser UserEntity No
Returns : Promise<UserDto>
import {
  Body,
  Controller,
  HttpCode,
  HttpStatus,
  Patch,
  Post,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiNoContentResponse,
  ApiOkResponse,
  ApiTags,
} from '@nestjs/swagger';
import { RoleType } from 'common/constants';
import { AuthUser, Roles } from 'decorators';
import { AuthGuard, RolesGuard } from 'guards';
import { AuthUserInterceptor } from 'interceptors';
import {
  LoginPayloadDto,
  UserLoginDto,
  UserRegisterDto,
} from 'modules/auth/dtos';
import { AuthService } from 'modules/auth/services';
import { UserDto } from 'modules/user/dtos';
import { UserEntity } from 'modules/user/entities';
import { UserAuthService, UserService } from 'modules/user/services';
import { PartnerService } from 'modules/partner/services';

@Controller('Auth')
@ApiTags('Auth')
export class AuthController {
  constructor(
    private readonly _userService: UserService,
    private readonly _userAuthService: UserAuthService,
    private readonly _authService: AuthService,
    private readonly _partnerService: PartnerService,
  ) {}

  @Post('login')
  @HttpCode(HttpStatus.OK)
  @ApiOkResponse({
    status: HttpStatus.OK,
    type: LoginPayloadDto,
    description: 'User info with access token',
  })
  async userLogin(
    @Body() userLoginDto: UserLoginDto,
  ): Promise<LoginPayloadDto> {
    const user = await this._authService.validateUser(userLoginDto);
    const token = await this._authService.createToken(user);

    return new LoginPayloadDto(user.toDto(), token);
  }

  @Post('register')
  @UseGuards(AuthGuard, RolesGuard)
  @ApiBearerAuth()
  @HttpCode(HttpStatus.OK)
  @Roles(RoleType.ADMIN, RoleType.ROOT, RoleType.P_ADMIN)
  @ApiOkResponse({
    status: HttpStatus.OK,
    type: UserDto,
    description: 'Successfully Registered',
  })
  async userRegister(
    @Body() userRegisterDto: UserRegisterDto,
    @AuthUser() authUser: UserEntity,
  ): Promise<UserDto> {
    const {firstName, lastName, email, password, role} = userRegisterDto

    if (userRegisterDto.partner && (authUser.userAuth.role === RoleType.ADMIN || authUser.userAuth.role === RoleType.ROOT)) {
      const partner = await this._partnerService.getPartnerEntity({ uuid: userRegisterDto.partner})
      const user = await this._userService.createUser({firstName, lastName, email, partner, password}, authUser, role);
      return (await this._userService.getUser({uuid : user.uuid})).toDto();
    } else {
      const user = await this._userService.createUser({firstName, lastName, email, partner: authUser.partner, password}, authUser, role);
      return (await this._userService.getUser({uuid : user.uuid})).toDto();
    }
  }

  @Patch('logout')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiNoContentResponse({
    description: 'Successfully Logout',
  })
  @UseGuards(AuthGuard, RolesGuard)
  @UseInterceptors(AuthUserInterceptor)
  @ApiBearerAuth()
  @Roles(RoleType.USER, RoleType.ADMIN, RoleType.ROOT)
  async userLogout(@AuthUser() user: UserEntity): Promise<void> {
    await this._userAuthService.updateLastLogoutDate(user.userAuth);
  }
}

result-matching ""

    No results matching ""