-
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
Showing
9 changed files
with
4,663 additions
and
681 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,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; |
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,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; |
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,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; |
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,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; |
Oops, something went wrong.