Compare commits

...

3 Commits

9 changed files with 75 additions and 19 deletions

View File

@ -4,7 +4,11 @@ import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
async function setupSwagger(app) {
const config = new DocumentBuilder().build();
const config = new DocumentBuilder()
.setTitle('Tickets API')
.setDescription('Description of the API')
.setVersion('0.0.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
}

View File

@ -7,39 +7,56 @@ import {
UseGuards,
} from '@nestjs/common';
import { AuthService } from '../services/auth.service';
import { UserCredentialsDto } from '../models/dto';
import { Tokens } from '../models/types';
import { TokensDto, UserCredentialsDto } from '../models/dto';
import { RefreshTokenGuard } from '../common/guards';
import { GetCurrentUser, GetCurrentUserId, Public } from '../common/decorators';
import { ApiCreatedResponse, ApiTags } from '@nestjs/swagger';
@ApiTags('Authentication')
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) {}
@ApiCreatedResponse({
description: 'User signed up successfully',
type: TokensDto,
})
@Public()
@Post('signup')
@HttpCode(HttpStatus.CREATED)
public async signup(
@Body() userCredentials: UserCredentialsDto
): Promise<Tokens> {
): Promise<TokensDto> {
return this.authService.signup(userCredentials);
}
@ApiCreatedResponse({
description: 'User signin successfully',
type: TokensDto,
})
@Public()
@Post('signin')
@HttpCode(HttpStatus.OK)
public async signin(
@Body() userCredentials: UserCredentialsDto
): Promise<Tokens> {
): Promise<TokensDto> {
return this.authService.signin(userCredentials);
}
@ApiCreatedResponse({
description: 'User signed out successfully',
type: Boolean,
})
@Post('logout')
@HttpCode(HttpStatus.OK)
public async logout(@GetCurrentUserId() userId: number): Promise<boolean> {
return this.authService.logout(userId);
}
@ApiCreatedResponse({
description: 'User tokens refreshed successfully',
type: TokensDto,
})
@Public()
@UseGuards(RefreshTokenGuard)
@Post('refresh')
@ -47,7 +64,7 @@ export class AuthController {
public async refresh(
@GetCurrentUserId() userId: number,
@GetCurrentUser('refresh_token') refresh_token: string
): Promise<Tokens> {
): Promise<TokensDto> {
return this.authService.refresh(userId, refresh_token);
}
}

View File

@ -1 +1,2 @@
export * from './user-credentials.dto';
export * from './tokens.dto';

View File

@ -0,0 +1,22 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class TokensDto {
@ApiProperty({
title: 'Access token',
description: 'Access token',
example: 'eyJhbGci',
})
@IsNotEmpty()
@IsString()
public access_token: string;
@ApiProperty({
title: 'Refresh token',
description: 'Refresh token',
example: 'eyJhbGci',
})
@IsNotEmpty()
@IsString()
public refresh_token: string;
}

View File

@ -1,10 +1,22 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator';
export class UserCredentialsDto {
@ApiProperty({
title: 'E-Mail',
description: 'User email',
example: 'foo@bar.com',
})
@IsNotEmpty()
@IsEmail()
public email: string;
@ApiProperty({
title: 'Password',
description: 'User password',
example: '$tr0ngP@$$w0rd',
minLength: 8,
})
@IsNotEmpty()
@IsString()
@MinLength(8)

View File

@ -1,3 +1,2 @@
export * from './tokens.type';
export * from './jwt-payload.type';
export * from './jwt-payload-with-refresh-token.type';

View File

@ -1,4 +0,0 @@
export type Tokens = {
access_token: string;
refresh_token: string;
};

View File

@ -1,6 +1,5 @@
import { ForbiddenException, Injectable } from '@nestjs/common';
import { UserCredentialsDto } from '../models/dto';
import { Tokens } from '../models/types';
import { TokensDto, UserCredentialsDto } from '../models/dto';
import { EncryptionService } from './encryption.service';
import { UserRepository } from '../repositories/user.repository';
import { TokenManagementService } from './token-management.service';
@ -13,7 +12,7 @@ export class AuthService {
private readonly encryptionService: EncryptionService
) {}
public async signup(userCredentials: UserCredentialsDto): Promise<Tokens> {
public async signup(userCredentials: UserCredentialsDto): Promise<TokensDto> {
const passwordHashed = await this.encryptionService.hashData(
userCredentials.password
);
@ -24,7 +23,7 @@ export class AuthService {
return this.generateAndPersistTokens(user.id, user.email);
}
public async signin(userCredentials: UserCredentialsDto): Promise<Tokens> {
public async signin(userCredentials: UserCredentialsDto): Promise<TokensDto> {
const user = await this.userRepository.findUserByEmail(
userCredentials.email
);
@ -43,7 +42,10 @@ export class AuthService {
return this.generateAndPersistTokens(user.id, user.email);
}
public async refresh(userId: number, refreshToken: string): Promise<Tokens> {
public async refresh(
userId: number,
refreshToken: string
): Promise<TokensDto> {
const user = await this.userRepository.findUserById(userId);
if (!user || !user.hashedRt) {
throw new ForbiddenException('Access Denied');
@ -71,7 +73,7 @@ export class AuthService {
private async generateAndPersistTokens(
userId: number,
email: string
): Promise<Tokens> {
): Promise<TokensDto> {
const tokens = await this.tokenManagementService.generateTokens(
userId,
email

View File

@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Tokens } from '../models/types';
import { JwtService } from '@nestjs/jwt';
import { TokensDto } from '../models/dto';
@Injectable()
export class TokenManagementService {
@ -24,7 +24,10 @@ export class TokenManagementService {
this.JWT_SECRET_RT = this.configService.get<string>('JWT_SECRET_RT');
}
public async generateTokens(userId: number, email: string): Promise<Tokens> {
public async generateTokens(
userId: number,
email: string
): Promise<TokensDto> {
const access_token: string = await this.createAccessToken(userId, email);
const refresh_token: string = await this.createRefreshToken(userId, email);
return { access_token, refresh_token };