From 1a53c17cb1976afc5e965f5526e4c5a66ffa7ceb Mon Sep 17 00:00:00 2001 From: yashrajverma Date: Tue, 11 May 2021 19:56:47 +0530 Subject: [PATCH] Added forgot pass --- .history/controllers/auth_20210511194950.js | 134 -------------- .history/controllers/auth_20210511195210.js | 188 -------------------- .history/controllers/auth_20210511195315.js | 134 -------------- .history/controllers/auth_20210511195522.js | 187 ------------------- models/User.js | 36 ---- 5 files changed, 679 deletions(-) delete mode 100644 .history/controllers/auth_20210511194950.js delete mode 100644 .history/controllers/auth_20210511195210.js delete mode 100644 .history/controllers/auth_20210511195315.js delete mode 100644 .history/controllers/auth_20210511195522.js diff --git a/.history/controllers/auth_20210511194950.js b/.history/controllers/auth_20210511194950.js deleted file mode 100644 index d9b71a4..0000000 --- a/.history/controllers/auth_20210511194950.js +++ /dev/null @@ -1,134 +0,0 @@ -const bcrypt = require("bcryptjs"); -const User = require("../models/User"); -const Student = require("../models/Student"); -const jwt = require("jsonwebtoken"); -const JWT_secret = "Cantileverlabs"; -const messagebird = require("messagebird")("llVKD53ve6QRpbCKOHzWBADaS"); - -module.exports.Protected = async (req, res, next) => { - res.send("Hello User"); -}; -module.exports.postSignup = async (req, res, next) => { - try { - //we need firstName , lastName , email , password as input - let firstName = req.body.firstName || " "; - let lastName = req.body.lastName || " "; - let email = req.body.email; - let password = req.body.password; - let user = await User.findOne({ email: email }); - if (user) { - res.json({ - message: "User already exist", - type: "error", - }); - } else { - const hashedPass = await bcrypt.hash(password, 12); - user = new User({ - firstName: firstName, - lastName: lastName, - email: email, - password: hashedPass, - isAdmin: false, - }); - user = await user.save(); - await Student.deleteOne({ user: user._id }); - let student = new Student({ - user: user._id, - }); - student = await student.save(); - user.student = student._id; - await user.save(); - res.json({ - message: "Successfully signed Up", - type: "success", - }); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.postSignin = async (req, res, next) => { - try { - //we need email and password as input - let email = req.body.email; - let password = req.body.password; - let user = await User.findOne({ email: email }); - if (user) { - const isMatched = await bcrypt.compare(password, user.password); - if (isMatched) { - const token = jwt.sign({ _id: user._id }, JWT_secret); - res.json({ - token: token, - }); - } else { - res.json({ - message: "email and password doesn't match", - type: "error", - }); - } - } else { - res.json({ - message: "No user with this email exists", - type: "error", - }); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.sendOTP = (req, res, next) => { - //uNNYosMopvvCW9RTR1tRWJmYC test - //llVKD53ve6QRpbCKOHzWBADaS live - try { - const { phoneNumber } = req.body; - if (!phoneNumber) { - res.status(422).json({ message: "Please Add All Required Fields" }); - return; - } else { - messagebird.verify.create( - phoneNumber, - { - template: "Your verification code is %token", - }, - function (err, response) { - if (err) { - console.log(err); - res.status(422).json({ message: err.errors[0].description }); - } else { - console.log(response); - res.json({ id: response.id }); - } - } - ); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.getOTP = (req, res, next) => { - try { - const { id, token } = req.body; - messagebird.verify.verify(id, token, function (err, response) { - if (err) { - console.log({ error: err.errors[0].description, id: id }); - res.json({ error: err.errors[0].description, id: id }); - } else { - console.log(response); - res.json({ message: "Code Verified" }); - } - }); - } catch (err) { - console.log(err); - } -}; - -module.exports.checkProtected = (req, res, next) => { - console.log(req.user); - res.json({ - message: "Protected", - user: req.user, - }); -}; diff --git a/.history/controllers/auth_20210511195210.js b/.history/controllers/auth_20210511195210.js deleted file mode 100644 index edae889..0000000 --- a/.history/controllers/auth_20210511195210.js +++ /dev/null @@ -1,188 +0,0 @@ -const bcrypt = require("bcryptjs"); -const User = require("../models/User"); -const Student = require("../models/Student"); -const jwt = require("jsonwebtoken"); -const JWT_secret = "Cantileverlabs"; -const messagebird = require("messagebird")("llVKD53ve6QRpbCKOHzWBADaS"); - -module.exports.Protected = async (req, res, next) => { - res.send("Hello User"); -}; -module.exports.postSignup = async (req, res, next) => { - try { - //we need firstName , lastName , email , password as input - let firstName = req.body.firstName || " "; - let lastName = req.body.lastName || " "; - let email = req.body.email; - let password = req.body.password; - let user = await User.findOne({ email: email }); - if (user) { - res.json({ - message: "User already exist", - type: "error", - }); - } else { - const hashedPass = await bcrypt.hash(password, 12); - user = new User({ - firstName: firstName, - lastName: lastName, - email: email, - password: hashedPass, - isAdmin: false, - }); - user = await user.save(); - await Student.deleteOne({ user: user._id }); - let student = new Student({ - user: user._id, - }); - student = await student.save(); - user.student = student._id; - await user.save(); - res.json({ - message: "Successfully signed Up", - type: "success", - }); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.postSignin = async (req, res, next) => { - try { - //we need email and password as input - let email = req.body.email; - let password = req.body.password; - let user = await User.findOne({ email: email }); - if (user) { - const isMatched = await bcrypt.compare(password, user.password); - if (isMatched) { - const token = jwt.sign({ _id: user._id }, JWT_secret); - res.json({ - token: token, - }); - } else { - res.json({ - message: "email and password doesn't match", - type: "error", - }); - } - } else { - res.json({ - message: "No user with this email exists", - type: "error", - }); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.sendOTP = (req, res, next) => { - //uNNYosMopvvCW9RTR1tRWJmYC test - //llVKD53ve6QRpbCKOHzWBADaS live - try { - const { phoneNumber } = req.body; - if (!phoneNumber) { - res.status(422).json({ message: "Please Add All Required Fields" }); - return; - } else { - messagebird.verify.create( - phoneNumber, - { - template: "Your verification code is %token", - }, - function (err, response) { - if (err) { - console.log(err); - res.status(422).json({ message: err.errors[0].description }); - } else { - console.log(response); - res.json({ id: response.id }); - } - } - ); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.getOTP = (req, res, next) => { - try { - const { id, token } = req.body; - messagebird.verify.verify(id, token, function (err, response) { - if (err) { - console.log({ error: err.errors[0].description, id: id }); - res.json({ error: err.errors[0].description, id: id }); - } else { - console.log(response); - res.json({ message: "Code Verified" }); - } - }); - } catch (err) { - console.log(err); - } -}; - -module.exports.forgotpassword = async (req, res, next) => { - const { email, link } = req.body; //link = https://cantileverlabs.herokuapp.com/resetpassword/:id/:token - User.findOne({ email }) - .then((result) => { - if (!result) { - res.status(404).json({ error: "User not found with this Email" }); - return; - } else { - const payload = { - email: result.email, - _id: result._id, - }; - const secret = JWT_secret + result.password; - const token = jwt.sign(payload, secret, { expiresIn: "10m" }); - const user_token = new User({ - passwordResetToken: token, - }); - user_token.save(); - const reset_link = `${link}/${result._id}/${token}`; - res - .status(200) - .json({ reset_link, message: "link is active for 10 mins" }); - } - }) - .catch((err) => { - console.log(err); - }); - //verify email then set password. -}; -module.exports.resetpassword = async (req, res, next) => { - const { _id, token } = req.params; - const { password } = req.body; - await User.findById({ _id }).then((result) => { - if (result) { - const secret = JWT_secret + result.password; - const user_token = User.findOne({ - passwordResetToken: result.passwordResetToken, - }); - const payload = jwt.verify(token, secret); - if (token == user_token) { - User.findByIdAndUpdate(_id, { $set: { password } }) - .then((data) => { - res.json({ message: "Password Updated!" }); - }) - .catch((err) => { - console.log(err); - }); - } else { - res.status(422).json({ error: "some error occured" }); - } - } - }); -}; - -module.exports.checkProtected = (req, res, next) => { - console.log(req.user); - res.json({ - message: "Protected", - user: req.user, - }); -}; diff --git a/.history/controllers/auth_20210511195315.js b/.history/controllers/auth_20210511195315.js deleted file mode 100644 index d9b71a4..0000000 --- a/.history/controllers/auth_20210511195315.js +++ /dev/null @@ -1,134 +0,0 @@ -const bcrypt = require("bcryptjs"); -const User = require("../models/User"); -const Student = require("../models/Student"); -const jwt = require("jsonwebtoken"); -const JWT_secret = "Cantileverlabs"; -const messagebird = require("messagebird")("llVKD53ve6QRpbCKOHzWBADaS"); - -module.exports.Protected = async (req, res, next) => { - res.send("Hello User"); -}; -module.exports.postSignup = async (req, res, next) => { - try { - //we need firstName , lastName , email , password as input - let firstName = req.body.firstName || " "; - let lastName = req.body.lastName || " "; - let email = req.body.email; - let password = req.body.password; - let user = await User.findOne({ email: email }); - if (user) { - res.json({ - message: "User already exist", - type: "error", - }); - } else { - const hashedPass = await bcrypt.hash(password, 12); - user = new User({ - firstName: firstName, - lastName: lastName, - email: email, - password: hashedPass, - isAdmin: false, - }); - user = await user.save(); - await Student.deleteOne({ user: user._id }); - let student = new Student({ - user: user._id, - }); - student = await student.save(); - user.student = student._id; - await user.save(); - res.json({ - message: "Successfully signed Up", - type: "success", - }); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.postSignin = async (req, res, next) => { - try { - //we need email and password as input - let email = req.body.email; - let password = req.body.password; - let user = await User.findOne({ email: email }); - if (user) { - const isMatched = await bcrypt.compare(password, user.password); - if (isMatched) { - const token = jwt.sign({ _id: user._id }, JWT_secret); - res.json({ - token: token, - }); - } else { - res.json({ - message: "email and password doesn't match", - type: "error", - }); - } - } else { - res.json({ - message: "No user with this email exists", - type: "error", - }); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.sendOTP = (req, res, next) => { - //uNNYosMopvvCW9RTR1tRWJmYC test - //llVKD53ve6QRpbCKOHzWBADaS live - try { - const { phoneNumber } = req.body; - if (!phoneNumber) { - res.status(422).json({ message: "Please Add All Required Fields" }); - return; - } else { - messagebird.verify.create( - phoneNumber, - { - template: "Your verification code is %token", - }, - function (err, response) { - if (err) { - console.log(err); - res.status(422).json({ message: err.errors[0].description }); - } else { - console.log(response); - res.json({ id: response.id }); - } - } - ); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.getOTP = (req, res, next) => { - try { - const { id, token } = req.body; - messagebird.verify.verify(id, token, function (err, response) { - if (err) { - console.log({ error: err.errors[0].description, id: id }); - res.json({ error: err.errors[0].description, id: id }); - } else { - console.log(response); - res.json({ message: "Code Verified" }); - } - }); - } catch (err) { - console.log(err); - } -}; - -module.exports.checkProtected = (req, res, next) => { - console.log(req.user); - res.json({ - message: "Protected", - user: req.user, - }); -}; diff --git a/.history/controllers/auth_20210511195522.js b/.history/controllers/auth_20210511195522.js deleted file mode 100644 index 70fab0a..0000000 --- a/.history/controllers/auth_20210511195522.js +++ /dev/null @@ -1,187 +0,0 @@ -const bcrypt = require("bcryptjs"); -const User = require("../models/User"); -const Student = require("../models/Student"); -const jwt = require("jsonwebtoken"); -const JWT_secret = "Cantileverlabs"; -const messagebird = require("messagebird")("llVKD53ve6QRpbCKOHzWBADaS"); - -module.exports.Protected = async (req, res, next) => { - res.send("Hello User"); -}; -module.exports.postSignup = async (req, res, next) => { - try { - //we need firstName , lastName , email , password as input - let firstName = req.body.firstName || " "; - let lastName = req.body.lastName || " "; - let email = req.body.email; - let password = req.body.password; - let user = await User.findOne({ email: email }); - if (user) { - res.json({ - message: "User already exist", - type: "error", - }); - } else { - const hashedPass = await bcrypt.hash(password, 12); - user = new User({ - firstName: firstName, - lastName: lastName, - email: email, - password: hashedPass, - isAdmin: false, - }); - user = await user.save(); - await Student.deleteOne({ user: user._id }); - let student = new Student({ - user: user._id, - }); - student = await student.save(); - user.student = student._id; - await user.save(); - res.json({ - message: "Successfully signed Up", - type: "success", - }); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.postSignin = async (req, res, next) => { - try { - //we need email and password as input - let email = req.body.email; - let password = req.body.password; - let user = await User.findOne({ email: email }); - if (user) { - const isMatched = await bcrypt.compare(password, user.password); - if (isMatched) { - const token = jwt.sign({ _id: user._id }, JWT_secret); - res.json({ - token: token, - }); - } else { - res.json({ - message: "email and password doesn't match", - type: "error", - }); - } - } else { - res.json({ - message: "No user with this email exists", - type: "error", - }); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.sendOTP = (req, res, next) => { - //uNNYosMopvvCW9RTR1tRWJmYC test - //llVKD53ve6QRpbCKOHzWBADaS live - try { - const { phoneNumber } = req.body; - if (!phoneNumber) { - res.status(422).json({ message: "Please Add All Required Fields" }); - return; - } else { - messagebird.verify.create( - phoneNumber, - { - template: "Your verification code is %token", - }, - function (err, response) { - if (err) { - console.log(err); - res.status(422).json({ message: err.errors[0].description }); - } else { - console.log(response); - res.json({ id: response.id }); - } - } - ); - } - } catch (err) { - console.log(err); - } -}; - -module.exports.getOTP = (req, res, next) => { - try { - const { id, token } = req.body; - messagebird.verify.verify(id, token, function (err, response) { - if (err) { - console.log({ error: err.errors[0].description, id: id }); - res.json({ error: err.errors[0].description, id: id }); - } else { - console.log(response); - res.json({ message: "Code Verified" }); - } - }); - } catch (err) { - console.log(err); - } -}; -module.exports.forgotpassword = async (req, res, next) => { - const { email, link } = req.body; //link = https://cantileverlabs.herokuapp.com/resetpassword/:id/:token - User.findOne({ email }) - .then((result) => { - if (!result) { - res.status(404).json({ error: "User not found with this Email" }); - return; - } else { - const payload = { - email: result.email, - _id: result._id, - }; - const secret = JWT_secret + result.password; - const token = jwt.sign(payload, secret, { expiresIn: "10m" }); - const user_token = new User({ - passwordResetToken: token, - }); - user_token.save(); - const reset_link = `${link}/${result._id}/${token}`; - res - .status(200) - .json({ reset_link, message: "link is active for 10 mins" }); - } - }) - .catch((err) => { - console.log(err); - }); - //verify email then set password. -}; -module.exports.resetpassword = async (req, res, next) => { - const { _id, token } = req.params; - const { password } = req.body; - await User.findById({ _id }).then((result) => { - if (result) { - const secret = JWT_secret + result.password; - const user_token = User.findOne({ - passwordResetToken: result.passwordResetToken, - }); - const payload = jwt.verify(token, secret); - if (token == user_token) { - User.findByIdAndUpdate(_id, { $set: { password } }) - .then((data) => { - res.json({ message: "Password Updated!" }); - }) - .catch((err) => { - console.log(err); - }); - } else { - res.status(422).json({ error: "some error occured" }); - } - } - }); -}; - -module.exports.checkProtected = (req, res, next) => { - console.log(req.user); - res.json({ - message: "Protected", - user: req.user, - }); -}; diff --git a/models/User.js b/models/User.js index db9528a..a9efe6e 100644 --- a/models/User.js +++ b/models/User.js @@ -3,7 +3,6 @@ const crypto = require("crypto"); const Schema = mongoose.Schema; const userSchema = new Schema({ - firstName: { type: String, required: true, @@ -37,39 +36,4 @@ const userSchema = new Schema({ //need to add isAdmin }); - firstName : { - type :String , - required : true - } , - lastName : { - type:String , - required: true - } , - email : { - type:String , - required: true - } , - password : { - type : String - } , - googleId : { - type : String - } , - student : { - type : mongoose.Types.ObjectId , - ref: 'Student' - } , - isAdmin : { - type : Boolean - } , - numLoggedIn : { - type : Number - } , - clicked : { - type : Object - } - //need to add isAdmin -}) ; - - module.exports = mongoose.model("User", userSchema);