-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from mapeveri/feat/refresh-token
Add refresh token
- Loading branch information
Showing
6 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/languages/infrastructure/ui/api/v1/controllers/auth/refreshTokenPostController.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Body, Controller, HttpCode, Inject, Post } from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import LoginPostResponseDto from './loginPostResponseDto'; | ||
import { ApiBadRequestResponse, ApiInternalServerErrorResponse, ApiOkResponse, ApiTags } from '@nestjs/swagger'; | ||
import RefreshTokenPostDto from './refreshTokenPostDto'; | ||
import RefreshTokenPostResponseDto from './refreshTokenPostResponseDto'; | ||
import { QUERY_BUS, QueryBus } from '@src/shared/domain/buses/queryBus/queryBus'; | ||
import FindUserQuery from '@src/languages/application/user/query/find/findUserQuery'; | ||
|
||
@ApiTags('Auth') | ||
@Controller() | ||
export default class RefreshTokenPostController { | ||
public constructor(@Inject(QUERY_BUS) private queryBus: QueryBus, private jwtService: JwtService) {} | ||
|
||
@Post('auth/refresh-token') | ||
@HttpCode(200) | ||
@ApiOkResponse({ type: LoginPostResponseDto }) | ||
@ApiBadRequestResponse({ description: 'Bad Request.' }) | ||
@ApiInternalServerErrorResponse({ description: 'Internal Server Error.' }) | ||
async run(@Body() payload: RefreshTokenPostDto): Promise<RefreshTokenPostResponseDto> { | ||
const decodedRefreshToken = this.jwtService.verify(payload.refreshToken); | ||
if (decodedRefreshToken.revoked) { | ||
throw new Error('Token revoked'); | ||
} | ||
|
||
const user = await this.queryBus.ask(new FindUserQuery(decodedRefreshToken.id)); | ||
const refreshToken = this.jwtService.sign(user.content); | ||
|
||
return { | ||
refreshToken, | ||
}; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/languages/infrastructure/ui/api/v1/controllers/auth/refreshTokenPostDto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
export default class RefreshTokenPostDto { | ||
@ApiProperty() | ||
@IsNotEmpty() | ||
@IsString() | ||
refreshToken: string; | ||
} |
9 changes: 9 additions & 0 deletions
9
src/languages/infrastructure/ui/api/v1/controllers/auth/refreshTokenPostResponseDto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { IsNotEmpty, IsString } from 'class-validator'; | ||
|
||
export default class RefreshTokenPostResponseDto { | ||
@ApiProperty() | ||
@IsNotEmpty() | ||
@IsString() | ||
refreshToken: string; | ||
} |