From c49bf65f51e65bed4a7453e08e9ed31b3929c912 Mon Sep 17 00:00:00 2001 From: Ziyad Khalil <36865563+ZiyadKhalil@users.noreply.github.com> Date: Tue, 13 Jul 2021 23:32:19 +0200 Subject: [PATCH 01/13] mmeeting url (#24) --- meetings/src/models/connection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meetings/src/models/connection.ts b/meetings/src/models/connection.ts index 1f82d1e..6baf0ee 100644 --- a/meetings/src/models/connection.ts +++ b/meetings/src/models/connection.ts @@ -1,6 +1,6 @@ import mongoose from "mongoose"; -const uri = "mongodb://meetings-mongo:27017/meetings-test"; +const uri = "mongodb://meetings-db-service:27017/meetings-test"; const connectDb = () => mongoose.connect(uri); From 8636d013af09f97b773c80a10c83f12e1daaca4e Mon Sep 17 00:00:00 2001 From: Ziyad Khalil <36865563+ZiyadKhalil@users.noreply.github.com> Date: Wed, 14 Jul 2021 00:21:09 +0200 Subject: [PATCH 02/13] Meetings url (#25) * mmeeting url * mmeeting url * fix' --- analysis/index.ts | 4 ---- meetings/src/models/connection.ts | 1 - 2 files changed, 5 deletions(-) diff --git a/analysis/index.ts b/analysis/index.ts index 0f08155..06dedda 100644 --- a/analysis/index.ts +++ b/analysis/index.ts @@ -11,10 +11,6 @@ const server = app.listen(port, () => { var socket = require("socket.io"); let io = socket(server); -app.get("/", (req, res) => { - res.sendFile(__dirname + "/index.html"); -}); - var emotions: { [k: string]: { [k: string]: string } } = {}; var instructors: { [k: string]: { [k: string]: number } } = {}; diff --git a/meetings/src/models/connection.ts b/meetings/src/models/connection.ts index 6baf0ee..541f93d 100644 --- a/meetings/src/models/connection.ts +++ b/meetings/src/models/connection.ts @@ -3,5 +3,4 @@ import mongoose from "mongoose"; const uri = "mongodb://meetings-db-service:27017/meetings-test"; const connectDb = () => mongoose.connect(uri); - export { connectDb }; From a69d7a8db4f9224e8197d86d94bc7680d32be146 Mon Sep 17 00:00:00 2001 From: Hager Eldakroury Date: Wed, 14 Jul 2021 00:55:59 +0200 Subject: [PATCH 03/13] fix(analysis): Allow cross-origin --- analysis/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/analysis/index.ts b/analysis/index.ts index 06dedda..166e4cc 100644 --- a/analysis/index.ts +++ b/analysis/index.ts @@ -8,9 +8,11 @@ const port = 4004; const server = app.listen(port, () => { console.log("listening for requests on port 4004,"); }); -var socket = require("socket.io"); -let io = socket(server); - +const io = require("socket.io")(server, { + cors: { + origin: "*", + }, +}); var emotions: { [k: string]: { [k: string]: string } } = {}; var instructors: { [k: string]: { [k: string]: number } } = {}; From 7cb2308d2129392368935eafc1a3388830f3eb72 Mon Sep 17 00:00:00 2001 From: Hager Eldakroury Date: Wed, 14 Jul 2021 12:25:28 +0200 Subject: [PATCH 04/13] fix(meetings): Correct requests to auth --- meetings/src/routes/end.ts | 2 +- meetings/src/routes/join.ts | 2 +- meetings/src/routes/list.ts | 2 +- meetings/src/routes/new.ts | 2 +- meetings/src/routes/start.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/meetings/src/routes/end.ts b/meetings/src/routes/end.ts index 4d3ff4d..8bb2f97 100644 --- a/meetings/src/routes/end.ts +++ b/meetings/src/routes/end.ts @@ -21,7 +21,7 @@ router.post(`${baseUrl}/end/:id`, async (req: Request, res: Response) => { }, }; resV = await axios.post( - "http://auth:3000/api/v1/auth/verify", + "http://auth-service:4002/api/v1/auth/verify", { role: "instructor" }, config ); diff --git a/meetings/src/routes/join.ts b/meetings/src/routes/join.ts index bfabc76..ec7f8d8 100644 --- a/meetings/src/routes/join.ts +++ b/meetings/src/routes/join.ts @@ -19,7 +19,7 @@ router.post(`${baseUrl}/join/:id`, async (req: Request, res: Response) => { }, }; resV = await axios.post( - "http://auth:3000/api/v1/auth/verify", + "http://auth-service:4002/api/v1/auth/verify", { role: "student" }, config ); diff --git a/meetings/src/routes/list.ts b/meetings/src/routes/list.ts index b2f231c..b1bc1e9 100644 --- a/meetings/src/routes/list.ts +++ b/meetings/src/routes/list.ts @@ -19,7 +19,7 @@ router.get(`${baseUrl}/listhost`, async (req: Request, res: Response) => { }, }; resV = await axios.post( - "http://auth:3000/api/v1/auth/verify", + "http://auth-service:4002/api/v1/auth/verify", { role: "instructor" }, config ); diff --git a/meetings/src/routes/new.ts b/meetings/src/routes/new.ts index 3f81009..3aa5e64 100644 --- a/meetings/src/routes/new.ts +++ b/meetings/src/routes/new.ts @@ -30,7 +30,7 @@ router.post( }, }; resV = await axios.post( - "http://auth:3000/api/v1/auth/verify", + "http://auth-service:4002/api/v1/auth/verify", { role: "instructor" }, config ); diff --git a/meetings/src/routes/start.ts b/meetings/src/routes/start.ts index c2a9310..b2b43e0 100644 --- a/meetings/src/routes/start.ts +++ b/meetings/src/routes/start.ts @@ -22,7 +22,7 @@ router.post(`${baseUrl}/start/:id`, async (req: Request, res: Response) => { }, }; resV = await axios.post( - "http://auth:3000/api/v1/auth/verify", + "http://auth-service:4002/api/v1/auth/verify", { role: "instructor" }, config ); From 1d03ef5c50de1d9f7416cddae2f8cddc40c0e067 Mon Sep 17 00:00:00 2001 From: Hager Eldakroury Date: Wed, 14 Jul 2021 15:07:56 +0200 Subject: [PATCH 05/13] fix(server): Handle cors --- auth/index.ts | 5 +++++ auth/package.json | 1 + auth/yarn.lock | 15 ++++++++++++++- meetings/index.ts | 4 ++++ meetings/package.json | 1 + meetings/yarn.lock | 15 ++++++++++++++- 6 files changed, 39 insertions(+), 2 deletions(-) diff --git a/auth/index.ts b/auth/index.ts index a715051..c8eaca3 100644 --- a/auth/index.ts +++ b/auth/index.ts @@ -1,5 +1,6 @@ import express from "express"; import cookieParser from "cookie-parser"; +const cors = require("cors"); import { signupRouter } from "./src/routes/signup"; import { loginRouter } from "./src/routes/login"; @@ -17,6 +18,10 @@ app.use(cookieParser()); app.use(signupRouter); app.use(loginRouter); + +app.use(cors()); +app.options("*", cors()); + app.use(verifyRouter); const port = 4002; diff --git a/auth/package.json b/auth/package.json index b87e690..d15ba0c 100644 --- a/auth/package.json +++ b/auth/package.json @@ -10,6 +10,7 @@ }, "dependencies": { "cookie-parser": "^1.4.5", + "cors": "^2.8.5", "dotenv": "^8.2.0", "express": "^4.17.1", "express-validator": "^6.10.1", diff --git a/auth/yarn.lock b/auth/yarn.lock index c04faab..0e2b889 100644 --- a/auth/yarn.lock +++ b/auth/yarn.lock @@ -646,6 +646,14 @@ core-util-is@~1.0.0: resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac= +cors@^2.8.5: + version "2.8.5" + resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" + integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== + dependencies: + object-assign "^4" + vary "^1" + create-require@^1.1.0: version "1.1.1" resolved "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz" @@ -1980,6 +1988,11 @@ normalize-url@^4.1.0: resolved "https://registry.npmjs.org/normalize-url/-/normalize-url-4.5.0.tgz" integrity sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ== +object-assign@^4: + version "4.1.1" + resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" + integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM= + object-inspect@^1.9.0: version "1.10.2" resolved "https://registry.npmjs.org/object-inspect/-/object-inspect-1.10.2.tgz" @@ -2832,7 +2845,7 @@ validator@^13.5.2: resolved "https://registry.yarnpkg.com/validator/-/validator-13.6.0.tgz#1e71899c14cdc7b2068463cb24c1cc16f6ec7059" integrity sha512-gVgKbdbHgtxpRyR8K0O6oFZPhhB5tT1jeEHZR0Znr9Svg03U0+r9DXWMrnRAB+HtCStDQKlaIZm42tVsVjqtjg== -vary@~1.1.2: +vary@^1, vary@~1.1.2: version "1.1.2" resolved "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz" integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw= diff --git a/meetings/index.ts b/meetings/index.ts index 68465d2..62903c9 100644 --- a/meetings/index.ts +++ b/meetings/index.ts @@ -1,5 +1,6 @@ import express from "express"; import cookieParser from "cookie-parser"; +const cors = require("cors"); import { connectDb } from "./src/models/connection"; @@ -18,6 +19,9 @@ app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(cookieParser()); +app.use(cors()); +app.options("*", cors()); + app.use(newMeetingsRouter); app.use(listMeetingsRouter); app.use(startMeetingsRouter); diff --git a/meetings/package.json b/meetings/package.json index 4d72ed8..38a67fb 100644 --- a/meetings/package.json +++ b/meetings/package.json @@ -11,6 +11,7 @@ "dependencies": { "axios": "^0.21.1", "cookie-parser": "^1.4.5", + "cors": "^2.8.5", "dotenv": "^10.0.0", "express": "^4.17.1", "express-validator": "^6.10.1", diff --git a/meetings/yarn.lock b/meetings/yarn.lock index a0a8bb0..a95401e 100644 --- a/meetings/yarn.lock +++ b/meetings/yarn.lock @@ -665,6 +665,14 @@ core-util-is@~1.0.0: resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac= +cors@^2.8.5: + version "2.8.5" + resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" + integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== + dependencies: + object-assign "^4" + vary "^1" + create-require@^1.1.0: version "1.1.1" resolved "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz" @@ -2017,6 +2025,11 @@ normalize-url@^4.1.0: resolved "https://registry.npmjs.org/normalize-url/-/normalize-url-4.5.0.tgz" integrity sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ== +object-assign@^4: + version "4.1.1" + resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" + integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM= + object-inspect@^1.9.0: version "1.10.2" resolved "https://registry.npmjs.org/object-inspect/-/object-inspect-1.10.2.tgz" @@ -2944,7 +2957,7 @@ validator@^13.5.2: resolved "https://registry.yarnpkg.com/validator/-/validator-13.6.0.tgz#1e71899c14cdc7b2068463cb24c1cc16f6ec7059" integrity sha512-gVgKbdbHgtxpRyR8K0O6oFZPhhB5tT1jeEHZR0Znr9Svg03U0+r9DXWMrnRAB+HtCStDQKlaIZm42tVsVjqtjg== -vary@~1.1.2: +vary@^1, vary@~1.1.2: version "1.1.2" resolved "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz" integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw= From 7a3181ba721ae22f2c1a4bdfdc0d06da3d683e52 Mon Sep 17 00:00:00 2001 From: Hager Eldakroury Date: Wed, 14 Jul 2021 16:21:26 +0200 Subject: [PATCH 06/13] docs: Update readme with deployed urls --- analysis/README.md | 2 + auth/README.md | 6 +-- meetings/README.md | 100 +++++++++++++++++++++++---------------------- 3 files changed, 56 insertions(+), 52 deletions(-) diff --git a/analysis/README.md b/analysis/README.md index 153f878..9814e6a 100644 --- a/analysis/README.md +++ b/analysis/README.md @@ -1,5 +1,7 @@ ### analysis protocols +URL http://134.209.132.84:4004/ + **event: "join"** `socket.emit('join', {sid:room})` diff --git a/auth/README.md b/auth/README.md index 630f205..69c760e 100644 --- a/auth/README.md +++ b/auth/README.md @@ -4,7 +4,7 @@ POST - http://localhost:5000/api/v1/auth/signup + http://178.128.140.169:4002/api/v1/auth/signup - Example Request @@ -47,7 +47,7 @@ POST - http://localhost:5000/api/v1/auth/login + http://178.128.140.169:4002/api/v1/auth/login - Example Request @@ -83,7 +83,7 @@ POST - http://localhost:5000/api/v1/auth/verify + http://178.128.140.169:4002/api/v1/auth/verify - Example Request diff --git a/meetings/README.md b/meetings/README.md index 910f8e8..32fe3c8 100644 --- a/meetings/README.md +++ b/meetings/README.md @@ -5,7 +5,7 @@ POST - [http://localhost:4000/api/v1/meetings/new](http://localhost:4000/api/v1/meetings/new) + http://134.209.132.81:4000/api/v1/meetings/new ``` { @@ -46,7 +46,7 @@ - for both cases(current, incoming), same endpoint is used, no additional param is needed - [http://localhost:4000/api/v1/meetings/join/{id}](http://localhost:4000/api/v1/meetings/join/%7Bid%7D) + http://134.209.132.81:4000/api/v1/meetings/join - Response @@ -78,7 +78,7 @@ POST - [http://localhost:4000/api/v1/meetings/start/{id}](http://localhost:4000/api/v1/meetings/start/%7Bid%7D) + http://134.209.132.81:4000/api/v1/meetings/start/{id} - Response @@ -100,61 +100,63 @@ - list for host - [http://localhost:4000/api/v1/meetings/listhost?status='incoming'](http://localhost:4000/api/v1/meetings/listhost?status=%27incoming%27) | 'ended' - - - default all meetings - - Example response - - ``` - [ - { - "status": "incoming", - "_id": "60c662da67acdd002d0e7087", - "title": "test", - "course": "test", - "host": "60c662da67acdd002d0e7098", - "startTime": "2021-06-10T18:30:35.000Z", - "endTime": "2021-06-10T18:30:35.000Z", - "sid": "RM1b7ecdd56a5f3b43d3b0bd18422e4548", - "__v": 0 - }, - { - "status": "incoming", - "_id": "60c67a68f3e76b010b289c75", - "title": "test2", - "course": "test", - "host": "60c662da67acdd002d0e7098", - "startTime": "2021-06-10T18:30:35.000Z", - "endTime": "2021-06-10T18:30:35.000Z", - "sid": "RM1b7ecdd56a5f3b43d3b0bd18422e4548", - "__v": 0 - }, - { - "status": "incoming", - "_id": "60c67aa5f3e76b010b289c76", - "title": "test22", - "course": "test", - "host": "60c662da67acdd002d0e7098", - "startTime": "2021-06-10T18:30:35.000Z", - "endTime": "2021-06-10T18:30:35.000Z", - "sid": "RM1b7ecdd56a5f3b43d3b0bd18422e4548", - "__v": 0 - } - ] - - ``` + http://134.209.132.81:4000/api/v1/meetings/listhost?status='incoming' + http://134.209.132.81:4000/api/v1/meetings/listhost?status='ended' + + - default all meetings + - Example response + + ``` + [ + { + "status": "incoming", + "_id": "60c662da67acdd002d0e7087", + "title": "test", + "course": "test", + "host": "60c662da67acdd002d0e7098", + "startTime": "2021-06-10T18:30:35.000Z", + "endTime": "2021-06-10T18:30:35.000Z", + "sid": "RM1b7ecdd56a5f3b43d3b0bd18422e4548", + "__v": 0 + }, + { + "status": "incoming", + "_id": "60c67a68f3e76b010b289c75", + "title": "test2", + "course": "test", + "host": "60c662da67acdd002d0e7098", + "startTime": "2021-06-10T18:30:35.000Z", + "endTime": "2021-06-10T18:30:35.000Z", + "sid": "RM1b7ecdd56a5f3b43d3b0bd18422e4548", + "__v": 0 + }, + { + "status": "incoming", + "_id": "60c67aa5f3e76b010b289c76", + "title": "test22", + "course": "test", + "host": "60c662da67acdd002d0e7098", + "startTime": "2021-06-10T18:30:35.000Z", + "endTime": "2021-06-10T18:30:35.000Z", + "sid": "RM1b7ecdd56a5f3b43d3b0bd18422e4548", + "__v": 0 + } + ] + + ``` - list for students - [http://localhost:4000/api/v1/meetings/liststudent?status='incoming'](http://localhost:4000/api/v1/meetings/liststudent?status=%27incoming%27) | 'ended' + http://134.209.132.81:4000/api/v1/meetings/liststudent?status='incoming' + http://134.209.132.81:4000/api/v1/meetings/liststudent?status='ended' - - default all meetings + - default all meetings - **end** POST - [http://localhost:4000/api/v1/meetings/end/{id}](http://localhost:4000/api/v1/meetings/end/%7Bid%7D) + http://134.209.132.81:4000/api/v1/meetings/end/{id} - Response From 0065f0a4dcc3e80ff812cace10e412fd03ab383e Mon Sep 17 00:00:00 2001 From: Hager Eldakroury Date: Thu, 15 Jul 2021 00:06:25 +0200 Subject: [PATCH 07/13] fix(auth): Standard endpoints response --- auth/README.md | 21 +++++++++++++++++++-- auth/src/models/attributes.ts | 2 ++ auth/src/models/schema.ts | 4 ++++ auth/src/routes/login.ts | 8 +++++--- auth/src/routes/signup.ts | 18 +++++++++++++----- auth/src/routes/verify.ts | 10 +++++++++- 6 files changed, 52 insertions(+), 11 deletions(-) diff --git a/auth/README.md b/auth/README.md index 69c760e..f1ec024 100644 --- a/auth/README.md +++ b/auth/README.md @@ -13,6 +13,7 @@ ```json { + "username": "hager", "email": "test@test.com", "password": "12345678", "role": "student" @@ -27,7 +28,10 @@ ```json { - "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImhhZ2VyQHRlc3QuY29tIiwicm9sZSI6Imluc3RydWN0b3IiLCJpYXQiOjE2MjM2MjMyOTYsImV4cCI6MTYyNDA1NTI5Nn0.0UmSsZCKkbs2zfcJJN6-1h3T1E6DKkAe9Hw5v7uT9zk" + "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImhhZ2VyQHRlc3QuY29tIiwicm9sZSI6Imluc3RydWN0b3IiLCJpYXQiOjE2MjM2MjMyOTYsImV4cCI6MTYyNDA1NTI5Nn0.0UmSsZCKkbs2zfcJJN6-1h3T1E6DKkAe9Hw5v7uT9zk", + "username": "hager", + "email": "test@test.com", + "role": "student" } ``` @@ -53,6 +57,7 @@ ```json { + "username": "hager", "email": "test@test.com", "password": "12345678" } @@ -67,7 +72,9 @@ ```json { "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImhhZ2VyQHRlc3QuY29tIiwicm9sZSI6Imluc3RydWN0b3IiLCJpYXQiOjE2MjM2MjMyOTYsImV4cCI6MTYyNDA1NTI5Nn0.0UmSsZCKkbs2zfcJJN6-1h3T1E6DKkAe9Hw5v7uT9zk", - "role": "instructor" + "username": "hager", + "email": "test@test.com", + "role": "student" } ``` @@ -98,6 +105,16 @@ - Response + ```json + { + "message": "Verified", + "_id": "60ef5d2c0a179c0f3819d528", + "username": "fhdnkn", + "email": "hagerashrakat@test.com", + "role": "instructor" + } + ``` + - verified 200 - verified diff --git a/auth/src/models/attributes.ts b/auth/src/models/attributes.ts index 9260ef8..2e1a157 100644 --- a/auth/src/models/attributes.ts +++ b/auth/src/models/attributes.ts @@ -2,6 +2,7 @@ import mongoose from "mongoose"; // An interface to describe the types of a user attributes interface UserAttributes { + username: string; email: string; password: string; role: string; @@ -9,6 +10,7 @@ interface UserAttributes { // An interface that keeps tracks of any values added by mongo interface UserDocument extends mongoose.Document { + username: string; email: string; password: string; role: string; diff --git a/auth/src/models/schema.ts b/auth/src/models/schema.ts index 46c1aad..3368aab 100644 --- a/auth/src/models/schema.ts +++ b/auth/src/models/schema.ts @@ -1,6 +1,10 @@ import mongoose from "mongoose"; const schema = new mongoose.Schema({ + username: { + type: String, + required: true, + }, email: { type: String, required: true, diff --git a/auth/src/routes/login.ts b/auth/src/routes/login.ts index e6d0685..0f13b49 100644 --- a/auth/src/routes/login.ts +++ b/auth/src/routes/login.ts @@ -7,7 +7,7 @@ import { baseUrl } from "../config"; const router = express.Router(); router.post(`${baseUrl}/login`, async (req, res) => { - const { email, password } = req.body; + const { username, email, password } = req.body; try { const user = await User.findOne({ email }); @@ -21,9 +21,11 @@ router.post(`${baseUrl}/login`, async (req, res) => { } const { role } = user; - const userJWT = createAccessToken({ email, role }); + const userJWT = createAccessToken({ username, email, role }); - return res.status(201).send({ token: userJWT, role: role }); + return res + .status(201) + .send({ token: userJWT, username: username, email: email, role: role }); } catch (error) { return res.status(400).send(error.message); } diff --git a/auth/src/routes/signup.ts b/auth/src/routes/signup.ts index f560425..750587d 100644 --- a/auth/src/routes/signup.ts +++ b/auth/src/routes/signup.ts @@ -9,6 +9,7 @@ const router = express.Router(); router.post( `${baseUrl}/signup`, [ + body("username").not().isEmpty().withMessage("Username is required"), body("email").isEmail().withMessage("Email not Valid"), body("password") .trim() @@ -26,7 +27,7 @@ router.post( return res.send(errors.array({ onlyFirstError: true })[0].msg); } - const { email, password, role } = req.body; + const { username, email, password, role } = req.body; try { // see if the user exists @@ -35,13 +36,20 @@ router.post( } // making a new user and saving them into the database - const newUser = await makeUser({ email, password, role }); + const newUser = await makeUser({ username, email, password, role }); await newUser.save(); // consider user signed in - const userJWT = createAccessToken({ _id: newUser._id, email, role }); - - return res.status(201).send({ token: userJWT }); + const userJWT = createAccessToken({ + _id: newUser._id, + username, + email, + role, + }); + + return res + .status(201) + .send({ token: userJWT, username: username, email: email, role: role }); } catch (error) { return res.status(400).send(error.message); } diff --git a/auth/src/routes/verify.ts b/auth/src/routes/verify.ts index 3f308a7..c81b53d 100644 --- a/auth/src/routes/verify.ts +++ b/auth/src/routes/verify.ts @@ -20,7 +20,15 @@ router.post(`${baseUrl}/verify`, (req, res) => { if (role && payload.role != role) { return res.status(400).send("Role not authorized"); } - return res.status(200).send({ message: "Verified", _id: payload._id }); + return res + .status(200) + .send({ + message: "Verified", + _id: payload._id, + username: payload.username, + email: payload.email, + role: payload.role, + }); } catch (err) { return res.status(400).send("Invalid Token"); } From c3179bcb046a35e851d5b5bfda667bae4c5e6328 Mon Sep 17 00:00:00 2001 From: Hager Eldakroury Date: Thu, 15 Jul 2021 00:43:41 +0200 Subject: [PATCH 08/13] fix(auth): cors config --- auth/index.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/auth/index.ts b/auth/index.ts index c8eaca3..5764482 100644 --- a/auth/index.ts +++ b/auth/index.ts @@ -16,12 +16,11 @@ app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(cookieParser()); +app.use(cors()); + app.use(signupRouter); app.use(loginRouter); -app.use(cors()); -app.options("*", cors()); - app.use(verifyRouter); const port = 4002; From eda8d0233a2126e6b7cd2c6d11bdaab4ea4ce604 Mon Sep 17 00:00:00 2001 From: Hager Eldakroury Date: Thu, 15 Jul 2021 01:05:35 +0200 Subject: [PATCH 09/13] fix: Correct erros status --- auth/src/routes/signup.ts | 4 +++- meetings/src/routes/new.ts | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/auth/src/routes/signup.ts b/auth/src/routes/signup.ts index 750587d..892fc7e 100644 --- a/auth/src/routes/signup.ts +++ b/auth/src/routes/signup.ts @@ -24,7 +24,9 @@ router.post( const errors = validationResult(req); if (!errors.isEmpty()) { - return res.send(errors.array({ onlyFirstError: true })[0].msg); + return res + .status(400) + .send(errors.array({ onlyFirstError: true })[0].msg); } const { username, email, password, role } = req.body; diff --git a/meetings/src/routes/new.ts b/meetings/src/routes/new.ts index 3aa5e64..6f4ae71 100644 --- a/meetings/src/routes/new.ts +++ b/meetings/src/routes/new.ts @@ -17,7 +17,9 @@ router.post( const errors = validationResult(req); if (!errors.isEmpty()) { - return res.send(errors.array({ onlyFirstError: true })[0].msg); + return res + .status(400) + .send(errors.array({ onlyFirstError: true })[0].msg); } let resV; From 5063db062ea7bf59a720d1fff1b74fb01fd8e3f2 Mon Sep 17 00:00:00 2001 From: Hager Eldakroury Date: Thu, 15 Jul 2021 01:23:39 +0200 Subject: [PATCH 10/13] fix(auth): username not required --- auth/src/models/connection.ts | 3 ++- auth/src/routes/login.ts | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/auth/src/models/connection.ts b/auth/src/models/connection.ts index 09a86f6..3bdcd29 100644 --- a/auth/src/models/connection.ts +++ b/auth/src/models/connection.ts @@ -1,6 +1,7 @@ import mongoose from "mongoose"; -const uri = "mongodb://auth-db-service:27017/auth-test"; +const uri = + "mongodb+srv://hager:hagercontactsapp@cluster0.i4gfy.mongodb.net/contactsapp?retryWrites=true&w=majority"; const connectDb = () => mongoose.connect(uri); diff --git a/auth/src/routes/login.ts b/auth/src/routes/login.ts index 0f13b49..e60e50e 100644 --- a/auth/src/routes/login.ts +++ b/auth/src/routes/login.ts @@ -7,7 +7,7 @@ import { baseUrl } from "../config"; const router = express.Router(); router.post(`${baseUrl}/login`, async (req, res) => { - const { username, email, password } = req.body; + const { email, password } = req.body; try { const user = await User.findOne({ email }); @@ -19,7 +19,7 @@ router.post(`${baseUrl}/login`, async (req, res) => { if (!passValid) { return res.status(400).send("Password is not correct"); } - const { role } = user; + const { role, username } = user; const userJWT = createAccessToken({ username, email, role }); From b5fba7bc62f6a20c41c7f49b4aba33d14717e637 Mon Sep 17 00:00:00 2001 From: Hager Eldakroury Date: Thu, 15 Jul 2021 01:31:39 +0200 Subject: [PATCH 11/13] fix(auth): Make verify GET' --- auth/README.md | 3 +-- auth/src/routes/verify.ts | 18 ++++++++---------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/auth/README.md b/auth/README.md index f1ec024..4a1d9cc 100644 --- a/auth/README.md +++ b/auth/README.md @@ -57,7 +57,6 @@ ```json { - "username": "hager", "email": "test@test.com", "password": "12345678" } @@ -88,7 +87,7 @@ - **verify** - POST + GET http://178.128.140.169:4002/api/v1/auth/verify diff --git a/auth/src/routes/verify.ts b/auth/src/routes/verify.ts index c81b53d..a10d17a 100644 --- a/auth/src/routes/verify.ts +++ b/auth/src/routes/verify.ts @@ -7,7 +7,7 @@ dotenv.config(); const router = express.Router(); -router.post(`${baseUrl}/verify`, (req, res) => { +router.get(`${baseUrl}/verify`, (req, res) => { const token = req.header("Authorization")?.split(" ")[1]; const role = req.body.role; if (!token) { @@ -20,15 +20,13 @@ router.post(`${baseUrl}/verify`, (req, res) => { if (role && payload.role != role) { return res.status(400).send("Role not authorized"); } - return res - .status(200) - .send({ - message: "Verified", - _id: payload._id, - username: payload.username, - email: payload.email, - role: payload.role, - }); + return res.status(200).send({ + message: "Verified", + _id: payload._id, + username: payload.username, + email: payload.email, + role: payload.role, + }); } catch (err) { return res.status(400).send("Invalid Token"); } From a95bfb9a49d3f4930b22c29741412eeac40682fe Mon Sep 17 00:00:00 2001 From: Hager Eldakroury Date: Thu, 15 Jul 2021 01:46:18 +0200 Subject: [PATCH 12/13] fix(auth): Correct database uri --- auth/src/models/connection.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/auth/src/models/connection.ts b/auth/src/models/connection.ts index 3bdcd29..6f2dc33 100644 --- a/auth/src/models/connection.ts +++ b/auth/src/models/connection.ts @@ -1,8 +1,6 @@ import mongoose from "mongoose"; -const uri = - "mongodb+srv://hager:hagercontactsapp@cluster0.i4gfy.mongodb.net/contactsapp?retryWrites=true&w=majority"; - +const uri = "mongodb://auth-db-service:27017/auth-test"; const connectDb = () => mongoose.connect(uri); export { connectDb }; From a2f2399cc166f03e0a61a5c610242eaa47771c66 Mon Sep 17 00:00:00 2001 From: Hager Eldakroury Date: Thu, 15 Jul 2021 05:14:47 +0200 Subject: [PATCH 13/13] fix(meetings): Merge list endpoints --- meetings/src/routes/list.ts | 97 ++++++++++++++----------------------- 1 file changed, 36 insertions(+), 61 deletions(-) diff --git a/meetings/src/routes/list.ts b/meetings/src/routes/list.ts index b1bc1e9..132ac91 100644 --- a/meetings/src/routes/list.ts +++ b/meetings/src/routes/list.ts @@ -6,7 +6,7 @@ import axios from "axios"; const router = express.Router(); -router.get(`${baseUrl}/listhost`, async (req: Request, res: Response) => { +router.get(`${baseUrl}/list`, async (req: Request, res: Response) => { const status = req.query.status; let resV; @@ -20,7 +20,6 @@ router.get(`${baseUrl}/listhost`, async (req: Request, res: Response) => { }; resV = await axios.post( "http://auth-service:4002/api/v1/auth/verify", - { role: "instructor" }, config ); } catch (err) { @@ -31,70 +30,46 @@ router.get(`${baseUrl}/listhost`, async (req: Request, res: Response) => { } const user = resV.data._id; + const role = resV.data.role; - let meetings; + if (role == "instructor") { + let meetings; - try { - if (status == "ended" || status == "incoming") { - meetings = await Meeting.find({ host: user, status: status }); - } else { - meetings = await Meeting.find({ host: user }); - } - - if (!meetings) { - return res.status(201).send("No meetings found"); - } - - return res.status(201).send(meetings); - } catch (error) { - return res.status(400); - } -}); - -router.get(`${baseUrl}/liststudent`, async (req: Request, res: Response) => { - const status = req.query.status; - - let resV; - //validating the user's token - try { - const token = req.header("Authorization")?.split(" ")[1]; - let config = { - headers: { - Authorization: "Bearer " + token, - }, - }; - resV = await axios.post( - "http://auth:3000/api/v1/auth/verify", - { role: "student" }, - config - ); - } catch (err) { - return res.status(400).send("User is not authorized"); - } - if (resV.status == 400) { - return res.status(400).send("User is not authorized"); - } - - const user = resV.data._id; + try { + if (status == "ended" || status == "incoming") { + meetings = await Meeting.find({ host: user, status: status }); + } else { + meetings = await Meeting.find({ host: user }); + } - let userMeetings; + if (!meetings) { + return res.status(201).send("No meetings found"); + } - try { - const userMeeting = await UserMeetings.findOne({ name: user }); - const userMeetingsIds = userMeeting?.meetings; - - if (status === "ended" || status === "incoming") { - userMeetings = await Meeting.find({ - _id: { $in: userMeetingsIds }, - status: status, - }); - } else { - userMeetings = await Meeting.find({ _id: { $in: userMeetingsIds } }); + return res.status(201).send(meetings); + } catch (error) { + return res.status(400); + } + } else if (role == "student") { + let userMeetings; + + try { + const userMeeting = await UserMeetings.findOne({ name: user }); + const userMeetingsIds = userMeeting?.meetings; + + if (status === "ended" || status === "incoming") { + userMeetings = await Meeting.find({ + _id: { $in: userMeetingsIds }, + status: status, + }); + } else { + userMeetings = await Meeting.find({ _id: { $in: userMeetingsIds } }); + } + + return res.status(201).send(userMeetings); + } catch (error) { + return res.status(400); } - - return res.status(201).send(userMeetings); - } catch (error) { - return res.status(400); } });