Feature: Added Code generation based on swagger.json #4
|
@ -7,8 +7,7 @@ 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';
|
||||
|
||||
|
@ -21,7 +20,7 @@ export class AuthController {
|
|||
@HttpCode(HttpStatus.CREATED)
|
||||
public async signup(
|
||||
@Body() userCredentials: UserCredentialsDto
|
||||
): Promise<Tokens> {
|
||||
): Promise<TokensDto> {
|
||||
return this.authService.signup(userCredentials);
|
||||
}
|
||||
|
||||
|
@ -30,7 +29,7 @@ export class AuthController {
|
|||
@HttpCode(HttpStatus.OK)
|
||||
public async signin(
|
||||
@Body() userCredentials: UserCredentialsDto
|
||||
): Promise<Tokens> {
|
||||
): Promise<TokensDto> {
|
||||
return this.authService.signin(userCredentials);
|
||||
}
|
||||
|
||||
|
@ -47,7 +46,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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
export * from './user-credentials.dto';
|
||||
export * from './tokens.dto';
|
||||
|
|
|
@ -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;
|
||||
}
|
|
@ -1,3 +1,2 @@
|
|||
export * from './tokens.type';
|
||||
export * from './jwt-payload.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 { 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
|
||||
|
|
|
@ -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 };
|
||||
|
|
Loading…
Reference in New Issue