Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Edugaze-Inc/edugaze-be into…
Browse files Browse the repository at this point in the history
… main
  • Loading branch information
ziyadkhalil committed Jul 15, 2021
2 parents ee6a102 + a2f2399 commit 2287f6e
Show file tree
Hide file tree
Showing 22 changed files with 196 additions and 143 deletions.
2 changes: 2 additions & 0 deletions analysis/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
### analysis protocols

URL http://134.209.132.84:4004/

**event: "join"**

`socket.emit('join', {sid:room})`
Expand Down
10 changes: 4 additions & 6 deletions analysis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +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);

app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
const io = require("socket.io")(server, {
cors: {
origin: "*",
},
});

var emotions: { [k: string]: { [k: string]: string } } = {};
var instructors: { [k: string]: { [k: string]: number } } = {};

Expand Down
28 changes: 22 additions & 6 deletions auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

POST

http://localhost:5000/api/v1/auth/signup
http://178.128.140.169:4002/api/v1/auth/signup

- Example Request

Expand All @@ -13,6 +13,7 @@

```json
{
"username": "hager",
"email": "[email protected]",
"password": "12345678",
"role": "student"
Expand All @@ -27,7 +28,10 @@

```json
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImhhZ2VyQHRlc3QuY29tIiwicm9sZSI6Imluc3RydWN0b3IiLCJpYXQiOjE2MjM2MjMyOTYsImV4cCI6MTYyNDA1NTI5Nn0.0UmSsZCKkbs2zfcJJN6-1h3T1E6DKkAe9Hw5v7uT9zk"
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImhhZ2VyQHRlc3QuY29tIiwicm9sZSI6Imluc3RydWN0b3IiLCJpYXQiOjE2MjM2MjMyOTYsImV4cCI6MTYyNDA1NTI5Nn0.0UmSsZCKkbs2zfcJJN6-1h3T1E6DKkAe9Hw5v7uT9zk",
"username": "hager",
"email": "[email protected]",
"role": "student"
}
```

Expand All @@ -47,7 +51,7 @@

POST

http://localhost:5000/api/v1/auth/login
http://178.128.140.169:4002/api/v1/auth/login

- Example Request

Expand All @@ -67,7 +71,9 @@
```json
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImhhZ2VyQHRlc3QuY29tIiwicm9sZSI6Imluc3RydWN0b3IiLCJpYXQiOjE2MjM2MjMyOTYsImV4cCI6MTYyNDA1NTI5Nn0.0UmSsZCKkbs2zfcJJN6-1h3T1E6DKkAe9Hw5v7uT9zk",
"role": "instructor"
"username": "hager",
"email": "[email protected]",
"role": "student"
}
```

Expand All @@ -81,9 +87,9 @@

- **verify**

POST
GET

http://localhost:5000/api/v1/auth/verify
http://178.128.140.169:4002/api/v1/auth/verify

- Example Request

Expand All @@ -98,6 +104,16 @@

- Response

```json
{
"message": "Verified",
"_id": "60ef5d2c0a179c0f3819d528",
"username": "fhdnkn",
"email": "[email protected]",
"role": "instructor"
}
```

- verified

200 - verified
Expand Down
4 changes: 4 additions & 0 deletions auth/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -15,8 +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(verifyRouter);

const port = 4002;
Expand Down
1 change: 1 addition & 0 deletions auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions auth/src/models/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import mongoose from "mongoose";

// An interface to describe the types of a user attributes
interface UserAttributes {
username: string;
email: string;
password: string;
role: string;
}

// An interface that keeps tracks of any values added by mongo
interface UserDocument extends mongoose.Document {
username: string;
email: string;
password: string;
role: string;
Expand Down
1 change: 0 additions & 1 deletion auth/src/models/connection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import mongoose from "mongoose";

const uri = "mongodb://auth-db-service:27017/auth-test";

const connectDb = () => mongoose.connect(uri);

export { connectDb };
4 changes: 4 additions & 0 deletions auth/src/models/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import mongoose from "mongoose";

const schema = new mongoose.Schema({
username: {
type: String,
required: true,
},
email: {
type: String,
required: true,
Expand Down
8 changes: 5 additions & 3 deletions auth/src/routes/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ 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({ 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);
}
Expand Down
22 changes: 16 additions & 6 deletions auth/src/routes/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -23,10 +24,12 @@ 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 { email, password, role } = req.body;
const { username, email, password, role } = req.body;

try {
// see if the user exists
Expand All @@ -35,13 +38,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);
}
Expand Down
10 changes: 8 additions & 2 deletions auth/src/routes/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -20,7 +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 });
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");
}
Expand Down
15 changes: 14 additions & 1 deletion auth/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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=
Expand Down
Loading

0 comments on commit 2287f6e

Please sign in to comment.