Compare commits
3 Commits
10e481669e
...
0fca8f3831
Author | SHA1 | Date |
---|---|---|
Igor Hrenowitsch Propisnov | 0fca8f3831 | |
Igor Hrenowitsch Propisnov | e6f3e8f6be | |
Igor Hrenowitsch Propisnov | 2549dc9077 |
|
@ -4,7 +4,11 @@ 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().build();
|
const config = new DocumentBuilder()
|
||||||
|
.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,39 +7,56 @@ import {
|
||||||
UseGuards,
|
UseGuards,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { AuthService } from '../services/auth.service';
|
import { AuthService } from '../services/auth.service';
|
||||||
import { UserCredentialsDto } from '../models/dto';
|
import { TokensDto, 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<Tokens> {
|
): Promise<TokensDto> {
|
||||||
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<Tokens> {
|
): Promise<TokensDto> {
|
||||||
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')
|
||||||
|
@ -47,7 +64,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<Tokens> {
|
): Promise<TokensDto> {
|
||||||
return this.authService.refresh(userId, refresh_token);
|
return this.authService.refresh(userId, refresh_token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
export * from './user-credentials.dto';
|
export * from './user-credentials.dto';
|
||||||
|
export * from './tokens.dto';
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -1,10 +1,22 @@
|
||||||
|
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,3 +1,2 @@
|
||||||
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';
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
export type Tokens = {
|
|
||||||
access_token: string;
|
|
||||||
refresh_token: string;
|
|
||||||
};
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { ForbiddenException, Injectable } from '@nestjs/common';
|
import { ForbiddenException, Injectable } from '@nestjs/common';
|
||||||
import { UserCredentialsDto } from '../models/dto';
|
import { TokensDto, 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';
|
||||||
|
@ -13,7 +12,7 @@ export class AuthService {
|
||||||
private readonly encryptionService: EncryptionService
|
private readonly encryptionService: EncryptionService
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public async signup(userCredentials: UserCredentialsDto): Promise<Tokens> {
|
public async signup(userCredentials: UserCredentialsDto): Promise<TokensDto> {
|
||||||
const passwordHashed = await this.encryptionService.hashData(
|
const passwordHashed = await this.encryptionService.hashData(
|
||||||
userCredentials.password
|
userCredentials.password
|
||||||
);
|
);
|
||||||
|
@ -24,7 +23,7 @@ export class AuthService {
|
||||||
return this.generateAndPersistTokens(user.id, user.email);
|
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(
|
const user = await this.userRepository.findUserByEmail(
|
||||||
userCredentials.email
|
userCredentials.email
|
||||||
);
|
);
|
||||||
|
@ -43,7 +42,10 @@ export class AuthService {
|
||||||
return this.generateAndPersistTokens(user.id, user.email);
|
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);
|
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');
|
||||||
|
@ -71,7 +73,7 @@ export class AuthService {
|
||||||
private async generateAndPersistTokens(
|
private async generateAndPersistTokens(
|
||||||
userId: number,
|
userId: number,
|
||||||
email: string
|
email: string
|
||||||
): Promise<Tokens> {
|
): Promise<TokensDto> {
|
||||||
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,7 +24,10 @@ 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(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 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