Browse Source

FEATURE | refresh token

merge-requests/24/head
andrew_bashliy 3 years ago
parent
commit
ee958e3da4
  1. 2
      Dockerfile
  2. 2
      src/core/namespaces/sessions.namespace.ts
  3. 8
      src/rest/app/auth/auth.module.ts
  4. 20
      src/rest/app/auth/controllers/app-auth.controller.ts
  5. 2
      src/rest/app/auth/dto/index.ts
  6. 8
      src/rest/app/auth/dto/refresh-token.dto.ts
  7. 9
      src/rest/app/auth/services/app-auth.service.ts

2
Dockerfile

@ -26,4 +26,4 @@ COPY . /home/node/app @@ -26,4 +26,4 @@ COPY . /home/node/app
# ARG NODE_ENV
# ENV NODE_ENV $NODE_ENV
RUN npm install
# RUN npm install

2
src/core/namespaces/sessions.namespace.ts

@ -60,7 +60,7 @@ export namespace Sessions { @@ -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<ITokenPair>

8
src/rest/app/auth/auth.module.ts

@ -1,5 +1,5 @@ @@ -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 { @@ -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],
}

20
src/rest/app/auth/controllers/app-auth.controller.ts

@ -1,8 +1,14 @@ @@ -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 { @@ -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)
}
}

2
src/rest/app/auth/dto/index.ts

@ -2,3 +2,5 @@ export * from './login.dto' @@ -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'

8
src/rest/app/auth/dto/refresh-token.dto.ts

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
import { IsString } from 'class-validator'
import { DtoProperty } from 'src/shared'
export class RefreshTokenDto {
@DtoProperty()
@IsString()
refreshToken: string
}

9
src/rest/app/auth/services/app-auth.service.ts

@ -1,6 +1,6 @@ @@ -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' @@ -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 { @@ -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)
}
}

Loading…
Cancel
Save