generator-nest-js-boilerplate
Version:
This generator will help you to build your own Nest.js Mongodb API using TypeScript 4
379 lines (354 loc) • 9.28 kB
text/typescript
import {
Body,
Controller,
HttpCode,
Get,
Post,
Delete,
Put,
Param,
Request,
UnauthorizedException,
UseGuards,
NotFoundException,
ForbiddenException,
HttpStatus,
UseInterceptors,
} from '@nestjs/common';
import {
ApiTags,
ApiBody,
ApiOkResponse,
ApiInternalServerErrorResponse,
ApiUnauthorizedResponse,
ApiBearerAuth,
ApiNotFoundResponse,
ApiBadRequestResponse,
ApiConflictResponse,
ApiNoContentResponse,
ApiExtraModels,
getSchemaPath,
} from '@nestjs/swagger';
import { JwtService } from '@nestjs/jwt';
import { Request as ExpressRequest } from 'express';
import { ConfigService } from '@nestjs/config';
import UsersService from '@v1/users/users.service';
import JwtAccessGuard from '@guards/jwt-access.guard';
import WrapResponseInterceptor from '@interceptors/wrap-response.interceptor';
import AuthBearer from '@decorators/auth-bearer.decorator';
import { LoginPayload } from '@v1/auth/interfaces/login-payload.interface';
import { UserEntity } from '@prisma/client';
import { UserResponseEntity } from '@v1/users/entities/user-response.entity';
import { DecodedUser } from './interfaces/decoded-user.interface';
import LocalAuthGuard from './guards/local-auth.guard';
import AuthService from './auth.service';
import RefreshTokenDto from './dto/refresh-token.dto';
import SignInDto from './dto/sign-in.dto';
import SignUpDto from './dto/sign-up.dto';
import VerifyUserDto from './dto/verify-user.dto';
import JwtTokensDto from './dto/jwt-tokens.dto';
import RolesGuard from '@guards/roles.guard';
import { Roles, RolesEnum } from '@decorators/roles.decorator';
export default class AuthController {
constructor(
private readonly authService: AuthService,
private readonly jwtService: JwtService,
private readonly usersService: UsersService,
private readonly configService: ConfigService,
) {}
async signIn( req: ExpressRequest): Promise<JwtTokensDto> {
const user = req.user as LoginPayload;
return this.authService.login(user);
}
async signUp( user: SignUpDto): Promise<UserEntity> {
return this.usersService.create(user);
}
async refreshToken(
refreshTokenDto: RefreshTokenDto,
): Promise<JwtTokensDto | never> {
const decodedUser = this.jwtService.decode(
refreshTokenDto.refreshToken,
) as DecodedUser;
if (!decodedUser) {
throw new ForbiddenException('Incorrect token');
}
const oldRefreshToken:
| string
| null = await this.authService.getRefreshTokenByEmail(decodedUser.email);
// if the old refresh token is not equal to request refresh token then this user is unauthorized
if (!oldRefreshToken || oldRefreshToken !== refreshTokenDto.refreshToken) {
throw new UnauthorizedException(
'Authentication credentials were missing or incorrect',
);
}
const payload = {
id: decodedUser.id,
email: decodedUser.email,
roles: decodedUser.roles,
};
return this.authService.login(payload);
}
async verifyUser( verifyUserDto: VerifyUserDto): Promise<{} | never> {
const foundUser = await this.usersService.getUnverifiedUserByEmail(
verifyUserDto.email,
);
if (!foundUser) {
throw new NotFoundException('The user does not exist');
}
return this.usersService.update(foundUser.id, { verified: true });
}
async logout( token: string): Promise<{} | never> {
const decodedUser: DecodedUser | null = await this.authService.verifyToken(
token,
this.configService.get<string>('ACCESS_TOKEN') || '283f01ccce922bcc2399e7f8ded981285963cec349daba382eb633c1b3a5f282',
);
if (!decodedUser) {
throw new ForbiddenException('Incorrect token');
}
const deletedUsersCount = await this.authService.deleteTokenByEmail(
decodedUser.email,
);
if (deletedUsersCount === 0) {
throw new NotFoundException();
}
return {};
}
async logoutAll(): Promise<{}> {
return this.authService.deleteAllTokens();
}
async getUserByAccessToken(
token: string,
): Promise<DecodedUser | never> {
const decodedUser: DecodedUser | null = await this.authService.verifyToken(
token,
this.configService.get<string>('ACCESS_TOKEN') || '283f01ccce922bcc2399e7f8ded981285963cec349daba382eb633c1b3a5f282',
);
if (!decodedUser) {
throw new ForbiddenException('Incorrect token');
}
const { exp, iat, ...user } = decodedUser;
return user;
}
}