chnage type to dto

This commit is contained in:
Igor Hrenowitsch Propisnov 2024-05-22 10:11:29 +02:00
parent 10e481669e
commit 2549dc9077
7 changed files with 29 additions and 18 deletions

View File

@ -7,8 +7,7 @@ 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';
@ -21,7 +20,7 @@ export class AuthController {
@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);
} }
@ -30,7 +29,7 @@ export class AuthController {
@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);
} }
@ -47,7 +46,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);
} }
} }

View File

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

View File

@ -0,0 +1,11 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class TokensDto {
@IsNotEmpty()
@IsString()
public access_token: string;
@IsNotEmpty()
@IsString()
public refresh_token: string;
}

View File

@ -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';

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 { 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

View File

@ -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 };