|
8 | 8 |
|
9 | 9 | 2. After setting up, go to the Database Deployment Page. You would see a list of the Databases you have set up. Select `Connect` on the cluster you just created earlier on for User Service.
|
10 | 10 |
|
11 |
| -  |
| 11 | +  |
12 | 12 |
|
13 | 13 | 3. Select the `Drivers` option, as we have to link to a Node.js App (User Service).
|
14 | 14 |
|
15 |
| -  |
| 15 | +  |
16 | 16 |
|
17 | 17 | 4. Select `Node.js` in the `Driver` pull-down menu, and copy the connection string.
|
18 | 18 |
|
19 |
| - Notice, you may see `<password>` in this connection string. We will be replacing this with the admin account password that we created earlier on when setting up the Shared Cluster. |
| 19 | + Notice, you may see `<password>` in this connection string. We will be replacing this with the admin account password that we created earlier on when setting up the Shared Cluster. |
20 | 20 |
|
21 |
| -  |
| 21 | +  |
22 | 22 |
|
23 | 23 | 5. In the `user-service` directory, create a copy of the `.env.sample` file and name it `.env`.
|
24 | 24 |
|
|
45 | 45 | - Endpoint: http://localhost:3001/users
|
46 | 46 |
|
47 | 47 | - Body
|
| 48 | + |
48 | 49 | - Required: `username` (string), `email` (string), `password` (string)
|
49 | 50 |
|
50 | 51 | ```json
|
|
57 | 58 |
|
58 | 59 | - Responses:
|
59 | 60 |
|
60 |
| - | Response Code | Explanation | |
61 |
| - |-----------------------------|-------------------------------------------------------| |
62 |
| - | 201 (Created) | User created successfully, created user data returned | |
63 |
| - | 400 (Bad Request) | Missing fields | |
64 |
| - | 409 (Conflict) | Duplicate username or email encountered | |
65 |
| - | 500 (Internal Server Error) | Database or server error | |
| 61 | + | Response Code | Explanation | |
| 62 | + | --------------------------- | ----------------------------------------------------- | |
| 63 | + | 201 (Created) | User created successfully, created user data returned | |
| 64 | + | 400 (Bad Request) | Missing fields | |
| 65 | + | 409 (Conflict) | Duplicate username or email encountered | |
| 66 | + | 500 (Internal Server Error) | Database or server error | |
66 | 67 |
|
67 | 68 | ### Get User
|
68 | 69 |
|
|
75 | 76 | - Endpoint: http://localhost:3001/users/{userId}
|
76 | 77 |
|
77 | 78 | - Parameters
|
78 |
| - - Required: `userId` path parameter |
79 |
| - - Example: `http://localhost:3001/users/60c72b2f9b1d4c3a2e5f8b4c` |
| 79 | + |
| 80 | + - Required: `userId` path parameter |
| 81 | + - Example: `http://localhost:3001/users/60c72b2f9b1d4c3a2e5f8b4c` |
80 | 82 |
|
81 | 83 | - <a name="auth-header">Headers</a>
|
82 |
| - |
83 |
| - - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
84 |
| - |
85 |
| - - Explanation: This endpoint requires the client to include a JWT (JSON Web Token) in the HTTP request header for authentication and authorization. This token is generated during the authentication process (i.e., login) and contains information about the user's identity. The server verifies this token to ensure that the client is authorized to access the data. |
86 |
| - |
87 |
| - - Auth Rules: |
88 |
| - |
89 |
| - - Admin users: Can retrieve any user's data. The server verifies the user associated with the JWT token is an admin user and allows access to the requested user's data. |
90 |
| - |
91 |
| - - Non-admin users: Can only retrieve their own data. The server checks if the user ID in the request URL matches the ID of the user associated with the JWT token. If it matches, the server returns the user's own data. |
92 |
| - |
| 84 | + |
| 85 | + - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
| 86 | + |
| 87 | + - Explanation: This endpoint requires the client to include a JWT (JSON Web Token) in the HTTP request header for authentication and authorization. This token is generated during the authentication process (i.e., login) and contains information about the user's identity. The server verifies this token to ensure that the client is authorized to access the data. |
| 88 | + |
| 89 | + - Auth Rules: |
| 90 | + |
| 91 | + - Admin users: Can retrieve any user's data. The server verifies the user associated with the JWT token is an admin user and allows access to the requested user's data. |
| 92 | + - Non-admin users: Can only retrieve their own data. The server checks if the user ID in the request URL matches the ID of the user associated with the JWT token. If it matches, the server returns the user's own data. |
| 93 | + |
93 | 94 | - Responses:
|
94 | 95 |
|
95 |
| - | Response Code | Explanation | |
96 |
| - |-----------------------------|----------------------------------------------------------| |
97 |
| - | 200 (OK) | Success, user data returned | |
98 |
| - | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
99 |
| - | 403 (Forbidden) | Access denied for non-admin users accessing others' data | |
100 |
| - | 404 (Not Found) | User with the specified ID not found | |
101 |
| - | 500 (Internal Server Error) | Database or server error | |
| 96 | + | Response Code | Explanation | |
| 97 | + | --------------------------- | -------------------------------------------------------- | |
| 98 | + | 200 (OK) | Success, user data returned | |
| 99 | + | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
| 100 | + | 403 (Forbidden) | Access denied for non-admin users accessing others' data | |
| 101 | + | 404 (Not Found) | User with the specified ID not found | |
| 102 | + | 500 (Internal Server Error) | Database or server error | |
102 | 103 |
|
103 | 104 | ### Get All Users
|
104 | 105 |
|
105 | 106 | - This endpoint allows retrieval of all users' data from the database.
|
106 | 107 | - HTTP Method: `GET`
|
107 | 108 | - Endpoint: http://localhost:3001/users
|
108 | 109 | - Headers
|
109 |
| - - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
110 |
| - - Auth Rules: |
111 | 110 |
|
112 |
| - - Admin users: Can retrieve all users' data. The server verifies the user associated with the JWT token is an admin user and allows access to all users' data. |
113 |
| - |
114 |
| - - Non-admin users: Not allowed access. |
| 111 | + - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
| 112 | + - Auth Rules: |
| 113 | + |
| 114 | + - Admin users: Can retrieve all users' data. The server verifies the user associated with the JWT token is an admin user and allows access to all users' data. |
| 115 | + - Non-admin users: Not allowed access. |
115 | 116 |
|
116 | 117 | - Responses:
|
117 | 118 |
|
118 |
| - | Response Code | Explanation | |
119 |
| - |-----------------------------|--------------------------------------------------| |
120 |
| - | 200 (OK) | Success, all user data returned | |
121 |
| - | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
122 |
| - | 403 (Forbidden) | Access denied for non-admin users | |
123 |
| - | 500 (Internal Server Error) | Database or server error | |
| 119 | + | Response Code | Explanation | |
| 120 | + | --------------------------- | ------------------------------------------------ | |
| 121 | + | 200 (OK) | Success, all user data returned | |
| 122 | + | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
| 123 | + | 403 (Forbidden) | Access denied for non-admin users | |
| 124 | + | 500 (Internal Server Error) | Database or server error | |
124 | 125 |
|
125 | 126 | ### Update User
|
126 | 127 |
|
|
131 | 132 | - Endpoint: http://localhost:3001/users/{userId}
|
132 | 133 |
|
133 | 134 | - Parameters
|
| 135 | + |
134 | 136 | - Required: `userId` path parameter
|
135 | 137 |
|
136 | 138 | - Body
|
| 139 | + |
137 | 140 | - At least one of the following fields is required: `username` (string), `email` (string), `password` (string)
|
138 | 141 |
|
139 | 142 | ```json
|
|
145 | 148 | ```
|
146 | 149 |
|
147 | 150 | - Headers
|
148 |
| - - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
149 |
| - - Auth Rules: |
150 | 151 |
|
151 |
| - - Admin users: Can update any user's data. The server verifies the user associated with the JWT token is an admin user and allows the update of requested user's data. |
152 |
| - |
153 |
| - - Non-admin users: Can only update their own data. The server checks if the user ID in the request URL matches the ID of the user associated with the JWT token. If it matches, the server updates the user's own data. |
| 152 | + - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
| 153 | + - Auth Rules: |
| 154 | + |
| 155 | + - Admin users: Can update any user's data. The server verifies the user associated with the JWT token is an admin user and allows the update of requested user's data. |
| 156 | + - Non-admin users: Can only update their own data. The server checks if the user ID in the request URL matches the ID of the user associated with the JWT token. If it matches, the server updates the user's own data. |
154 | 157 |
|
155 | 158 | - Responses:
|
156 | 159 |
|
157 |
| - | Response Code | Explanation | |
158 |
| - |-----------------------------|---------------------------------------------------------| |
159 |
| - | 200 (OK) | User updated successfully, updated user data returned | |
160 |
| - | 400 (Bad Request) | Missing fields | |
161 |
| - | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
162 |
| - | 403 (Forbidden) | Access denied for non-admin users updating others' data | |
163 |
| - | 404 (Not Found) | User with the specified ID not found | |
164 |
| - | 409 (Conflict) | Duplicate username or email encountered | |
165 |
| - | 500 (Internal Server Error) | Database or server error | |
| 160 | + | Response Code | Explanation | |
| 161 | + | --------------------------- | ------------------------------------------------------- | |
| 162 | + | 200 (OK) | User updated successfully, updated user data returned | |
| 163 | + | 400 (Bad Request) | Missing fields | |
| 164 | + | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
| 165 | + | 403 (Forbidden) | Access denied for non-admin users updating others' data | |
| 166 | + | 404 (Not Found) | User with the specified ID not found | |
| 167 | + | 409 (Conflict) | Duplicate username or email encountered | |
| 168 | + | 500 (Internal Server Error) | Database or server error | |
166 | 169 |
|
167 | 170 | ### Update User Privilege
|
168 | 171 |
|
|
173 | 176 | - Endpoint: http://localhost:3001/users/{userId}
|
174 | 177 |
|
175 | 178 | - Parameters
|
| 179 | + |
176 | 180 | - Required: `userId` path parameter
|
177 | 181 |
|
178 | 182 | - Body
|
| 183 | + |
179 | 184 | - Required: `isAdmin` (boolean)
|
180 | 185 |
|
181 | 186 | ```json
|
|
185 | 190 | ```
|
186 | 191 |
|
187 | 192 | - Headers
|
188 |
| - - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
189 |
| - - Auth Rules: |
190 | 193 |
|
191 |
| - - Admin users: Can update any user's privilege. The server verifies the user associated with the JWT token is an admin user and allows the privilege update. |
192 |
| - - Non-admin users: Not allowed access. |
| 194 | + - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
| 195 | + - Auth Rules: |
| 196 | + |
| 197 | + - Admin users: Can update any user's privilege. The server verifies the user associated with the JWT token is an admin user and allows the privilege update. |
| 198 | + - Non-admin users: Not allowed access. |
193 | 199 |
|
194 | 200 | > :bulb: You may need to manually assign admin status to the first user by directly editing the database document before using this endpoint.
|
195 | 201 |
|
196 | 202 | - Responses:
|
197 | 203 |
|
198 |
| - | Response Code | Explanation | |
199 |
| - |-----------------------------|-----------------------------------------------------------------| |
200 |
| - | 200 (OK) | User privilege updated successfully, updated user data returned | |
201 |
| - | 400 (Bad Request) | Missing fields | |
202 |
| - | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
203 |
| - | 403 (Forbidden) | Access denied for non-admin users | |
204 |
| - | 404 (Not Found) | User with the specified ID not found | |
205 |
| - | 500 (Internal Server Error) | Database or server error | |
| 204 | + | Response Code | Explanation | |
| 205 | + | --------------------------- | --------------------------------------------------------------- | |
| 206 | + | 200 (OK) | User privilege updated successfully, updated user data returned | |
| 207 | + | 400 (Bad Request) | Missing fields | |
| 208 | + | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
| 209 | + | 403 (Forbidden) | Access denied for non-admin users | |
| 210 | + | 404 (Not Found) | User with the specified ID not found | |
| 211 | + | 500 (Internal Server Error) | Database or server error | |
206 | 212 |
|
207 | 213 | ### Delete User
|
208 | 214 |
|
|
212 | 218 | - Parameters
|
213 | 219 |
|
214 | 220 | - Required: `userId` path parameter
|
| 221 | + |
215 | 222 | - Headers
|
216 | 223 |
|
217 | 224 | - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
|
|
221 | 228 | - Admin users: Can delete any user's data. The server verifies the user associated with the JWT token is an admin user and allows the deletion of requested user's data.
|
222 | 229 |
|
223 | 230 | - Non-admin users: Can only delete their own data. The server checks if the user ID in the request URL matches the ID of the user associated with the JWT token. If it matches, the server deletes the user's own data.
|
| 231 | + |
224 | 232 | - Responses:
|
225 | 233 |
|
226 |
| - | Response Code | Explanation | |
227 |
| - |-----------------------------|---------------------------------------------------------| |
228 |
| - | 200 (OK) | User deleted successfully | |
229 |
| - | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
230 |
| - | 403 (Forbidden) | Access denied for non-admin users deleting others' data | |
231 |
| - | 404 (Not Found) | User with the specified ID not found | |
232 |
| - | 500 (Internal Server Error) | Database or server error | |
| 234 | + | Response Code | Explanation | |
| 235 | + | --------------------------- | ------------------------------------------------------- | |
| 236 | + | 200 (OK) | User deleted successfully | |
| 237 | + | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
| 238 | + | 403 (Forbidden) | Access denied for non-admin users deleting others' data | |
| 239 | + | 404 (Not Found) | User with the specified ID not found | |
| 240 | + | 500 (Internal Server Error) | Database or server error | |
233 | 241 |
|
234 | 242 | ### Login
|
235 | 243 |
|
236 | 244 | - This endpoint allows a user to authenticate with an email and password and returns a JWT access token. The token is valid for 1 day and can be used subsequently to access protected resources. For example usage, refer to the [Authorization header section in the Get User endpoint](#auth-header).
|
237 | 245 | - HTTP Method: `POST`
|
238 | 246 | - Endpoint: http://localhost:3001/auth/login
|
239 | 247 | - Body
|
| 248 | + |
240 | 249 | - Required: `email` (string), `password` (string)
|
241 | 250 |
|
242 | 251 | ```json
|
|
248 | 257 |
|
249 | 258 | - Responses:
|
250 | 259 |
|
251 |
| - | Response Code | Explanation | |
252 |
| - |-----------------------------|----------------------------------------------------| |
253 |
| - | 200 (OK) | Login successful, JWT token and user data returned | |
254 |
| - | 400 (Bad Request) | Missing fields | |
255 |
| - | 401 (Unauthorized) | Incorrect email or password | |
256 |
| - | 500 (Internal Server Error) | Database or server error | |
| 260 | + | Response Code | Explanation | |
| 261 | + | --------------------------- | -------------------------------------------------- | |
| 262 | + | 200 (OK) | Login successful, JWT token and user data returned | |
| 263 | + | 400 (Bad Request) | Missing fields | |
| 264 | + | 401 (Unauthorized) | Incorrect email or password | |
| 265 | + | 500 (Internal Server Error) | Database or server error | |
257 | 266 |
|
258 | 267 | ### Verify Token
|
259 | 268 |
|
260 | 269 | - This endpoint allows one to verify a JWT access token to authenticate and retrieve the user's data associated with the token.
|
261 | 270 | - HTTP Method: `GET`
|
262 | 271 | - Endpoint: http://localhost:3001/auth/verify-token
|
263 | 272 | - Headers
|
| 273 | + |
264 | 274 | - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>`
|
265 | 275 |
|
266 | 276 | - Responses:
|
267 | 277 |
|
268 |
| - | Response Code | Explanation | |
269 |
| - |-----------------------------|----------------------------------------------------| |
270 |
| - | 200 (OK) | Token verified, authenticated user's data returned | |
271 |
| - | 401 (Unauthorized) | Missing/invalid/expired JWT | |
272 |
| - | 500 (Internal Server Error) | Database or server error | |
| 278 | + | Response Code | Explanation | |
| 279 | + | --------------------------- | -------------------------------------------------- | |
| 280 | + | 200 (OK) | Token verified, authenticated user's data returned | |
| 281 | + | 401 (Unauthorized) | Missing/invalid/expired JWT | |
| 282 | + | 500 (Internal Server Error) | Database or server error | |
| 283 | + |
| 284 | +### Build Docker |
| 285 | + |
| 286 | +```bash |
| 287 | +# Navigate to the user-service app directory |
| 288 | +cd apps/user-service |
| 289 | +# Build dockerfile after replacing the build arguments (Ensure that your docker daemon is running beforehand) |
| 290 | +docker build -t user-service --build-arg JWT_TOKEN='replace_with_jwt_token' --build-arg DB_CLOUD_URI='replace_with_db_uri_here' -f Dockerfile . |
| 291 | +``` |
0 commit comments