Compare commits
No commits in common. "0fca8f38313f2d45f3ee9acb851cf1e80ae66c9d" and "10e481669e40f25d9149ff4ca112d4dd8fd3950a" have entirely different histories.
0fca8f3831
...
10e481669e
|
@ -4,11 +4,7 @@ import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
||||||
import { ValidationPipe } from '@nestjs/common';
|
import { ValidationPipe } from '@nestjs/common';
|
||||||
|
|
||||||
async function setupSwagger(app) {
|
async function setupSwagger(app) {
|
||||||
const config = new DocumentBuilder()
|
const config = new DocumentBuilder().build();
|
||||||
.setTitle('Tickets API')
|
|
||||||
.setDescription('Description of the API')
|
|
||||||
.setVersion('0.0.0')
|
|
||||||
.build();
|
|
||||||
const document = SwaggerModule.createDocument(app, config);
|
const document = SwaggerModule.createDocument(app, config);
|
||||||
SwaggerModule.setup('api', app, document);
|
SwaggerModule.setup('api', app, document);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,56 +7,39 @@ import {
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { AuthService } from '../services/auth.service';
|
import { AuthService } from '../services/auth.service';
|
||||||
import { TokensDto, UserCredentialsDto } from '../models/dto';
|
import { UserCredentialsDto } from '../models/dto';
|
||||||
|
import { Tokens } from '../models/types';
|
||||||
import { RefreshTokenGuard } from '../common/guards';
|
import { RefreshTokenGuard } from '../common/guards';
|
||||||
import { GetCurrentUser, GetCurrentUserId, Public } from '../common/decorators';
|
import { GetCurrentUser, GetCurrentUserId, Public } from '../common/decorators';
|
||||||
import { ApiCreatedResponse, ApiTags } from '@nestjs/swagger';
|
|
||||||
|
|
||||||
@ApiTags('Authentication')
|
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
export class AuthController {
|
export class AuthController {
|
||||||
constructor(private readonly authService: AuthService) {}
|
constructor(private readonly authService: AuthService) {}
|
||||||
|
|
||||||
@ApiCreatedResponse({
|
|
||||||
description: 'User signed up successfully',
|
|
||||||
type: TokensDto,
|
|
||||||
})
|
|
||||||
@Public()
|
@Public()
|
||||||
@Post('signup')
|
@Post('signup')
|
||||||
@HttpCode(HttpStatus.CREATED)
|
@HttpCode(HttpStatus.CREATED)
|
||||||
public async signup(
|
public async signup(
|
||||||
@Body() userCredentials: UserCredentialsDto
|
@Body() userCredentials: UserCredentialsDto
|
||||||
): Promise<TokensDto> {
|
): Promise<Tokens> {
|
||||||
return this.authService.signup(userCredentials);
|
return this.authService.signup(userCredentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiCreatedResponse({
|
|
||||||
description: 'User signin successfully',
|
|
||||||
type: TokensDto,
|
|
||||||
})
|
|
||||||
@Public()
|
@Public()
|
||||||
@Post('signin')
|
@Post('signin')
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
public async signin(
|
public async signin(
|
||||||
@Body() userCredentials: UserCredentialsDto
|
@Body() userCredentials: UserCredentialsDto
|
||||||
): Promise<TokensDto> {
|
): Promise<Tokens> {
|
||||||
return this.authService.signin(userCredentials);
|
return this.authService.signin(userCredentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiCreatedResponse({
|
|
||||||
description: 'User signed out successfully',
|
|
||||||
type: Boolean,
|
|
||||||
})
|
|
||||||
@Post('logout')
|
@Post('logout')
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
public async logout(@GetCurrentUserId() userId: number): Promise<boolean> {
|
public async logout(@GetCurrentUserId() userId: number): Promise<boolean> {
|
||||||
return this.authService.logout(userId);
|
return this.authService.logout(userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiCreatedResponse({
|
|
||||||
description: 'User tokens refreshed successfully',
|
|
||||||
type: TokensDto,
|
|
||||||
})
|
|
||||||
@Public()
|
@Public()
|
||||||
@UseGuards(RefreshTokenGuard)
|
@UseGuards(RefreshTokenGuard)
|
||||||
@Post('refresh')
|
@Post('refresh')
|
||||||
|
@ -64,7 +47,7 @@ export class AuthController {
|
||||||
public async refresh(
|
public async refresh(
|
||||||
@GetCurrentUserId() userId: number,
|
@GetCurrentUserId() userId: number,
|
||||||
@GetCurrentUser('refresh_token') refresh_token: string
|
@GetCurrentUser('refresh_token') refresh_token: string
|
||||||
): Promise<TokensDto> {
|
): Promise<Tokens> {
|
||||||
return this.authService.refresh(userId, refresh_token);
|
return this.authService.refresh(userId, refresh_token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1 @@
|
||||||
export * from './user-credentials.dto';
|
export * from './user-credentials.dto';
|
||||||
export * from './tokens.dto';
|
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
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;
|
|
||||||
}
|
|
|
@ -1,22 +1,10 @@
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
|
||||||
import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator';
|
import { IsEmail, IsNotEmpty, IsString, MinLength } from 'class-validator';
|
||||||
|
|
||||||
export class UserCredentialsDto {
|
export class UserCredentialsDto {
|
||||||
@ApiProperty({
|
|
||||||
title: 'E-Mail',
|
|
||||||
description: 'User email',
|
|
||||||
example: 'foo@bar.com',
|
|
||||||
})
|
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsEmail()
|
@IsEmail()
|
||||||
public email: string;
|
public email: string;
|
||||||
|
|
||||||
@ApiProperty({
|
|
||||||
title: 'Password',
|
|
||||||
description: 'User password',
|
|
||||||
example: '$tr0ngP@$$w0rd',
|
|
||||||
minLength: 8,
|
|
||||||
})
|
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsString()
|
@IsString()
|
||||||
@MinLength(8)
|
@MinLength(8)
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
|
export * from './tokens.type';
|
||||||
export * from './jwt-payload.type';
|
export * from './jwt-payload.type';
|
||||||
export * from './jwt-payload-with-refresh-token.type';
|
export * from './jwt-payload-with-refresh-token.type';
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
export type Tokens = {
|
||||||
|
access_token: string;
|
||||||
|
refresh_token: string;
|
||||||
|
};
|
|
@ -1,5 +1,6 @@
|
||||||
import { ForbiddenException, Injectable } from '@nestjs/common';
|
import { ForbiddenException, Injectable } from '@nestjs/common';
|
||||||
import { TokensDto, UserCredentialsDto } from '../models/dto';
|
import { UserCredentialsDto } from '../models/dto';
|
||||||
|
import { Tokens } from '../models/types';
|
||||||
import { EncryptionService } from './encryption.service';
|
import { EncryptionService } from './encryption.service';
|
||||||
import { UserRepository } from '../repositories/user.repository';
|
import { UserRepository } from '../repositories/user.repository';
|
||||||
import { TokenManagementService } from './token-management.service';
|
import { TokenManagementService } from './token-management.service';
|
||||||
|
@ -12,7 +13,7 @@ export class AuthService {
|
||||||
private readonly encryptionService: EncryptionService
|
private readonly encryptionService: EncryptionService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public async signup(userCredentials: UserCredentialsDto): Promise<TokensDto> {
|
public async signup(userCredentials: UserCredentialsDto): Promise<Tokens> {
|
||||||
const passwordHashed = await this.encryptionService.hashData(
|
const passwordHashed = await this.encryptionService.hashData(
|
||||||
userCredentials.password
|
userCredentials.password
|
||||||
);
|
);
|
||||||
|
@ -23,7 +24,7 @@ export class AuthService {
|
||||||
return this.generateAndPersistTokens(user.id, user.email);
|
return this.generateAndPersistTokens(user.id, user.email);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async signin(userCredentials: UserCredentialsDto): Promise<TokensDto> {
|
public async signin(userCredentials: UserCredentialsDto): Promise<Tokens> {
|
||||||
const user = await this.userRepository.findUserByEmail(
|
const user = await this.userRepository.findUserByEmail(
|
||||||
userCredentials.email
|
userCredentials.email
|
||||||
);
|
);
|
||||||
|
@ -42,10 +43,7 @@ export class AuthService {
|
||||||
return this.generateAndPersistTokens(user.id, user.email);
|
return this.generateAndPersistTokens(user.id, user.email);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async refresh(
|
public async refresh(userId: number, refreshToken: string): Promise<Tokens> {
|
||||||
userId: number,
|
|
||||||
refreshToken: string
|
|
||||||
): Promise<TokensDto> {
|
|
||||||
const user = await this.userRepository.findUserById(userId);
|
const user = await this.userRepository.findUserById(userId);
|
||||||
if (!user || !user.hashedRt) {
|
if (!user || !user.hashedRt) {
|
||||||
throw new ForbiddenException('Access Denied');
|
throw new ForbiddenException('Access Denied');
|
||||||
|
@ -73,7 +71,7 @@ export class AuthService {
|
||||||
private async generateAndPersistTokens(
|
private async generateAndPersistTokens(
|
||||||
userId: number,
|
userId: number,
|
||||||
email: string
|
email: string
|
||||||
): Promise<TokensDto> {
|
): Promise<Tokens> {
|
||||||
const tokens = await this.tokenManagementService.generateTokens(
|
const tokens = await this.tokenManagementService.generateTokens(
|
||||||
userId,
|
userId,
|
||||||
email
|
email
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
|
import { Tokens } from '../models/types';
|
||||||
import { JwtService } from '@nestjs/jwt';
|
import { JwtService } from '@nestjs/jwt';
|
||||||
import { TokensDto } from '../models/dto';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TokenManagementService {
|
export class TokenManagementService {
|
||||||
|
@ -24,10 +24,7 @@ export class TokenManagementService {
|
||||||
this.JWT_SECRET_RT = this.configService.get<string>('JWT_SECRET_RT');
|
this.JWT_SECRET_RT = this.configService.get<string>('JWT_SECRET_RT');
|
||||||
}
|
}
|
||||||
|
|
||||||
public async generateTokens(
|
public async generateTokens(userId: number, email: string): Promise<Tokens> {
|
||||||
userId: number,
|
|
||||||
email: string
|
|
||||||
): Promise<TokensDto> {
|
|
||||||
const access_token: string = await this.createAccessToken(userId, email);
|
const access_token: string = await this.createAccessToken(userId, email);
|
||||||
const refresh_token: string = await this.createRefreshToken(userId, email);
|
const refresh_token: string = await this.createRefreshToken(userId, email);
|
||||||
return { access_token, refresh_token };
|
return { access_token, refresh_token };
|
||||||
|
|
Loading…
Reference in New Issue