-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8890af9
commit 1f2b1f8
Showing
15 changed files
with
2,311 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// This file will contain the logic and function that the api will use. | ||
// Importing required modules. | ||
const User = require("../model/userModel"); | ||
const jwt = require("jsonwebtoken"); | ||
|
||
//Enabling signup functionality. | ||
exports.signup = async function (req, res) { | ||
try { | ||
// Extracting the user data | ||
const newUser = await User.create({ | ||
name: req.body.name, | ||
email: req.body.email, | ||
password: req.body.password, | ||
passwordConfirm: req.body.passwordConfirm, | ||
}); | ||
// creating a jwt token | ||
const token = jwt.sign({ id: newUser._id }, process.env.JWT_SECRET, { | ||
expiresIn: process.env.JWT_EXPIRES_IN, | ||
}); | ||
// Sending the response back to the user | ||
res.status(201).json({ | ||
status: "success", | ||
token, | ||
}); | ||
} catch (err) { | ||
res.status(401).json({ | ||
status: "fail", | ||
message: "Signup Falied, Please check your Details.", | ||
}); | ||
} | ||
}; | ||
|
||
// Enabling login functionality | ||
exports.login = async function (req, res) { | ||
try { | ||
// Accessing the user data i.e. email and password. | ||
const email = req.body.email; | ||
const password = req.body.password; | ||
|
||
//checking if the user has entered email and passowrd or not. | ||
if (!email || !password) { | ||
throw new Error("Please enter the email and password correctly."); | ||
} | ||
// Extracting user details from database. | ||
const user = await User.findOne({ email: email }).select("+password"); | ||
// checking if the email and password of the user is correct | ||
if (!user || !(await user.correctPassword(password, user.password))) { | ||
throw new Error("Incorrect email or password"); | ||
} | ||
//If everything is ok, creating the token and sending it. | ||
const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { | ||
expiresIn: process.env.JWT_EXPIRES_IN, | ||
}); | ||
res.status(201).json({ | ||
status: "success", | ||
token, | ||
}); | ||
} catch (err) { | ||
res.status(401).json({ | ||
status: "fail", | ||
message: err.message, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
exports.getLoginPage = function (req, res) { | ||
try { | ||
res.status(200).render("login"); | ||
} catch (err) { | ||
res.status(404).json({ | ||
status: "fail", | ||
message: err.message, | ||
}); | ||
} | ||
}; | ||
|
||
exports.getSignupPage = function (req, res) { | ||
try { | ||
res.status(200).render("signup"); | ||
} catch (err) { | ||
res.status(404).json({ | ||
status: "fail", | ||
message: err.message, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// This file will contain our schema | ||
// Importing required modules. | ||
const mongoose = require("mongoose"); | ||
const validator = require("validator"); | ||
const bcrypt = require("bcryptjs"); | ||
|
||
// Creating user schema | ||
const userSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: [true, "Please Enter your name"], | ||
}, | ||
email: { | ||
type: String, | ||
required: [true, "Please mention your email-id."], | ||
unique: true, | ||
lowercase: true, | ||
validate: [validator.isEmail, "Please provide a valid email."], | ||
}, | ||
password: { | ||
type: String, | ||
minLength: 8, | ||
required: [true, "Please enter the password."], | ||
//Removing it from the output. | ||
select: false, | ||
}, | ||
passwordConfirm: { | ||
type: String, | ||
minLength: 8, | ||
required: [true, "Please re-enter your password "], | ||
validate: { | ||
validator: function (el) { | ||
return el === this.password; | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
// Creating a document middleware to encrypt our data as if someone tries to access the db he/she can't get to know about the password. | ||
userSchema.pre("save", async function (next) { | ||
// Encrypting the password with bcrypt library | ||
// Hashing the password with cost of 12 | ||
this.password = await bcrypt.hash(this.password, 12); | ||
|
||
// Deleting the passwordConfirm field, as it is only being used to check if the user has entered correct password of not while creating account. | ||
this.passwordConfirm = undefined; | ||
next(); | ||
}); | ||
|
||
// creating method on userSchema to check password during login. | ||
userSchema.methods.correctPassword = async function ( | ||
currentPassword, | ||
userPassword | ||
) { | ||
return await bcrypt.compare(currentPassword, userPassword); | ||
}; | ||
|
||
// Creating user model | ||
const User = mongoose.model("User", userSchema); | ||
module.exports = User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// This file will define where the user will be routed when they hit /api/v1/users this route. | ||
// Importing required files. | ||
const express = require("express"); | ||
const userController = require("../controller/userController"); | ||
|
||
// Creating router. | ||
const router = express.Router(); | ||
|
||
// Mounting router with specific path. | ||
router.route("/signup").post(userController.signup); | ||
router.route("/login").post(userController.login); | ||
|
||
// Exporting router | ||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// This file will contain routes for viewing page. | ||
// Importing required modules, | ||
const express = require("express"); | ||
const viewController = require("../controller/viewController"); | ||
|
||
// Creating router | ||
const router = express.Router(); | ||
|
||
// Mounting routes | ||
router.route("/login").get(viewController.getLoginPage); | ||
router.route("/signup").get(viewController.getSignupPage); | ||
|
||
// Exporting router. | ||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// This file will contain all the logic regarding express. | ||
// Importing required modules | ||
const express = require("express"); | ||
const userRouter = require("./api/routes/userRoutes"); | ||
const viewRouter = require("./api/routes/viewRoutes"); | ||
|
||
// Creating an instance of express application. | ||
const app = express(); | ||
|
||
// Setting view engine | ||
app.set("view engine", "ejs"); | ||
|
||
// Serving static files | ||
// Static files include css, javaScript, img files. We are telling our express application to access these static files from public folder. | ||
app.use(express.static(__dirname + "/public")); | ||
|
||
//Implemnting a middleware will ensure that incoming data is of json type. | ||
app.use(express.json()); | ||
|
||
// Using middlewar eto mount routes. | ||
app.use("/", viewRouter); | ||
app.use("/api/v1/users", userRouter); | ||
|
||
// Exporting app | ||
module.exports = app; |
Oops, something went wrong.