Skip to content

Commit 0f0c4f2

Browse files
authored
Merge pull request #30 from JawherKl/feature/21-authorize-swagger-api
update swagger interface to execute all the api with token generated …
2 parents ca6a724 + 0198ef4 commit 0f0c4f2

File tree

5 files changed

+128
-162
lines changed

5 files changed

+128
-162
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,5 @@ dist
129129
.yarn/install-state.gz
130130
.pnp.*
131131
.clinic/
132-
features.md
132+
features.md
133+
.qodo

config/swagger.js

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,50 @@ const swaggerJSDoc = require('swagger-jsdoc');
22
const swaggerUi = require('swagger-ui-express');
33

44
const swaggerOptions = {
5-
definition: {
6-
openapi: '3.0.0',
7-
info: {
8-
title: 'User API',
9-
version: '1.0.0',
10-
description: 'This API allows you to manage users, including authentication and CRUD operations.',
5+
definition: {
6+
openapi: '3.0.0',
7+
info: {
8+
title: 'User API',
9+
version: '1.0.0',
10+
description: 'User management API with authentication',
11+
},
12+
servers: [
13+
{
14+
url: 'http://localhost:3000',
15+
description: 'Development server',
1116
},
12-
host: 'localhost:3000',
13-
basePath: '/',
14-
securityDefinitions: {
17+
],
18+
components: {
19+
securitySchemes: {
1520
bearerAuth: {
16-
type: 'apiKey',
17-
name: 'x-auth-token',
21+
type: 'http',
1822
scheme: 'bearer',
19-
in: 'header',
20-
},
23+
bearerFormat: 'JWT',
24+
description: 'Enter your JWT token'
25+
}
2126
},
22-
security: [
23-
{
24-
bearerAuth: [], // Apply bearer auth globally to all routes
25-
},
26-
],
2727
},
28-
apis: ['./routes/userRoutes.js', './controllers/userController.js'],
29-
};
30-
28+
security: [
29+
{
30+
bearerAuth: []
31+
}
32+
],
33+
},
34+
apis: ['./routes/*.js'],
35+
};
36+
3137
// Initialize Swagger JSDoc
3238
const swaggerSpec = swaggerJSDoc(swaggerOptions);
3339

34-
module.exports = { swaggerUi, swaggerSpec };
40+
const swaggerUiOptions = {
41+
explorer: true,
42+
swaggerOptions: {
43+
persistAuthorization: true,
44+
},
45+
};
46+
47+
module.exports = {
48+
swaggerUi,
49+
swaggerSpec,
50+
swaggerUiOptions
51+
};

routes/loginRoutes.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const authController = require('../controllers/authController');
88
* post:
99
* summary: Login an existing user
1010
* description: Authenticates the user and returns a JWT token
11+
* security: []
1112
* requestBody:
1213
* required: true
1314
* content:
@@ -17,30 +18,21 @@ const authController = require('../controllers/authController');
1718
* properties:
1819
* email:
1920
* type: string
20-
* description: The user's email
2121
* password:
2222
* type: string
23-
* description: The user's password
24-
* required:
25-
* - email
26-
* - password
2723
* responses:
2824
* 200:
29-
* description: JWT token returned after successful login
25+
* description: Login successful
3026
* content:
3127
* application/json:
3228
* schema:
3329
* type: object
3430
* properties:
3531
* token:
3632
* type: string
37-
* description: The JWT token for authentication
38-
* 400:
39-
* description: Bad request (invalid input)
33+
* description: JWT token to be used for authentication
4034
* 401:
41-
* description: Unauthorized (incorrect credentials)
42-
* 500:
43-
* description: Server error
35+
* description: Invalid credentials
4436
*/
4537
router.post('/login', authController.login);
4638

routes/userRoutes.js

Lines changed: 83 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,56 @@
11
const express = require('express');
22
const userController = require('../controllers/userController');
3-
const authenticateToken = require('../middleware/auth');
4-
const authorize = require('../middleware/authorize');
3+
const auth = require('../middleware/auth');
54
const multer = require('multer');
65
const upload = multer({ dest: 'uploads/' });
76

87
const router = express.Router();
98

109
/**
11-
* @swagger
10+
* @openapi
11+
* components:
12+
* securitySchemes:
13+
* bearerAuth:
14+
* type: http
15+
* scheme: bearer
16+
* bearerFormat: JWT
17+
* schemas:
18+
* User:
19+
* type: object
20+
* properties:
21+
* name:
22+
* type: string
23+
* email:
24+
* type: string
25+
* password:
26+
* type: string
27+
* profile_picture:
28+
* type: string
29+
* format: binary
30+
*
1231
* /users:
1332
* get:
14-
* summary: Retrieve all users
15-
* description: Returns a list of all users
33+
* tags:
34+
* - Users
35+
* summary: Get all users
1636
* security:
1737
* - bearerAuth: []
1838
* responses:
1939
* 200:
20-
* description: A list of users
21-
* content:
22-
* application/json:
23-
* schema:
24-
* type: array
25-
* items:
26-
* type: object
27-
* properties:
28-
* id:
29-
* type: integer
30-
* name:
31-
* type: string
32-
* email:
33-
* type: string
40+
* description: List of users retrieved successfully
3441
* 401:
35-
* description: Unauthorized access
36-
* 500:
37-
* description: Server error
38-
*/
39-
router.get('/', authenticateToken, userController.getUsers);
40-
41-
/**
42-
* @swagger
43-
* /users/{id}:
44-
* get:
45-
* summary: Retrieve a user by ID
46-
* description: Returns a single user identified by their ID
47-
* parameters:
48-
* - name: id
49-
* in: path
50-
* required: true
51-
* description: The ID of the user to retrieve
52-
* schema:
53-
* type: integer
54-
* security:
55-
* - bearerAuth: []
56-
* responses:
57-
* 200:
58-
* description: User details
59-
* content:
60-
* application/json:
61-
* schema:
62-
* type: object
63-
* properties:
64-
* id:
65-
* type: integer
66-
* name:
67-
* type: string
68-
* email:
69-
* type: string
70-
* 401:
71-
* description: Unauthorized access
72-
* 404:
73-
* description: User not found
74-
* 500:
75-
* description: Server error
76-
*/
77-
router.get('/:id', authenticateToken, userController.getUserById);
78-
79-
/**
80-
* @swagger
81-
* /users:
42+
* description: Unauthorized - invalid token
43+
*
8244
* post:
45+
* tags:
46+
* - Users
8347
* summary: Create a new user
84-
* description: Creates a new user in the system
48+
* security:
49+
* - bearerAuth: []
8550
* requestBody:
8651
* required: true
8752
* content:
88-
* application/json:
53+
* multipart/form-data:
8954
* schema:
9055
* type: object
9156
* properties:
@@ -95,44 +60,46 @@ router.get('/:id', authenticateToken, userController.getUserById);
9560
* type: string
9661
* password:
9762
* type: string
98-
* required:
99-
* - name
100-
* - email
101-
* - password
102-
* security:
103-
* - bearerAuth: []
63+
* picture:
64+
* type: string
65+
* format: binary
10466
* responses:
10567
* 201:
10668
* description: User created successfully
107-
* content:
108-
* application/json:
109-
* schema:
110-
* type: object
111-
* properties:
112-
* message:
113-
* type: string
114-
* userId:
115-
* type: integer
116-
* 400:
117-
* description: Validation error
11869
* 401:
119-
* description: Unauthorized access
120-
* 500:
121-
* description: Server error
122-
*/
123-
router.post('/', authenticateToken, upload.single('picture'), userController.createUser);
124-
125-
/**
126-
* @swagger
70+
* description: Unauthorized - invalid token
71+
*
12772
* /users/{id}:
73+
* get:
74+
* tags:
75+
* - Users
76+
* summary: Get user by ID
77+
* security:
78+
* - bearerAuth: []
79+
* parameters:
80+
* - in: path
81+
* name: id
82+
* required: true
83+
* schema:
84+
* type: integer
85+
* responses:
86+
* 200:
87+
* description: User found successfully
88+
* 401:
89+
* description: Unauthorized - invalid token
90+
* 404:
91+
* description: User not found
92+
*
12893
* put:
129-
* summary: Update an existing user
130-
* description: Updates the details of an existing user by ID
94+
* tags:
95+
* - Users
96+
* summary: Update user
97+
* security:
98+
* - bearerAuth: []
13199
* parameters:
132-
* - name: id
133-
* in: path
100+
* - in: path
101+
* name: id
134102
* required: true
135-
* description: The ID of the user to update
136103
* schema:
137104
* type: integer
138105
* requestBody:
@@ -146,52 +113,41 @@ router.post('/', authenticateToken, upload.single('picture'), userController.cre
146113
* type: string
147114
* email:
148115
* type: string
149-
* required:
150-
* - name
151-
* - email
152-
* security:
153-
* - bearerAuth: []
116+
* password:
117+
* type: string
154118
* responses:
155119
* 200:
156120
* description: User updated successfully
157-
* 400:
158-
* description: Bad request, validation error
159121
* 401:
160-
* description: Unauthorized access
122+
* description: Unauthorized - invalid token
161123
* 404:
162124
* description: User not found
163-
* 500:
164-
* description: Server error
165-
*/
166-
router.put('/:id', authenticateToken, userController.updateUser);
167-
168-
/**
169-
* @swagger
170-
* /users/{id}:
125+
*
171126
* delete:
172-
* summary: Delete a user by ID
173-
* description: Deletes a user from the system by their ID
127+
* tags:
128+
* - Users
129+
* summary: Delete user
130+
* security:
131+
* - bearerAuth: []
174132
* parameters:
175-
* - name: id
176-
* in: path
133+
* - in: path
134+
* name: id
177135
* required: true
178-
* description: The ID of the user to delete
179136
* schema:
180137
* type: integer
181-
* security:
182-
* - bearerAuth: []
183138
* responses:
184139
* 200:
185140
* description: User deleted successfully
186141
* 401:
187-
* description: Unauthorized access
188-
* 403:
189-
* description: Forbidden, insufficient privileges
142+
* description: Unauthorized - invalid token
190143
* 404:
191144
* description: User not found
192-
* 500:
193-
* description: Server error
194145
*/
195-
router.delete('/:id', authenticateToken, authorize('admin'), userController.deleteUser);
146+
147+
router.get('/', auth, userController.getUsers);
148+
router.post('/', auth, upload.single('picture'), userController.createUser);
149+
router.get('/:id', auth, userController.getUserById);
150+
router.put('/:id', auth, userController.updateUser);
151+
router.delete('/:id', auth, userController.deleteUser);
196152

197153
module.exports = router;
38.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)