Added forgot password
This commit is contained in:
parent
30cd12d738
commit
7c66f436ae
|
@ -0,0 +1,102 @@
|
||||||
|
const express = require("express");
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
const bodyparser = require("body-parser");
|
||||||
|
const authRoute = require("./routes/auth");
|
||||||
|
const profileRoute = require("./routes/profile");
|
||||||
|
const paymentRoute = require("./routes/payment");
|
||||||
|
const courseRoute = require("./routes/course");
|
||||||
|
const adminRoute = require("./routes/admin");
|
||||||
|
const port = process.env.PORT || 5000;
|
||||||
|
|
||||||
|
const cors = require("cors");
|
||||||
|
//const passport = require('passport');
|
||||||
|
//const cookieSession = require('cookie-session') ;
|
||||||
|
//require('./passport-setup') ;
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
const MONGO_URI = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0.dqxva.mongodb.net/${process.env.MONGO_DEFAULT_DATABASE}?retryWrites=true&w=majority`;
|
||||||
|
|
||||||
|
app.use(cors());
|
||||||
|
app.use(bodyparser.json());
|
||||||
|
require("./models/Coupon");
|
||||||
|
|
||||||
|
// app.use(cookieSession({
|
||||||
|
// name: 'test-session',
|
||||||
|
// keys: ['key1', 'key2']
|
||||||
|
// }))
|
||||||
|
|
||||||
|
// const isLoggedIn = (req , res , next) => {
|
||||||
|
// if(req.user)
|
||||||
|
// {
|
||||||
|
// next()
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// res.json({
|
||||||
|
// error : "No user"
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// app.use(passport.initialize());
|
||||||
|
// app.use(passport.session());
|
||||||
|
|
||||||
|
// app.get('/failed' , (req , res) => {
|
||||||
|
// res.json({
|
||||||
|
// error : "You have failed to login"
|
||||||
|
// })
|
||||||
|
// })
|
||||||
|
|
||||||
|
// app.get('/' , (req , res) => {
|
||||||
|
// console.log('you are not logged in');
|
||||||
|
|
||||||
|
// })
|
||||||
|
|
||||||
|
// app.get('/good' ,isLoggedIn , (req , res) => {
|
||||||
|
// res.json({
|
||||||
|
// message:"success" ,
|
||||||
|
// user : req.user
|
||||||
|
// })
|
||||||
|
// })
|
||||||
|
|
||||||
|
// app.get('/auth/google',
|
||||||
|
// passport.authenticate('google', { scope: ['profile' , 'email'] }));
|
||||||
|
|
||||||
|
// app.get('/auth/google/callback',
|
||||||
|
// passport.authenticate('google', { failureRedirect: '/failed' }),
|
||||||
|
// function(req, res) {
|
||||||
|
// res.redirect('/good');
|
||||||
|
// });
|
||||||
|
|
||||||
|
// app.get('/logout', (req , res) => {
|
||||||
|
// req.session = null ;
|
||||||
|
// req.logout() ;
|
||||||
|
// res.redirect('/') ;
|
||||||
|
// })
|
||||||
|
app.use(require("./routes/Coupon"));
|
||||||
|
app.use(authRoute);
|
||||||
|
|
||||||
|
app.use(profileRoute);
|
||||||
|
|
||||||
|
app.use(paymentRoute);
|
||||||
|
|
||||||
|
app.use(courseRoute);
|
||||||
|
|
||||||
|
app.use(adminRoute);
|
||||||
|
|
||||||
|
mongoose
|
||||||
|
.connect(MONGO_URI, {
|
||||||
|
useNewUrlParser: true,
|
||||||
|
useUnifiedTopology: true,
|
||||||
|
useFindAndModify: false,
|
||||||
|
})
|
||||||
|
.then((result) => {
|
||||||
|
console.log("connected");
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log("server is running on port", port);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
});
|
|
@ -0,0 +1,102 @@
|
||||||
|
const express = require("express");
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
const bodyparser = require("body-parser");
|
||||||
|
const authRoute = require("./routes/auth");
|
||||||
|
const profileRoute = require("./routes/profile");
|
||||||
|
const paymentRoute = require("./routes/payment");
|
||||||
|
const courseRoute = require("./routes/course");
|
||||||
|
const adminRoute = require("./routes/admin");
|
||||||
|
const port = process.env.PORT || 5000;
|
||||||
|
|
||||||
|
const cors = require("cors");
|
||||||
|
//const passport = require('passport');
|
||||||
|
//const cookieSession = require('cookie-session') ;
|
||||||
|
//require('./passport-setup') ;
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
const MONGO_URI = `mongodb+srv://Cantilever:Cantilever@cluster0.dqxva.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
|
||||||
|
|
||||||
|
app.use(cors());
|
||||||
|
app.use(bodyparser.json());
|
||||||
|
require("./models/Coupon");
|
||||||
|
|
||||||
|
// app.use(cookieSession({
|
||||||
|
// name: 'test-session',
|
||||||
|
// keys: ['key1', 'key2']
|
||||||
|
// }))
|
||||||
|
|
||||||
|
// const isLoggedIn = (req , res , next) => {
|
||||||
|
// if(req.user)
|
||||||
|
// {
|
||||||
|
// next()
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// res.json({
|
||||||
|
// error : "No user"
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// app.use(passport.initialize());
|
||||||
|
// app.use(passport.session());
|
||||||
|
|
||||||
|
// app.get('/failed' , (req , res) => {
|
||||||
|
// res.json({
|
||||||
|
// error : "You have failed to login"
|
||||||
|
// })
|
||||||
|
// })
|
||||||
|
|
||||||
|
// app.get('/' , (req , res) => {
|
||||||
|
// console.log('you are not logged in');
|
||||||
|
|
||||||
|
// })
|
||||||
|
|
||||||
|
// app.get('/good' ,isLoggedIn , (req , res) => {
|
||||||
|
// res.json({
|
||||||
|
// message:"success" ,
|
||||||
|
// user : req.user
|
||||||
|
// })
|
||||||
|
// })
|
||||||
|
|
||||||
|
// app.get('/auth/google',
|
||||||
|
// passport.authenticate('google', { scope: ['profile' , 'email'] }));
|
||||||
|
|
||||||
|
// app.get('/auth/google/callback',
|
||||||
|
// passport.authenticate('google', { failureRedirect: '/failed' }),
|
||||||
|
// function(req, res) {
|
||||||
|
// res.redirect('/good');
|
||||||
|
// });
|
||||||
|
|
||||||
|
// app.get('/logout', (req , res) => {
|
||||||
|
// req.session = null ;
|
||||||
|
// req.logout() ;
|
||||||
|
// res.redirect('/') ;
|
||||||
|
// })
|
||||||
|
app.use(require("./routes/Coupon"));
|
||||||
|
app.use(authRoute);
|
||||||
|
|
||||||
|
app.use(profileRoute);
|
||||||
|
|
||||||
|
app.use(paymentRoute);
|
||||||
|
|
||||||
|
app.use(courseRoute);
|
||||||
|
|
||||||
|
app.use(adminRoute);
|
||||||
|
|
||||||
|
mongoose
|
||||||
|
.connect(MONGO_URI, {
|
||||||
|
useNewUrlParser: true,
|
||||||
|
useUnifiedTopology: true,
|
||||||
|
useFindAndModify: false,
|
||||||
|
})
|
||||||
|
.then((result) => {
|
||||||
|
console.log("connected");
|
||||||
|
app.listen(port, () => {
|
||||||
|
console.log("server is running on port", port);
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.log(err);
|
||||||
|
});
|
|
@ -0,0 +1,134 @@
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,144 @@
|
||||||
|
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 } = req.body;
|
||||||
|
const user = await user.findOne({ email });
|
||||||
|
//verify email then set password.
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({ error: "No user with that Email id" });
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
|
res.json({
|
||||||
|
message: "Protected",
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,147 @@
|
||||||
|
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 } = req.body;
|
||||||
|
const user = await user.findOne({ email });
|
||||||
|
//verify email then set password.
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({ error: "No user with that Email id" });
|
||||||
|
} else {
|
||||||
|
const resetToken = user.createPasswordResetToken();
|
||||||
|
await user.save();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
module.exports.resetpassword = async (req, res, next) => {};
|
||||||
|
|
||||||
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
|
res.json({
|
||||||
|
message: "Protected",
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,147 @@
|
||||||
|
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 } = req.body;
|
||||||
|
const user = await user.findOne({ email });
|
||||||
|
//verify email then set password.
|
||||||
|
if (!user) {
|
||||||
|
return res.status(404).json({ error: "No user with that Email id" });
|
||||||
|
} else {
|
||||||
|
// const resetToken = user.createPasswordResetToken();
|
||||||
|
// await user.save();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
module.exports.resetpassword = async (req, res, next) => {};
|
||||||
|
|
||||||
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
|
res.json({
|
||||||
|
message: "Protected",
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,148 @@
|
||||||
|
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 } = req.body;
|
||||||
|
const user = await user.findOne({ email }).then((result) => {
|
||||||
|
if (!result) {
|
||||||
|
return res.status(404).json({ error: "No user with that Email id" });
|
||||||
|
} else {
|
||||||
|
const resetToken = user.createPasswordResetToken();
|
||||||
|
await user.save();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//verify email then set password.
|
||||||
|
};
|
||||||
|
module.exports.resetpassword = async (req, res, next) => {};
|
||||||
|
|
||||||
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
|
res.json({
|
||||||
|
message: "Protected",
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,148 @@
|
||||||
|
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 } = req.body;
|
||||||
|
const user = await user.findOne({ email }).then((result) => {
|
||||||
|
if (!result) {
|
||||||
|
return res.status(404).json({ error: "No user with that Email id" });
|
||||||
|
} else {
|
||||||
|
const resetToken = user.createPasswordResetToken();
|
||||||
|
user.save();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//verify email then set password.
|
||||||
|
};
|
||||||
|
module.exports.resetpassword = async (req, res, next) => {};
|
||||||
|
|
||||||
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
|
res.json({
|
||||||
|
message: "Protected",
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,148 @@
|
||||||
|
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 } = req.body;
|
||||||
|
const user = await user.findOne({ email }).then((result) => {
|
||||||
|
if (!result) {
|
||||||
|
return res.status(404).json({ error: "No user with that Email id" });
|
||||||
|
} else {
|
||||||
|
// const resetToken = user.createPasswordResetToken();
|
||||||
|
// user.save();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//verify email then set password.
|
||||||
|
};
|
||||||
|
module.exports.resetpassword = async (req, res, next) => {};
|
||||||
|
|
||||||
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
|
res.json({
|
||||||
|
message: "Protected",
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,148 @@
|
||||||
|
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 } = req.body;
|
||||||
|
const user = await User.findOne({ email }).then((result) => {
|
||||||
|
if (!result) {
|
||||||
|
return res.status(404).json({ error: "No user with that Email id" });
|
||||||
|
} else {
|
||||||
|
// const resetToken = user.createPasswordResetToken();
|
||||||
|
// user.save();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//verify email then set password.
|
||||||
|
};
|
||||||
|
module.exports.resetpassword = async (req, res, next) => {};
|
||||||
|
|
||||||
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
|
res.json({
|
||||||
|
message: "Protected",
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,148 @@
|
||||||
|
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 } = req.body;
|
||||||
|
const user = await User.findOne({ email }).then((result) => {
|
||||||
|
if (!result) {
|
||||||
|
return res.status(404).json({ error: "No user with that Email id" });
|
||||||
|
} else {
|
||||||
|
const resetToken = user.createPasswordResetToken();
|
||||||
|
user.save();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//verify email then set password.
|
||||||
|
};
|
||||||
|
module.exports.resetpassword = async (req, res, next) => {};
|
||||||
|
|
||||||
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
|
res.json({
|
||||||
|
message: "Protected",
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,148 @@
|
||||||
|
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 } = req.body;
|
||||||
|
const user = await User.findOne({ email }).then((result) => {
|
||||||
|
if (!result) {
|
||||||
|
return res.status(404).json({ error: "No user with that Email id" });
|
||||||
|
} else {
|
||||||
|
// const resetToken = user.createPasswordResetToken();
|
||||||
|
// user.save();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
//verify email then set password.
|
||||||
|
};
|
||||||
|
module.exports.resetpassword = async (req, res, next) => {};
|
||||||
|
|
||||||
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
|
res.json({
|
||||||
|
message: "Protected",
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,154 @@
|
||||||
|
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 } = req.body;
|
||||||
|
crypto.randomBytes(32, (err, buffer) => {
|
||||||
|
if (err) {
|
||||||
|
console.log("error in crypto");
|
||||||
|
}
|
||||||
|
const token = buffer.toString("hex");
|
||||||
|
User.findOne({ email }).then((user) => {
|
||||||
|
if (!user) {
|
||||||
|
res.status(404).json({ error: "User not found with this email." });
|
||||||
|
}
|
||||||
|
user.passwordResetToken = token; //generated token;
|
||||||
|
user.passwordResetExpires = Date.now() + 10 * 6 * 1000; //valid for 10 mins.
|
||||||
|
user.save().then((res) => {});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
//verify email then set password.
|
||||||
|
};
|
||||||
|
module.exports.resetpassword = async (req, res, next) => {};
|
||||||
|
|
||||||
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
|
res.json({
|
||||||
|
message: "Protected",
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,154 @@
|
||||||
|
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 } = req.body;
|
||||||
|
crypto.randomBytes(32, (err, buffer) => {
|
||||||
|
if (err) {
|
||||||
|
console.log("error in crypto");
|
||||||
|
}
|
||||||
|
const token = buffer.toString("hex");
|
||||||
|
User.findOne({ email }).then((user) => {
|
||||||
|
if (!user) {
|
||||||
|
res.status(404).json({ error: "User not found with this email." });
|
||||||
|
}
|
||||||
|
user.passwordResetToken = token; //generated token;
|
||||||
|
user.passwordResetExpires = Date.now() + 10 * 6 * 1000; //valid for 10 mins.
|
||||||
|
user.save().then((res) => {}); //need to do the task.
|
||||||
|
});
|
||||||
|
});
|
||||||
|
//verify email then set password.
|
||||||
|
};
|
||||||
|
module.exports.resetpassword = async (req, res, next) => {};
|
||||||
|
|
||||||
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
|
console.log(req.user);
|
||||||
|
res.json({
|
||||||
|
message: "Protected",
|
||||||
|
user: req.user,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,179 @@
|
||||||
|
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;
|
||||||
|
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 reset_link = `${link}/${result._id}/${token}`;
|
||||||
|
res.status(200).json({ reset_link });
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.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;
|
||||||
|
User.findById({ _id }).then((result) => {
|
||||||
|
if (result) {
|
||||||
|
const secret = JWT_secret + result.password;
|
||||||
|
const payload = jwt.verify(token, secret);
|
||||||
|
if (payload) {
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,179 @@
|
||||||
|
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;
|
||||||
|
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 reset_link = `${link}/${result._id}/${token}`;
|
||||||
|
res.status(200).json({ reset_link });
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.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 payload = jwt.verify(token, secret);
|
||||||
|
if (payload) {
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,179 @@
|
||||||
|
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 reset_link = `${link}/${result._id}/${token}`;
|
||||||
|
res.status(200).json({ reset_link });
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.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 payload = jwt.verify(token, secret);
|
||||||
|
if (payload) {
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,181 @@
|
||||||
|
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 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 payload = jwt.verify(token, secret);
|
||||||
|
if (payload) {
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,186 @@
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,186 @@
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,188 @@
|
||||||
|
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,
|
||||||
|
});
|
||||||
|
};
|
|
@ -0,0 +1,37 @@
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
|
||||||
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
|
const userSchema = new Schema({
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
otp: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
//need to add isAdmin
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = mongoose.model("User", userSchema);
|
|
@ -0,0 +1,44 @@
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
const crypto = require("crypto");
|
||||||
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
|
const userSchema = new Schema({
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
otp: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
passwordResetToken: String,
|
||||||
|
passwordResetExpires: Date,
|
||||||
|
//need to add isAdmin
|
||||||
|
});
|
||||||
|
|
||||||
|
userSchema.methods.createPasswordResetToken = function () {
|
||||||
|
const resetToken = crypto.randomBytes(32).toString("hex");
|
||||||
|
crypto.createHash("sha256").update(resetToken).digest("hex");
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = mongoose.model("User", userSchema);
|
|
@ -0,0 +1,51 @@
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
const crypto = require("crypto");
|
||||||
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
|
const userSchema = new Schema({
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
otp: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
passwordResetToken: String,
|
||||||
|
passwordResetExpires: Date,
|
||||||
|
//need to add isAdmin
|
||||||
|
});
|
||||||
|
|
||||||
|
userSchema.methods.createPasswordResetToken = function () {
|
||||||
|
const resetToken = crypto.randomBytes(32).toString("hex");
|
||||||
|
this.passwordResetToken = crypto
|
||||||
|
.createHash("sha256")
|
||||||
|
.update(resetToken)
|
||||||
|
.digest("hex");
|
||||||
|
this.passwordResetExpires = Date.now() + 10 * 60 * 1000;
|
||||||
|
console.log("resetToken:" + resetToken);
|
||||||
|
|
||||||
|
return resetToken;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = mongoose.model("User", userSchema);
|
|
@ -0,0 +1,39 @@
|
||||||
|
const mongoose = require("mongoose");
|
||||||
|
const crypto = require("crypto");
|
||||||
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
|
const userSchema = new Schema({
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
otp: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
passwordResetToken: String,
|
||||||
|
passwordResetExpires: Date,
|
||||||
|
//need to add isAdmin
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = mongoose.model("User", userSchema);
|
|
@ -0,0 +1,16 @@
|
||||||
|
const express = require("express");
|
||||||
|
const authController = require("../controllers/auth");
|
||||||
|
const isAuth = require("../middleware/requirelogin");
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.get("/protected", isAuth, authController.checkProtected);
|
||||||
|
|
||||||
|
router.post("/signup", authController.postSignup);
|
||||||
|
|
||||||
|
router.post("/signin", authController.postSignin);
|
||||||
|
|
||||||
|
router.post("/sendotp", authController.sendOTP);
|
||||||
|
|
||||||
|
router.post("/getotp", authController.getOTP);
|
||||||
|
|
||||||
|
module.exports = router;
|
|
@ -0,0 +1,20 @@
|
||||||
|
const express = require("express");
|
||||||
|
const authController = require("../controllers/auth");
|
||||||
|
const isAuth = require("../middleware/requirelogin");
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.get("/protected", isAuth, authController.checkProtected);
|
||||||
|
|
||||||
|
router.post("/signup", authController.postSignup);
|
||||||
|
|
||||||
|
router.post("/signin", authController.postSignin);
|
||||||
|
|
||||||
|
router.post("/sendotp", authController.sendOTP);
|
||||||
|
|
||||||
|
router.post("/getotp", authController.getOTP);
|
||||||
|
|
||||||
|
router.post("/forgotpassword", authController.forgotpassword);
|
||||||
|
|
||||||
|
router.post("/resetpassword", authController.resetpassword);
|
||||||
|
|
||||||
|
module.exports = router;
|
|
@ -0,0 +1,20 @@
|
||||||
|
const express = require("express");
|
||||||
|
const authController = require("../controllers/auth");
|
||||||
|
const isAuth = require("../middleware/requirelogin");
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.get("/protected", isAuth, authController.checkProtected);
|
||||||
|
|
||||||
|
router.post("/signup", authController.postSignup);
|
||||||
|
|
||||||
|
router.post("/signin", authController.postSignin);
|
||||||
|
|
||||||
|
router.post("/sendotp", authController.sendOTP);
|
||||||
|
|
||||||
|
router.post("/getotp", authController.getOTP);
|
||||||
|
|
||||||
|
router.post("/forgotpassword", authController.forgotpassword);
|
||||||
|
|
||||||
|
router.post("/resetpassword/:id/:token", authController.resetpassword);
|
||||||
|
|
||||||
|
module.exports = router;
|
|
@ -0,0 +1,20 @@
|
||||||
|
const express = require("express");
|
||||||
|
const authController = require("../controllers/auth");
|
||||||
|
const isAuth = require("../middleware/requirelogin");
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.get("/protected", isAuth, authController.checkProtected);
|
||||||
|
|
||||||
|
router.post("/signup", authController.postSignup);
|
||||||
|
|
||||||
|
router.post("/signin", authController.postSignin);
|
||||||
|
|
||||||
|
router.post("/sendotp", authController.sendOTP);
|
||||||
|
|
||||||
|
router.post("/getotp", authController.getOTP);
|
||||||
|
|
||||||
|
router.post("/forgotpassword", authController.forgotpassword);
|
||||||
|
|
||||||
|
router.post("/resetpassword/:_id/:token", authController.resetpassword);
|
||||||
|
|
||||||
|
module.exports = router;
|
|
@ -0,0 +1 @@
|
||||||
|
const nodemailer = require("nodemailer");
|
2
app.js
2
app.js
|
@ -15,7 +15,7 @@ const cors = require("cors");
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
const MONGO_URI = `mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PASSWORD}@cluster0.dqxva.mongodb.net/${process.env.MONGO_DEFAULT_DATABASE}?retryWrites=true&w=majority`;
|
const MONGO_URI = `mongodb+srv://Cantilever:Cantilever@cluster0.dqxva.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`;
|
||||||
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use(bodyparser.json());
|
app.use(bodyparser.json());
|
||||||
|
|
|
@ -125,6 +125,60 @@ module.exports.getOTP = (req, res, next) => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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) => {
|
module.exports.checkProtected = (req, res, next) => {
|
||||||
console.log(req.user);
|
console.log(req.user);
|
||||||
res.json({
|
res.json({
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
|
const crypto = require("crypto");
|
||||||
const Schema = mongoose.Schema;
|
const Schema = mongoose.Schema;
|
||||||
|
|
||||||
const userSchema = new Schema({
|
const userSchema = new Schema({
|
||||||
|
@ -31,6 +31,8 @@ const userSchema = new Schema({
|
||||||
otp: {
|
otp: {
|
||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
|
passwordResetToken: String,
|
||||||
|
passwordResetExpires: Date,
|
||||||
//need to add isAdmin
|
//need to add isAdmin
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"messagebird": "^3.6.1",
|
"messagebird": "^3.6.1",
|
||||||
"mongoose": "^5.12.2",
|
"mongoose": "^5.12.2",
|
||||||
|
"nodemailer": "^6.6.0",
|
||||||
"passport": "^0.4.1",
|
"passport": "^0.4.1",
|
||||||
"passport-google-oauth": "^2.0.0",
|
"passport-google-oauth": "^2.0.0",
|
||||||
"razorpay": "^2.0.6",
|
"razorpay": "^2.0.6",
|
||||||
|
@ -1654,6 +1655,14 @@
|
||||||
"node": ">= 0.6"
|
"node": ">= 0.6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/nodemailer": {
|
||||||
|
"version": "6.6.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.6.0.tgz",
|
||||||
|
"integrity": "sha512-ikSMDU1nZqpo2WUPE0wTTw/NGGImTkwpJKDIFPZT+YvvR9Sj+ze5wzu95JHkBMglQLoG2ITxU21WukCC/XsFkg==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/nodemon": {
|
"node_modules/nodemon": {
|
||||||
"version": "2.0.7",
|
"version": "2.0.7",
|
||||||
"resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.7.tgz",
|
"resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.7.tgz",
|
||||||
|
@ -4051,6 +4060,11 @@
|
||||||
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
|
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
|
||||||
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
|
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
|
||||||
},
|
},
|
||||||
|
"nodemailer": {
|
||||||
|
"version": "6.6.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/nodemailer/-/nodemailer-6.6.0.tgz",
|
||||||
|
"integrity": "sha512-ikSMDU1nZqpo2WUPE0wTTw/NGGImTkwpJKDIFPZT+YvvR9Sj+ze5wzu95JHkBMglQLoG2ITxU21WukCC/XsFkg=="
|
||||||
|
},
|
||||||
"nodemon": {
|
"nodemon": {
|
||||||
"version": "2.0.7",
|
"version": "2.0.7",
|
||||||
"resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.7.tgz",
|
"resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.7.tgz",
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
"jsonwebtoken": "^8.5.1",
|
"jsonwebtoken": "^8.5.1",
|
||||||
"messagebird": "^3.6.1",
|
"messagebird": "^3.6.1",
|
||||||
"mongoose": "^5.12.2",
|
"mongoose": "^5.12.2",
|
||||||
|
"nodemailer": "^6.6.0",
|
||||||
"passport": "^0.4.1",
|
"passport": "^0.4.1",
|
||||||
"passport-google-oauth": "^2.0.0",
|
"passport-google-oauth": "^2.0.0",
|
||||||
"razorpay": "^2.0.6",
|
"razorpay": "^2.0.6",
|
||||||
|
|
|
@ -13,4 +13,8 @@ router.post("/sendotp", authController.sendOTP);
|
||||||
|
|
||||||
router.post("/getotp", authController.getOTP);
|
router.post("/getotp", authController.getOTP);
|
||||||
|
|
||||||
|
router.post("/forgotpassword", authController.forgotpassword);
|
||||||
|
|
||||||
|
router.post("/resetpassword/:_id/:token", authController.resetpassword);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
Loading…
Reference in New Issue