Skip to content

Commit

Permalink
Day 11 Done 🥳
Browse files Browse the repository at this point in the history
  • Loading branch information
seebham committed Sep 28, 2021
1 parent 83f199c commit bf12123
Show file tree
Hide file tree
Showing 9 changed files with 4,663 additions and 681 deletions.
39 changes: 39 additions & 0 deletions eCommerceServer/controllers/register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const User = require("../models/user");
const bcrypt = require("bcrypt");
const saltRounds = 10;
/**
*
* @param {*} req
* @param {*} res
*
* @description
* Check if the email is already registered.
* If not, create a new user.
* If yes, return an error.
*
*/

const register = async (req, res) => {
const { email, password } = req.body;

try {
const alreadyExists = await User.findOne({ where: { email } });
if (alreadyExists)
res.status(401).json({ error: "Email already registered" });
const salt = bcrypt.genSaltSync(saltRounds);
const hash = bcrypt.hashSync(password, salt);

const newUser = new User({
email: email.toLowerCase(),
password: hash,
fullName: "Dummy",
});
const savedUser = await newUser.save();
res.status(201).json(savedUser);
} catch (err) {
console.error(err);
res.status(500).json({ error: "Something went wrong" });
}
};

module.exports = register;
19 changes: 19 additions & 0 deletions eCommerceServer/database/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { Sequelize } = require("sequelize");

const sequelize = new Sequelize("postgres", "postgres", "lol", {
host: "localhost",
dialect: "postgres",
});

sequelize.sync();

(async () => {
try {
await sequelize.authenticate();
console.log("Connection has been established successfully.");
} catch (error) {
console.error("Unable to connect to the database:", error);
}
})();

module.exports = sequelize;
33 changes: 33 additions & 0 deletions eCommerceServer/middlewares/registerChecks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const { emailValidate, passwordValidate } = require("../utils/validation");

/**
*
* @param {*} req
* @param {*} res
* @param {*} next
* email validation
* password validation
* password confirmation
*/
const registerInitialChecks = (req, res, next) => {
console.log(req.body);
const { email, password, confirmPassword } = req.body;
if (!emailValidate(email)) res.status(400).send({ error: "Invalid email" });
if (!passwordValidate(password))
res.status(400).send({ error: "Invalid password" });
if (password !== confirmPassword)
res.status(400).send({ error: "Passwords do not match" });
if (
typeof email === "string" &&
email.length > 0 &&
emailValidate(email) &&
typeof password === "string" &&
password.length > 0 &&
passwordValidate(password) &&
password === confirmPassword
) {
next();
} else res.status(401).send("Registration checks failed!");
};

module.exports = registerInitialChecks;
19 changes: 19 additions & 0 deletions eCommerceServer/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { DataTypes } = require("sequelize");
const sequelize = require("../database");

const User = sequelize.define("User", {
fullName: {
type: DataTypes.STRING,
allowNull: false,
},
email: {
type: DataTypes.STRING,
allowNull: false,
},
password: {
type: DataTypes.STRING,
allowNull: false,
},
});

module.exports = User;
Loading

0 comments on commit bf12123

Please sign in to comment.