42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
const userCreds = require("./userData.json");
|
|
const bcrypt = require("bcrypt");
|
|
const jwt = require("jsonwebtoken");
|
|
const fs = require("fs");
|
|
const SECRETKEY = "IUGHIU*&*&^&)*FF&R&RE%R%RRP(*Y(*%C&$^%*)";
|
|
|
|
exports.signup = async (req, res) => {
|
|
let { username, email, password } = req.body;
|
|
|
|
try {
|
|
const existingUser = userCreds.find((user) => user.email === email);
|
|
if (existingUser) {
|
|
return res.status(400).json({ message: "User already exists, Please try again..." });
|
|
}
|
|
const hashpassword = await bcrypt.hash(password, 10);
|
|
const result = {
|
|
username: username,
|
|
id: userCreds.length + 1,
|
|
password: hashpassword,
|
|
email: email,
|
|
};
|
|
userCreds.push(result);
|
|
|
|
fs.writeFile("./userData.json", JSON.stringify(userCreds), (err) => {
|
|
if (err) {
|
|
return res.status(500).send("Cannot add details to the file");
|
|
}
|
|
return res.json({ status: "Data Added to the file" });
|
|
});
|
|
const signUpToken = jwt.sign(
|
|
{ email: result.email, id: result.id, usename: result.username },
|
|
SECRETKEY
|
|
);
|
|
console.log(signUpToken);
|
|
|
|
res.status(200).json({ user: result, token: signUpToken });
|
|
} catch (error) {
|
|
console.log(error);
|
|
res.status(500).json({ msg: "Something went wrong" });
|
|
}
|
|
};
|