Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

PORT=2000
JWT_SECRET=askfeed1234

12 changes: 3 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
const express = require("express");

const mongoose = require("mongoose");
const cors = require("cors");
const bcryptjs = require("bcryptjs");
const app = express();
require("dotenv").config();
require("./db/connectionDB");

require('./models/User')
require('./models/Survey')

const authRoutes = require("./routes/auth");
const surveyRoutes = require("./routes/survey");

const app = express();

app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cors());
app.use("/survey", surveyRoutes);

app.use("/api", authRoutes);

app.listen(process.env.PORT || 2000, () => {
Expand Down
86 changes: 49 additions & 37 deletions controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { User } = require("../models/user");
const nodemailer = require("nodemailer");
const bcryptjs = require("bcryptjs");
const jwt = require("jsonwebtoken");
JWT_SECRET = "askfeed1234";

const transporter = nodemailer.createTransport({
service: "gmail",
Expand All @@ -16,7 +17,7 @@ const signup = async (req, res) => {
const userexist = await User.findOne({ email });
if (!userexist) {
const hashedPass = await bcryptjs.hashSync(password, 10);
const token = jwt.sign({ username, email }, process.env.JWT_SECRET, {
const token = jwt.sign({ username, email }, JWT_SECRET, {
expiresIn: "30m",
});
const newUser = {
Expand All @@ -34,7 +35,7 @@ const signup = async (req, res) => {
to: email,
subject: "Account activation link",
html: `<h2>PLease click on given link to activate your account</h2>
<p>http://localhost:2000/api/activate/${token}</p>
<p>http://localhost:2000/api/activate/?token=${token}</p>
`,
};
try {
Expand All @@ -52,8 +53,8 @@ const signup = async (req, res) => {

const verifyAccount = async (req, res) => {
try {
const token = req.params.token;
const decodedUser = jwt.verify(token, process.env.JWT_SECRET);
const token = req.query.token;
const decodedUser = jwt.verify(token, JWT_SECRET);

const user = await User.findOne({ email: decodedUser.email, token: token });
if (user) {
Expand All @@ -80,7 +81,7 @@ const resetlink = async (req, res) => {
const user = await User.findOne({ email });
// console.log("user", user, email);
if (user) {
const token = jwt.sign({ email }, process.env.JWT_SECRET, {
const token = jwt.sign({ email }, JWT_SECRET, {
expiresIn: "30m",
});

Expand Down Expand Up @@ -111,41 +112,52 @@ const resetlink = async (req, res) => {
}
};

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

const changepassword = async(req,res) =>{
const { password}=req.body;
const token = req.query.token;
const decodedtoken = jwt.verify(token, process.env.JWT_SECRET);
const decodedtoken = jwt.verify(token,JWT_SECRET)
const hashedPass = await bcryptjs.hashSync(password, 10);
const user = await User.findOneAndUpdate(
{ email: decodedtoken.email, resetToken: token },
{
$set: {
const user= await User.findOneAndUpdate({email:decodedtoken.email, resetToken:token },
{$set:
{
password: hashedPass,
resetToken: null,
},
resetToken:null
}
}
);
if (user) {
res.send("Password updated successfully!");
} else {
res.send("Unable to reset password!");
}
};
const login = async (req, res) => {
const { email, password } = req.body;
const user = await User.findOne({ email });
const { _id } = user;
if (user) {
console.log("user", user);
const matchpassword = await bcryptjs.compare(password, user.password);
if (matchpassword && user.isVarified) {
const token = jwt.sign({ email, _id }, process.env.JWT_SECRET);
res.send({ token });
} else {
res.send("Login unsuccessful!");
)
if(user)
{
res.send("Password updated successfully!")
}
else
{
res.send("Unable to reset password!")
}
} else {
res.send("Incorrect Email or password!");
}
};
module.exports = { signup, verifyAccount, resetlink, changepassword, login };
const login=async(req,res)=>{
const {email,password}=req.body
const user=await User.findOne({email})
const {_id}=user
if(user)

{
console.log("user",user)
const matchpassword= await bcryptjs.compare(password,user.password)
if(matchpassword && user.isVarified)
{
const token=jwt.sign({email ,_id},JWT_SECRET)
res.send({token})

}
else
{
res.send("Login unsuccessful!")
}
}
else
{
res.send("Incorrect Email or password!")
}
}
module.exports = { signup, verifyAccount, resetlink ,changepassword,login};
8 changes: 0 additions & 8 deletions controllers/survey.js

This file was deleted.

16 changes: 6 additions & 10 deletions db/connectionDB.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
const mongoose = require("mongoose");

mongoose
.connect(
"mongodb+srv://santan:*Saheb13*@cluster0.cnnl3.mongodb.net/myFirstDatabase?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
)
.then(() => console.log("DB connected established"))
.catch((err) => console.log("DB Connection error: ", err));
mongoose.connect("mongodb+srv://santan:*Saheb13*@cluster0.cnnl3.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true,

}) .then(() => console.log("DB connected established"))
.catch(err => console.log("DB Connection error: " , err));
6 changes: 0 additions & 6 deletions middlewares/requireCredits.js

This file was deleted.

8 changes: 0 additions & 8 deletions middlewares/requireLogin.js

This file was deleted.

9 changes: 0 additions & 9 deletions models/recipient.js

This file was deleted.

61 changes: 26 additions & 35 deletions models/survey.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
const mongoose = require("mongoose");
const recipientSchema= require('./recipient')
const surveySchema = mongoose.Schema({
title: {
type: String,
},
category: {
type: String,
},
body: {
type: String,
},
subject: {
type: String,
},
recipients:[
recipientSchema
],
yes: {
type: Number,
default: 0,
},
No: {
type: Number,
default: 0,
},
_user:{
type:mongoose.Schema.Types.ObjectId,
ref:'User'
},
dateSent: Date,
lastResponded: Date
});
const Survey = mongoose.models.Survey||mongoose.model("Survey", surveySchema);
module.exports = { Survey };

const mongoose=require('mongoose')

const surveySchema=mongoose.Schema({
title:{
type:String
},
type:{
type:String
},

questionsSet:[
{
ques:{
type: String
},
ans:{
type: [Boolean]
}
}
]

})
//created the survey api now
const Survey = mongoose.model("Survey",surveySchema)
module.exports={Survey}
30 changes: 18 additions & 12 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
const mongoose = require("mongoose");
// const crypto = require ('crypto')


const userSchema = mongoose.Schema({

username: {
type: String,
required: true,
required:true,
max:64,
},
password: {
type: String,
required: true,
required:true,
},
email: {
type: String,
unique: true,
unique:true,
lowercase: true,
},
phoneNo: {
type: Number,
},
isVarified: {
isVarified:{
type: Boolean,
default: false,
default:false
},
token: {
type: String,
},
resetToken: {
type: String,
default: null,
token:{
type: String
},
resetToken:{
type: String ,
default:null
}

});
const User = mongoose.models.User || mongoose.model("User", userSchema);
const User = mongoose.model("User", userSchema);
module.exports = { User };
23 changes: 8 additions & 15 deletions routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
const express = require("express");
const express =require("express")
const router = express.Router();

const {
signup,
verifyAccount,
resetlink,
changepassword,
login,
} = require("../controllers/auth");
const {signup, verifyAccount,resetlink,changepassword,login} = require("../controllers/auth")

router.post("/signup", signup);
router.post("/activate", verifyAccount);
router.post("/resetlink", resetlink);
router.post("/changepassword", changepassword);
router.post("/login", login);

module.exports = router;
router.post('/signup', signup)
router.post('/activate', verifyAccount)
router.post('/resetlink', resetlink)
router.post('/changepassword',changepassword)
router.post('/login',login)
module.exports = router;
26 changes: 0 additions & 26 deletions routes/survey.js

This file was deleted.