Skip to content

Commit dc2da17

Browse files
swagger api doc
1 parent 1e21275 commit dc2da17

File tree

2 files changed

+69
-29
lines changed

2 files changed

+69
-29
lines changed

src/auth/auth.controller.ts

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,67 @@ import {
77
Res,
88
UseFilters,
99
UseGuards,
10-
} from '@nestjs/common';
11-
import { Response,Request } from 'express';
12-
import { AuthService } from './auth.service';
13-
import { Public } from './decorators/public.decorator';
14-
import { HttpExceptionFilter } from '../utils/http-exception.filter';
15-
import { AuthGuard } from '../common/guards/index';
16-
import { sendResponse } from '../utils/index';
17-
import { statusMessage } from '../constant/statusMessage';
10+
} from "@nestjs/common";
11+
import { Response, Request } from "express";
12+
import { AuthService } from "./auth.service";
13+
import { Public } from "./decorators/public.decorator";
14+
import { HttpExceptionFilter } from "../utils/http-exception.filter";
15+
import { AuthGuard } from "../common/guards/index";
16+
import {
17+
sendResponse,
18+
loginSuccessResponse,
19+
loginErrorResponse,
20+
} from "../utils/index";
21+
import { statusMessage } from "../constant/statusMessage";
22+
import { ApiResponse } from "@nestjs/swagger";
1823

19-
@Controller('auth')
24+
@Controller("auth")
2025
export class AuthController {
2126
constructor(private authService: AuthService) {}
2227

28+
@ApiResponse(loginSuccessResponse)
29+
@ApiResponse(loginErrorResponse)
2330
@Public()
24-
@HttpCode(HttpStatus.OK)
25-
@Post('login')
31+
@UseFilters(new HttpExceptionFilter())
32+
@Post("login")
2633
async signIn(@Body() signInDto: Record<string, any>, @Res() res: Response) {
27-
const token = await this.authService.signIn(signInDto.email, signInDto.password);
28-
console.log(token)
34+
const token = await this.authService.signIn(
35+
signInDto.email,
36+
signInDto.password
37+
);
38+
console.log(token);
2939

30-
res.cookie('access_token',token.access_token, {
40+
res.cookie("access_token", token.access_token, {
3141
httpOnly: true,
3242
expires: new Date(Date.now() + 1 * 24 * 60 * 60 * 1000),
33-
path: '/',
34-
sameSite: 'none',
43+
path: "/",
44+
sameSite: "none",
3545
secure: true,
3646
});
37-
38-
res.cookie('refresh_token', token.refresh_token, {
47+
48+
res.cookie("refresh_token", token.refresh_token, {
3949
httpOnly: true,
4050
expires: new Date(Date.now() + 1 * 24 * 60 * 60 * 1000),
41-
path: '/',
42-
sameSite: 'none',
51+
path: "/",
52+
sameSite: "none",
4353
secure: true,
4454
});
45-
return sendResponse(res,HttpStatus.OK,statusMessage[HttpStatus.OK],true,null);
55+
return sendResponse(
56+
res,
57+
HttpStatus.OK,
58+
statusMessage[HttpStatus.OK],
59+
true,
60+
null
61+
);
4662
}
4763

4864
// @Public()
4965
@UseGuards(AuthGuard)
50-
@Post('/refresh')
66+
@Post("/refresh")
5167
@UseFilters(new HttpExceptionFilter())
52-
async refreshTokens(
53-
@Res() request:Request,
54-
@Res() res: Response
55-
) {
68+
async refreshTokens(@Res() request: Request, @Res() res: Response) {
5669
// console.log(request);
57-
return res.sendStatus(200)
70+
return res.sendStatus(200);
5871
// return await this.authService.refreshTokens(userId, refreshToken);
5972
}
60-
61-
6273
}

src/utils/index.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,33 @@ export const loginSuccessResponse = {
173173
},
174174
},
175175
},
176+
};
177+
178+
export const loginErrorResponse = {
179+
status: 401,
180+
description: "Success! Returns the user data.",
181+
content: {
182+
"application/json": {
183+
schema: {
184+
type: "object",
185+
properties: {
186+
statusCode: { type: "number", example: 400 },
187+
isSuccess: { type: "boolean", example: false },
188+
message: { type: "string", example: "Unauthorized" },
189+
data: { type: "object", example: null },
190+
timestamp: { type: "date", example: "2023-12-22T05:17:16.499Z" },
191+
path: { type: "string", example: "/login" },
192+
},
193+
},
194+
example: {
195+
statusCode: 401,
196+
message: "Unauthorized",
197+
error: "Unauthorized",
198+
timestamp: "2023-12-22T06:00:04.100Z",
199+
path: "/auth/login",
200+
isSuccess: false,
201+
data: null,
202+
},
203+
},
204+
},
176205
};

0 commit comments

Comments
 (0)