Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ const cors = require("cors");

const app = express();

const authRoutes = require('./routes/auth.routes');
const authRoutes = require("./routes/auth.routes");
const courseRoutes = require("./routes/course.routes");

require("dotenv").config();

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use(
cors({
origin: ["http://localhost:3001"],
credentials: true,
allowedHeaders: ["Content-Type", "Authorization"],
origin: ["http://localhost:3001"],
})
);

app.use('/auth', authRoutes);
app.use("/auth", authRoutes);
app.use("/course", courseRoutes);

mongoose
.connect("mongodb://localhost:27017/techoptimumdasboard")
Expand Down
153 changes: 74 additions & 79 deletions controllers/auth.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,53 @@ sgMail.setApiKey(process.env.SENDGRID_API_KEY);
exports.postLoginController = (req, res) => {
const email = req.body.email;
const password = req.body.password;
const paramsExist = Object.keys(req.query).length > 0;
if (!paramsExist) {
User.findOne({
email,
})
.then((users) => {
checkPassword(users.password, password).then((result) => {
if (result) {
generateToken(email)
.then((token) => {
return res
.cookie("token", token, {
maxAge: 1000 * 60 * 60,
httpOnly: true,
signed: true,
})
.status(200)
.json({
success: true,
username: users.username,
active: users.active,
});
})
.catch((err) => {
console.log(err);
return res.status(505).json({
success: false,
errType: "tkngenerr",
msg: "Internal Server Error.",
});
});
} else {
return res.status(422).json({
success: false,
msg: "Invalid email or password.",
errType: "lgnfail",
});
}
});
})
.catch((err) => {
res.status(505).json({
User.findOne({
email,
})
.then((users) => {
if (!users) {
return res.status(422).json({
success: false,
msg: "Internal Server Error.",
errType: "dberr",
msg: "Email not recognized.",
errType: "emnnr",
});
console.log(err);
}
checkPassword(users.password, password).then((result) => {
if (result) {
generateToken(email)
.then((token) => {
return res.status(200).json({
token: token,
success: true,
username: users.username,
active: users.active,
});
})
.catch((err) => {
console.log(err);
return res.status(505).json({
success: false,
errType: "tkngenerr",
msg: "Internal Server Error.",
});
});
} else {
return res.status(422).json({
success: false,
msg: "Invalid email or password.",
errType: "lgnfail",
});
}
});
}
})
.catch((err) => {
res.status(505).json({
success: false,
msg: "Internal Server Error.",
errType: "dberr",
});
console.log(err);
});
};

exports.postRegisterController = (req, res) => {
Expand Down Expand Up @@ -108,18 +106,12 @@ exports.postRegisterController = (req, res) => {
.catch((err) => {
console.log(err);
});
return res
.cookie("token", token, {
maxAge: 1000 * 60 * 60,
signed: true,
httpOnly: true,
})
.status(200)
.json({
success: true,
username,
active: false,
});
return res.status(200).json({
token: token,
success: true,
username,
active: false,
});
})
.catch((err) => {
res.status(505).json({
Expand Down Expand Up @@ -158,7 +150,7 @@ exports.postRegisterController = (req, res) => {
};

exports.postLogoutController = (req, res) => {
res.clearCookie("token").status(200).json({ success: true });
res.status(200).json({ success: true });
};

exports.getVerifyController = (req, res) => {
Expand Down Expand Up @@ -222,28 +214,31 @@ exports.postFPassReq = (req, res) => {
});
} else {
users.token = verifyToken;
users.save().then((result) => {
const link = `http://localhost:3001/verify/reset-password/${verifyToken}`;
const msg = {
to: email,
from: process.env.FROM_EMAIL,
subject: "Password Reset Requested.",
html: `<h1>You requested a password reset.</h1><br><a href="${link}">Click here to continue.</a><br><br><h3>Sincerely, Tech Optimum</h3>`,
};
sgMail
.send(msg)
.then((result) => {
console.log("Email sent.");
})
.catch((err) => {
console.log(err);
users
.save()
.then((result) => {
const link = `http://localhost:3001/verify/reset-password/${verifyToken}`;
const msg = {
to: email,
from: process.env.FROM_EMAIL,
subject: "Password Reset Requested.",
html: `<h1>You requested a password reset.</h1><br><a href="${link}">Click here to continue.</a><br><br><h3>Sincerely, Tech Optimum</h3>`,
};
sgMail
.send(msg)
.then((result) => {
console.log("Email sent.");
})
.catch((err) => {
console.log(err);
});
return;
})
.then(() => {
res.json({
success: true,
});
return;
}).then(() => {
res.json({
success: true,
});
})
}
});
};
Expand Down
29 changes: 29 additions & 0 deletions controllers/course.controllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const User = require("../models/user.model");
const Course = require("../models/course.model");

exports.postCreateCourseController = (req, res) => {
const name = req.body.courseTitle;
const description = req.body.courseDescription;
const courseParts = req.body.courseParts;
Course.findOne({
name,
}).then((course) => {
if (course) {
return res.json({
success: false,
code: "coursealrex",
});
}
const newCourse = new Course({
courseName: name,
description: description,
user: req.user._id,
notionPageIds: courseParts,
});
newCourse.save().then((result) => {
return res.json({
success: true,
});
});
});
};
25 changes: 25 additions & 0 deletions models/course.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const CourseSchema = new Schema({
courseName: {
type: String,
required: true,
},
description: {
type: String,
required: true,
},
user: {
type: Schema.Types.ObjectId,
ref: 'user',
required: true,
},
notionPageIds: {
type: Object,
required: true,
}
});

module.exports = mongoose.model('course', CourseSchema);
4 changes: 4 additions & 0 deletions models/user.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ const UserSchema = new Schema({
token: {
type: String,
required: false,
},
courseProgress: {
type: Object,
required: false,
}
});

Expand Down
4 changes: 2 additions & 2 deletions routes/auth.routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require("express");
const { body } = require("express-validator/check");
const { body } = require("express-validator");

const router = express.Router();

Expand All @@ -21,7 +21,7 @@ router.post(
.contains("[@$!%*#?&]")
.withMessage("Password must contain a special character"),
body("confirmPassword")
.matches(req.body.password)
.matches("password")
.withMessage("Passwords must match"),
],
authControllers.postRegisterController
Expand Down
10 changes: 10 additions & 0 deletions routes/course.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require("express");
const authenticateToken = require("../middleware/authenticateToken");

const router = express.Router();

const courseControllers = require("../controllers/course.controllers");

router.post("/new-course", authenticateToken, courseControllers.postCreateCourseController);

module.exports = router;