Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 5b7f0c4

Browse files
authored
Merge pull request #12 from rohan-ramakrishnan/master
Adding courses functionality
2 parents ce32392 + 0a3bb31 commit 5b7f0c4

File tree

7 files changed

+149
-85
lines changed

7 files changed

+149
-85
lines changed

app.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ const cors = require("cors");
66

77
const app = express();
88

9-
const authRoutes = require('./routes/auth.routes');
9+
const authRoutes = require("./routes/auth.routes");
10+
const courseRoutes = require("./routes/course.routes");
1011

1112
require("dotenv").config();
1213

1314
app.use(bodyParser.urlencoded({ extended: true }));
1415
app.use(bodyParser.json());
15-
app.use(cookieParser(process.env.COOKIE_SECRET));
1616
app.use(
1717
cors({
18-
origin: ["http://localhost:3001"],
1918
credentials: true,
2019
allowedHeaders: ["Content-Type", "Authorization"],
20+
origin: ["http://localhost:3001"],
2121
})
2222
);
2323

24-
app.use('/auth', authRoutes);
24+
app.use("/auth", authRoutes);
25+
app.use("/course", courseRoutes);
2526

2627
mongoose
2728
.connect("mongodb://localhost:27017/techoptimumdasboard")

controllers/auth.controllers.js

