From ee958e3da49184efa85b71bb04f4516137792cc0 Mon Sep 17 00:00:00 2001 From: andrew_bashliy Date: Mon, 6 Sep 2021 11:49:50 +0300 Subject: [PATCH] FEATURE | refresh token --- Dockerfile | 2 +- src/core/namespaces/sessions.namespace.ts | 2 +- src/rest/app/auth/auth.module.ts | 8 ++++++-- .../auth/controllers/app-auth.controller.ts | 20 +++++++++++++++++-- src/rest/app/auth/dto/index.ts | 2 ++ src/rest/app/auth/dto/refresh-token.dto.ts | 8 ++++++++ .../app/auth/services/app-auth.service.ts | 9 +++++++-- 7 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 src/rest/app/auth/dto/refresh-token.dto.ts diff --git a/Dockerfile b/Dockerfile index b443d42..31681e8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,4 +26,4 @@ COPY . /home/node/app # ARG NODE_ENV # ENV NODE_ENV $NODE_ENV -RUN npm install +# RUN npm install diff --git a/src/core/namespaces/sessions.namespace.ts b/src/core/namespaces/sessions.namespace.ts index f13ee3e..99b41ae 100644 --- a/src/core/namespaces/sessions.namespace.ts +++ b/src/core/namespaces/sessions.namespace.ts @@ -60,7 +60,7 @@ export namespace Sessions { /** * Метод для поновлення сессії користувача - * @param {string} token - access token + * @param {string} token - refresh token * @returns Повертає access i reshresh токени */ refresh(token: string): Promise diff --git a/src/rest/app/auth/auth.module.ts b/src/rest/app/auth/auth.module.ts index 243b9a0..382fa1c 100644 --- a/src/rest/app/auth/auth.module.ts +++ b/src/rest/app/auth/auth.module.ts @@ -1,5 +1,5 @@ import { DynamicModule, Module } from '@nestjs/common' -import { UsersModule } from 'src/domain' +import { SessionsModule, UsersModule } from 'src/domain' import { JwtModule } from 'src/libs' import { AppAuthController } from './controllers' import { AppAuthService } from './services' @@ -11,7 +11,11 @@ export class AppAuthModule { static forRoot(): DynamicModule { return { module: AppAuthModule, - imports: [UsersModule.forFeature(), JwtModule.forFeature()], + imports: [ + UsersModule.forFeature(), + SessionsModule.forFeature(), + JwtModule.forFeature(), + ], providers: [AppAuthService], controllers: [AppAuthController], } diff --git a/src/rest/app/auth/controllers/app-auth.controller.ts b/src/rest/app/auth/controllers/app-auth.controller.ts index 58e0e32..2ce6435 100644 --- a/src/rest/app/auth/controllers/app-auth.controller.ts +++ b/src/rest/app/auth/controllers/app-auth.controller.ts @@ -1,8 +1,14 @@ import { Body, Controller, Ip, Post } from '@nestjs/common' import { ApiBody, ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger' import { AuthGuard } from 'src/domain/sessions/decorators' -import { ConfirmLoginCodePayloadDto, LoginPayloadDto, LogoutPayloadDto, TokenPairDto } from '../dto' -import { CheckCodePayloadDto } from '../dto/check-code.dto' +import { + ConfirmLoginCodePayloadDto, + LoginPayloadDto, + LogoutPayloadDto, + TokenPairDto, + CheckCodePayloadDto, + RefreshTokenDto, +} from '../dto' import { AppAuthService } from '../services' @ApiTags('App | auth') @@ -50,4 +56,14 @@ export class AppAuthController { public async checkCode(@Ip() ip: string, @Body() dto: CheckCodePayloadDto) { return await this.appAuthService.checkCode(ip, dto) } + + @ApiOperation({ summary: 'Поновлення сесії' }) + @ApiOkResponse({ + status: 200, + type: TokenPairDto, + }) + @Post('refresh-token') + public async refreshToken(@Body() dto: RefreshTokenDto) { + return await this.appAuthService.refreshToken(dto.refreshToken) + } } diff --git a/src/rest/app/auth/dto/index.ts b/src/rest/app/auth/dto/index.ts index 22fe8f7..3e4447a 100644 --- a/src/rest/app/auth/dto/index.ts +++ b/src/rest/app/auth/dto/index.ts @@ -2,3 +2,5 @@ export * from './login.dto' export * from './token-pair.dto' export * from './logout.dto' export * from './confirm-login-code.dto' +export * from './check-code.dto' +export * from './refresh-token.dto' diff --git a/src/rest/app/auth/dto/refresh-token.dto.ts b/src/rest/app/auth/dto/refresh-token.dto.ts new file mode 100644 index 0000000..21901b4 --- /dev/null +++ b/src/rest/app/auth/dto/refresh-token.dto.ts @@ -0,0 +1,8 @@ +import { IsString } from 'class-validator' +import { DtoProperty } from 'src/shared' + +export class RefreshTokenDto { + @DtoProperty() + @IsString() + refreshToken: string +} diff --git a/src/rest/app/auth/services/app-auth.service.ts b/src/rest/app/auth/services/app-auth.service.ts index 717707e..5c50f57 100644 --- a/src/rest/app/auth/services/app-auth.service.ts +++ b/src/rest/app/auth/services/app-auth.service.ts @@ -1,6 +1,6 @@ import { Inject, Injectable } from '@nestjs/common' -import { Users } from 'src/core' -import { USERS_AUTH_SERVICE } from 'src/core/consts' +import { Sessions, Users } from 'src/core' +import { SESSIONS_SERVICE, USERS_AUTH_SERVICE } from 'src/core/consts' import { ConfirmLoginCodePayloadDto, LoginPayloadDto, LogoutPayloadDto } from '../dto' import { CheckCodePayloadDto } from '../dto/check-code.dto' @@ -8,6 +8,7 @@ import { CheckCodePayloadDto } from '../dto/check-code.dto' export class AppAuthService { constructor( @Inject(USERS_AUTH_SERVICE) private readonly usersAuthService: Users.IUsersAuthService, + @Inject(SESSIONS_SERVICE) private readonly sessionsService: Sessions.ISessionsService, ) {} public async signIn(dto: LoginPayloadDto) { @@ -30,4 +31,8 @@ export class AppAuthService { public async logout(dto: LogoutPayloadDto) { return await this.usersAuthService.logout(dto.refreshToken) } + + public async refreshToken(token: string) { + return await this.sessionsService.refresh(token) + } }