|
1 | 1 | # User Service Guide
|
2 | 2 |
|
3 |
| -## Setting-up |
| 3 | +## Setting-up MongoDB |
4 | 4 |
|
5 | 5 | > :notebook: If you are familiar to MongoDB and wish to use a local instance, please feel free to do so. This guide utilizes MongoDB Cloud Services.
|
6 | 6 |
|
7 |
| -1. Set up a MongoDB Shared Cluster by following the steps in this [Guide](./MongoDBSetup.md). |
| 7 | +1. Set up a MongoDB Shared Cluster by following the steps in this [Guide](MongoDBSetup.md). |
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 | 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 |
|
25 |
| -6. Update the `DB_CLOUD_URI` of the `.env` file, and paste the string we copied earlier in step 4. Also remember to replace the `<password>` placeholder with the actual password. |
| 25 | +6. Update the `DB_CLOUD_URI` of the `.env` file, and paste the string we copied earlier in step 4. |
26 | 26 |
|
27 |
| -## Running User Service |
28 |
| - |
29 |
| -1. Open Command Line/Terminal and navigate into the `user-service` directory. |
30 |
| - |
31 |
| -2. Run the command: `npm install`. This will install all the necessary dependencies. |
32 |
| - |
33 |
| -3. If you are running the user service for the first time, run `npm run seed`, to seed the database with a default admin account. If you wish to change the default, please update the `.env` file. Alternatively, you can also edit your credentials and user profile after you have created the default account. |
34 |
| - |
35 |
| -4. Run the command `npm start` to start the User Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes. |
36 |
| - |
37 |
| -5. Using applications like Postman, you can interact with the User Service on port 3001. If you wish to change this, please update the `.env` file. |
38 |
| - |
39 |
| -## User Service API Guide |
| 27 | +## Setting-up Firebase |
40 | 28 |
|
41 |
| -### Create User |
| 29 | +1. Go to https://console.firebase.google.com/u/0/. |
42 | 30 |
|
43 |
| -- This endpoint allows adding a new user to the database (i.e., user registration). |
| 31 | +2. Create a project and choose a project name. Navigate to `Storage` and click on it to activate it. |
44 | 32 |
|
45 |
| -- HTTP Method: `POST` |
| 33 | +3. Select `Start in production mode` and your preferred cloud storage region. |
46 | 34 |
|
47 |
| -- Endpoint: http://localhost:3001/users |
48 |
| - |
49 |
| -- Body |
50 |
| - |
51 |
| - - Required: `firstName` (string), `lastName` (string), `username` (string), `email` (string), `password` (string) |
52 |
| - |
53 |
| - ```json |
54 |
| - { |
55 |
| - "firstName": "SampleFirstName", |
56 |
| - "lastName": "SampleLastName", |
57 |
| - "username": "SampleUserName", |
58 |
| - |
59 |
| - "password": "SecurePassword" |
60 |
| - } |
| 35 | +4. After Storage is created, go to `Rules` section and set rule to: |
61 | 36 | ```
|
62 |
| - |
63 |
| -- Responses: |
64 |
| - |
65 |
| - | Response Code | Explanation | |
66 |
| - | --------------------------- | ----------------------------------------------------- | |
67 |
| - | 201 (Created) | User created successfully, created user data returned | |
68 |
| - | 400 (Bad Request) | Missing fields | |
69 |
| - | 409 (Conflict) | Duplicate username or email encountered | |
70 |
| - | 500 (Internal Server Error) | Database or server error | |
71 |
| - |
72 |
| -### Get User |
73 |
| - |
74 |
| -- This endpoint allows retrieval of a single user's data from the database using the user's ID. |
75 |
| - |
76 |
| - > :bulb: The user ID refers to the MongoDB Object ID, a unique identifier automatically generated by MongoDB for each document in a collection. |
77 |
| - |
78 |
| -- HTTP Method: `GET` |
79 |
| - |
80 |
| -- Endpoint: http://localhost:3001/users/{userId} |
81 |
| - |
82 |
| -- Parameters |
83 |
| - |
84 |
| - - Required: `userId` path parameter |
85 |
| - - Example: `http://localhost:3001/users/60c72b2f9b1d4c3a2e5f8b4c` |
86 |
| - |
87 |
| -- <a name="auth-header">Headers</a> |
88 |
| - |
89 |
| - - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
90 |
| - |
91 |
| - - 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. |
92 |
| - |
93 |
| - - Auth Rules: |
94 |
| - |
95 |
| - - 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. |
96 |
| - - 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. |
97 |
| - |
98 |
| -- Responses: |
99 |
| - |
100 |
| - | Response Code | Explanation | |
101 |
| - | --------------------------- | -------------------------------------------------------- | |
102 |
| - | 200 (OK) | Success, user data returned | |
103 |
| - | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
104 |
| - | 403 (Forbidden) | Access denied for non-admin users accessing others' data | |
105 |
| - | 404 (Not Found) | User with the specified ID not found | |
106 |
| - | 500 (Internal Server Error) | Database or server error | |
107 |
| - |
108 |
| -### Get All Users |
109 |
| - |
110 |
| -- This endpoint allows retrieval of all users' data from the database. |
111 |
| -- HTTP Method: `GET` |
112 |
| -- Endpoint: http://localhost:3001/users |
113 |
| -- Headers |
114 |
| - |
115 |
| - - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
116 |
| - - Auth Rules: |
117 |
| - |
118 |
| - - 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. |
119 |
| - - Non-admin users: Not allowed access. |
120 |
| - |
121 |
| -- Responses: |
122 |
| - |
123 |
| - | Response Code | Explanation | |
124 |
| - | --------------------------- | ------------------------------------------------ | |
125 |
| - | 200 (OK) | Success, all user data returned | |
126 |
| - | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
127 |
| - | 403 (Forbidden) | Access denied for non-admin users | |
128 |
| - | 500 (Internal Server Error) | Database or server error | |
129 |
| - |
130 |
| -### Update User |
131 |
| - |
132 |
| -- This endpoint allows updating a user and their related data in the database using the user's ID. |
133 |
| - |
134 |
| -- HTTP Method: `PATCH` |
135 |
| - |
136 |
| -- Endpoint: http://localhost:3001/users/{userId} |
137 |
| - |
138 |
| -- Parameters |
139 |
| - |
140 |
| - - Required: `userId` path parameter |
141 |
| - |
142 |
| -- Body |
143 |
| - |
144 |
| - - At least one of the following fields is required: `username` (string), `email` (string), `password` (string) |
145 |
| - |
146 |
| - ```json |
147 |
| - { |
148 |
| - "username": "SampleUserName", |
149 |
| - |
150 |
| - "password": "SecurePassword" |
151 |
| - } |
152 |
| - ``` |
153 |
| - |
154 |
| -- Headers |
155 |
| - |
156 |
| - - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
157 |
| - - Auth Rules: |
158 |
| - |
159 |
| - - 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. |
160 |
| - - 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. |
161 |
| - |
162 |
| -- Responses: |
163 |
| - |
164 |
| - | Response Code | Explanation | |
165 |
| - | --------------------------- | ------------------------------------------------------- | |
166 |
| - | 200 (OK) | User updated successfully, updated user data returned | |
167 |
| - | 400 (Bad Request) | Missing fields | |
168 |
| - | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
169 |
| - | 403 (Forbidden) | Access denied for non-admin users updating others' data | |
170 |
| - | 404 (Not Found) | User with the specified ID not found | |
171 |
| - | 409 (Conflict) | Duplicate username or email encountered | |
172 |
| - | 500 (Internal Server Error) | Database or server error | |
173 |
| - |
174 |
| -### Update User Privilege |
175 |
| - |
176 |
| -- This endpoint allows updating a user’s privilege, i.e., promoting or demoting them from admin status. |
177 |
| - |
178 |
| -- HTTP Method: `PATCH` |
179 |
| - |
180 |
| -- Endpoint: http://localhost:3001/users/{userId} |
181 |
| - |
182 |
| -- Parameters |
183 |
| - |
184 |
| - - Required: `userId` path parameter |
185 |
| - |
186 |
| -- Body |
187 |
| - |
188 |
| - - Required: `isAdmin` (boolean) |
189 |
| - |
190 |
| - ```json |
191 |
| - { |
192 |
| - "isAdmin": true |
| 37 | + rules_version = '2'; |
| 38 | + service firebase.storage { |
| 39 | + match /b/{bucket}/o { |
| 40 | + match /{allPaths=**} { |
| 41 | + allow read: if true; |
| 42 | + allow write: if request.auth != null; |
| 43 | + } |
| 44 | + } |
193 | 45 | }
|
194 | 46 | ```
|
| 47 | + This rule ensures that only verified users can upload images while ensuring that URLs of images are public. Remember to click `Publish` to save changes. |
195 | 48 |
|
196 |
| -- Headers |
197 |
| - |
198 |
| - - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
199 |
| - - Auth Rules: |
200 |
| - |
201 |
| - - 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. |
202 |
| - - Non-admin users: Not allowed access. |
203 |
| - |
204 |
| -> :bulb: You may need to manually assign admin status to the first user by directly editing the database document before using this endpoint. |
205 |
| - |
206 |
| -- Responses: |
| 49 | +5. Go to `Settings`, `Project settings`, `Service accounts` and click `Generate new private key`. This will download a `.json` file, which will contain your credentials. |
207 | 50 |
|
208 |
| - | Response Code | Explanation | |
209 |
| - | --------------------------- | --------------------------------------------------------------- | |
210 |
| - | 200 (OK) | User privilege updated successfully, updated user data returned | |
211 |
| - | 400 (Bad Request) | Missing fields | |
212 |
| - | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
213 |
| - | 403 (Forbidden) | Access denied for non-admin users | |
214 |
| - | 404 (Not Found) | User with the specified ID not found | |
215 |
| - | 500 (Internal Server Error) | Database or server error | |
| 51 | +6. In `.env` of question service, replace: |
| 52 | + - `FIREBASE_PROJECT_ID` with `project_id` found in the downloaded json file. |
| 53 | + - `FIREBASE_PRIVATE_KEY` with `private_key` found in the downloaded json file. |
| 54 | + - `FIREBASE_CLIENT_EMAIL` with `client_email` found in the downloaded json file. |
| 55 | + - `FIREBASE_STORAGE_BUCKET` with the folder path of the Storage. It should look something like `gs://<appname>.appspot.com`. |
216 | 56 |
|
217 |
| -### Delete User |
218 |
| - |
219 |
| -- This endpoint allows deletion of a user and their related data from the database using the user's ID. |
220 |
| -- HTTP Method: `DELETE` |
221 |
| -- Endpoint: http://localhost:3001/users/{userId} |
222 |
| -- Parameters |
223 |
| - |
224 |
| - - Required: `userId` path parameter |
225 |
| - |
226 |
| -- Headers |
227 |
| - |
228 |
| - - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
229 |
| - |
230 |
| - - Auth Rules: |
231 |
| - |
232 |
| - - 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. |
233 |
| - |
234 |
| - - 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. |
235 |
| - |
236 |
| -- Responses: |
237 |
| - |
238 |
| - | Response Code | Explanation | |
239 |
| - | --------------------------- | ------------------------------------------------------- | |
240 |
| - | 200 (OK) | User deleted successfully | |
241 |
| - | 401 (Unauthorized) | Access denied due to missing/invalid/expired JWT | |
242 |
| - | 403 (Forbidden) | Access denied for non-admin users deleting others' data | |
243 |
| - | 404 (Not Found) | User with the specified ID not found | |
244 |
| - | 500 (Internal Server Error) | Database or server error | |
245 |
| - |
246 |
| -### Login |
247 |
| - |
248 |
| -- 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). |
249 |
| -- HTTP Method: `POST` |
250 |
| -- Endpoint: http://localhost:3001/auth/login |
251 |
| -- Body |
252 |
| - |
253 |
| - - Required: `email` (string), `password` (string) |
254 |
| - |
255 |
| - ```json |
256 |
| - { |
257 |
| - |
258 |
| - "password": "SecurePassword" |
259 |
| - } |
260 |
| - ``` |
261 |
| - |
262 |
| -- Responses: |
| 57 | +## Running User Service |
263 | 58 |
|
264 |
| - | Response Code | Explanation | |
265 |
| - | --------------------------- | -------------------------------------------------- | |
266 |
| - | 200 (OK) | Login successful, JWT token and user data returned | |
267 |
| - | 400 (Bad Request) | Missing fields | |
268 |
| - | 401 (Unauthorized) | Incorrect email or password | |
269 |
| - | 500 (Internal Server Error) | Database or server error | |
| 59 | +1. Open Command Line/Terminal and navigate into the `user-service` directory. |
270 | 60 |
|
271 |
| -### Verify Token |
| 61 | +2. Run the command: `npm install`. This will install all the necessary dependencies. |
272 | 62 |
|
273 |
| -- This endpoint allows one to verify a JWT access token to authenticate and retrieve the user's data associated with the token. |
274 |
| -- HTTP Method: `GET` |
275 |
| -- Endpoint: http://localhost:3001/auth/verify-token |
276 |
| -- Headers |
| 63 | +3. If you are running the user service for the first time, run `npm run seed`, to seed the database with a default admin account. If you wish to change the default, please update the `.env` file. Alternatively, you can also edit your credentials and user profile after you have created the default account. |
277 | 64 |
|
278 |
| - - Required: `Authorization: Bearer <JWT_ACCESS_TOKEN>` |
| 65 | +4. Run the command `npm start` to start the User Service in production mode, or use `npm run dev` for development mode, which includes features like automatic server restart when you make code changes. |
279 | 66 |
|
280 |
| -- Responses: |
| 67 | +5. To view User Service documentation, go to http://localhost:3001/docs. |
281 | 68 |
|
282 |
| - | Response Code | Explanation | |
283 |
| - | --------------------------- | -------------------------------------------------- | |
284 |
| - | 200 (OK) | Token verified, authenticated user's data returned | |
285 |
| - | 401 (Unauthorized) | Missing/invalid/expired JWT | |
286 |
| - | 500 (Internal Server Error) | Database or server error | |
| 69 | +6. Using applications like Postman, you can interact with the User Service on port 3001. If you wish to change this, please update the `.env` file. |
0 commit comments