Lines changed: 74 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,53 @@ sgMail.setApiKey(process.env.SENDGRID_API_KEY);
1010
exports.postLoginController = (req, res) => {
1111
const email = req.body.email;
1212
const password = req.body.password;
13-
const paramsExist = Object.keys(req.query).length > 0;
14-
if (!paramsExist) {
15-
User.findOne({
16-
email,
17-
})
18-
.then((users) => {
19-
checkPassword(users.password, password).then((result) => {
20-
if (result) {
21-
generateToken(email)
22-
.then((token) => {
23-
return res
24-
.cookie("token", token, {
25-
maxAge: 1000 * 60 * 60,
26-
httpOnly: true,
27-
signed: true,
28-
})
29-
.status(200)
30-
.json({
31-
success: true,
32-
username: users.username,
33-
active: users.active,
34-
});
35-
})
36-
.catch((err) => {
37-
console.log(err);
38-
return res.status(505).json({
39-
success: false,
40-
errType: "tkngenerr",
41-
msg: "Internal Server Error.",
42-
});
43-
});
44-
} else {
45-
return res.status(422).json({
46-
success: false,
47-
msg: "Invalid email or password.",
48-
errType: "lgnfail",
49-
});
50-
}
51-
});
52-
})
53-
.catch((err) => {
54-
res.status(505).json({
13+
User.findOne({
14+
email,
15+
})
16+
.then((users) => {
17+
if (!users) {
18+
return res.status(422).json({
5519
success: false,
56-
msg: "Internal Server Error.",
57-
errType: "dberr",
20+
msg: "Email not recognized.",
21+
errType: "emnnr",
5822
});
59-
console.log(err);
23+
}
24+
checkPassword(users.password, password).then((result) => {
25+
if (result) {
26+
generateToken(email)
27+
.then((token) => {
28+
return res.status(200).json({
29+
token: token,
30+
success: true,
31+
username: users.username,
32+
active: users.active,
33+
});
34+
})
35+
.catch((err) => {
36+
console.log(err);
37+
return res.status(505).json({
38+
success: false,
39+
errType: "tkngenerr",
40+
msg: "Internal Server Error.",
41+
});
42+
});
43+
} else {
44+
return res.status(422).json({
45+
success: false,
46+
msg: "Invalid email or password.",
47+
errType: "lgnfail",
48+
});
49+
}
6050
});
61-
}
51+
})
52+
.catch((err) => {
53+
res.status(505).json({
54+
success: false,
55+
msg: "Internal Server Error.",
56+
errType: "dberr",
57+
});
58+
console.log(err);
59+
});
6260
};
6361

6462
exports.postRegisterController = (req, res) => {
@@ -108,18 +106,12 @@ exports.postRegisterController = (req, res) => {
108106
.catch((err) => {
109107
console.log(err);
110108
});
111-
return res
112-
.cookie("token", token, {
113-
maxAge: 1000 * 60 * 60,
114-
signed: true,
115-
httpOnly: true,
116-
})
117-
.status(200)
118-
.json({
119-
success: true,
120-
username,
121-
active: false,
122-
});
109+
return res.status(200).json({
110+
token: token,
111+
success: true,
112+
username,
113+
active: false,
114+
});
123115
})
124116
.catch((err) => {
125117
res.status(505).json({
@@ -158,7 +150,7 @@ exports.postRegisterController = (req, res) => {
158150
};
159151

160152
exports.postLogoutController = (req, res) => {
161-
res.clearCookie("token").status(200).json({ success: true });
153+
res.status(200).json({ success: true });
162154
};
163155

164156
exports.getVerifyController = (req, res) => {
@@ -222,28 +214,31 @@ exports.postFPassReq = (req, res) => {
222214
});
223215
} else {
224216
users.token = verifyToken;
225-
users.save().then((result) => {
226-
const link = `http://localhost:3001/verify/reset-password/${verifyToken}`;
227-
const msg = {
228-
to: email,
229-
from: process.env.FROM_EMAIL,
230-
subject: "Password Reset Requested.",
231-
html: `<h1>You requested a password reset.</h1><br><a href="${link}">Click here to continue.</a><br><br><h3>Sincerely, Tech Optimum</h3>`,
232-
};
233-
sgMail
234-
.send(msg)
235-
.then((result) => {
236-
console.log("Email sent.");
237-
})
238-
.catch((err) => {
239-
console.log(err);
217+
users
218+
.save()
219+
.then((result) => {
220+
const link = `http://localhost:3001/verify/reset-password/${verifyToken}`;
221+
const msg = {
222+
to: email,
223+
from: process.env.FROM_EMAIL,
224+
subject: "Password Reset Requested.",
225+
html: `<h1>You requested a password reset.</h1><br><a href="${link}">Click here to continue.</a><br><br><h3>Sincerely, Tech Optimum</h3>`,
226+
};
227+
sgMail
228+
.send(msg)
229+
.then((result) => {
230+
console.log("Email sent.");
231+
})
232+
.catch((err) => {
233+
console.log(err);
234+
});
235+
return;
236+
})
237+
.then(() => {
238+
res.json({
239+
success: true,
240240
});
241-
return;
242-
}).then(() => {
243-
res.json({
244-
success: true,
245241
});
246-
})
247242
}
248243
});
249244
};

controllers/course.controllers.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const User = require("../models/user.model");
2+
const Course = require("../models/course.model");
3+
4+
exports.postCreateCourseController = (req, res) => {
5+
const name = req.body.courseTitle;
6+
const description = req.body.courseDescription;
7+
const courseParts = req.body.courseParts;
8+
Course.findOne({
9+
name,
10+
}).then((course) => {
11+
if (course) {
12+
return res.json({
13+
success: false,
14+
code: "coursealrex",
15+
});
16+
}
17+
const newCourse = new Course({
18+
courseName: name,
19+
description: description,
20+
user: req.user._id,
21+
notionPageIds: courseParts,
22+
});
23+
newCourse.save().then((result) => {
24+
return res.json({
25+
success: true,
26+
});
27+
});
28+
});
29+
};

models/course.model.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const mongoose = require('mongoose');
2+
3+
const Schema = mongoose.Schema;
4+
5+
const CourseSchema = new Schema({
6+
courseName: {
7+
type: String,
8+
required: true,
9+
},
10+
description: {
11+
type: String,
12+
required: true,
13+
},
14+
user: {
15+
type: Schema.Types.ObjectId,
16+
ref: 'user',
17+
required: true,
18+
},
19+
notionPageIds: {
20+
type: Object,
21+
required: true,
22+
}
23+
});
24+
25+
module.exports = mongoose.model('course', CourseSchema);

models/user.model.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const UserSchema = new Schema({
2222
token: {
2323
type: String,
2424
required: false,
25+
},
26+
courseProgress: {
27+
type: Object,
28+
required: false,
2529
}
2630
});
2731

routes/auth.routes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const express = require("express");
2-
const { body } = require("express-validator/check");
2+
const { body } = require("express-validator");
33

44
const router = express.Router();
55

@@ -21,7 +21,7 @@ router.post(
2121
.contains("[@$!%*#?&]")
2222
.withMessage("Password must contain a special character"),
2323
body("confirmPassword")
24-
.matches(req.body.password)
24+
.matches("password")
2525
.withMessage("Passwords must match"),
2626
],
2727
authControllers.postRegisterController

routes/course.routes.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const express = require("express");
2+
const authenticateToken = require("../middleware/authenticateToken");
3+
4+
const router = express.Router();
5+
6+
const courseControllers = require("../controllers/course.controllers");
7+
8+
router.post("/new-course", authenticateToken, courseControllers.postCreateCourseController);
9+
10+
module.exports = router;

0 commit comments

Comments
 (0)