Skip to content

Commit

Permalink
feat: add Swagger documentation for the refresh token API
Browse files Browse the repository at this point in the history
  • Loading branch information
a20688392 committed Aug 10, 2023
1 parent 94f4917 commit 57fa5d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Body, Controller, Get, Post, Req, UseGuards } from "@nestjs/common";
import {
ApiBadRequestResponse,
ApiBearerAuth,
ApiBody,
ApiConflictResponse,
ApiCreatedResponse,
ApiForbiddenResponse,
ApiOperation,
ApiTags,
ApiUnauthorizedResponse,
} from "@nestjs/swagger";
import { Request } from "express";
import { CreateUserDto } from "src/user/dto/create-user.dto";
Expand All @@ -17,6 +19,7 @@ import { CreateUserResponse } from "src/user/responses/create-user-response";

import { AuthService } from "./auth.service";
import { ForbiddenError } from "./exception/ForbiddenError";
import { UnauthorizedError } from "./exception/UnauthorizedError";
import { type JwtUser } from "./jwt/jwt.interface";
import { JwtRefreshGuard } from "./jwt/jwt-refresh.guard";
import { LocalAuthGuard } from "./local/local-auth.guard";
Expand Down Expand Up @@ -51,7 +54,8 @@ export class AuthController {
@Post("login")
@UseGuards(LocalAuthGuard)
@ApiOperation({
description: "Will return token when the account information is correct.",
description:
"Will return access and refresh token when the account information is correct.",
summary: "login with our account",
})
@ApiCreatedResponse({
Expand All @@ -68,7 +72,25 @@ export class AuthController {
}

@Get("refresh")
@ApiBearerAuth()
@UseGuards(JwtRefreshGuard)
@ApiOperation({
description:
"Will return access and refresh token when the refresh token carried in the request is valid.",
summary: "refresh token",
})
@ApiCreatedResponse({
description: "Success with generated token",
type: GenerateTokenResponse,
})
@ApiUnauthorizedResponse({
description: String(
"Token error \n " +
"When the token is not carried in the request. \n" +
"When the token has expired or become invalid.",
),
type: UnauthorizedError,
})
async refresh(@Req() request: Request) {
return this.login(request);
}
Expand Down
17 changes: 17 additions & 0 deletions src/auth/exception/UnauthorizedError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ApiProperty } from "@nestjs/swagger";

export class UnauthorizedError {
@ApiProperty({
description: "HTTP Code",
example: "401",
type: "number",
})
public readonly statusCode: number;

@ApiProperty({
description: "Error message",
example: "Unauthorized",
type: "string",
})
public readonly message: string;
}

0 comments on commit 57fa5d2

Please sign in to comment.