Skip to content

Commit

Permalink
Init Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhv12 committed May 12, 2023
1 parent 6237c4b commit a03c1b2
Show file tree
Hide file tree
Showing 5,412 changed files with 541,809 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE =
5 changes: 5 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const config={
Useremail:'[email protected]',
UserPassword:'fdkmgjcesrzgwjmcz',
}
export default config;
49 changes: 49 additions & 0 deletions controllers/blog-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Blog from "../model/blog";

export const getAllBlogs = async (req, res, next) => {
let blogs;
try {
blogs = await Blog.find();
// .populate("user");
} catch (err) {
console.log(err);
}
if (!blogs) {
return res.status(404).json({ message: "No blog found" });
}
return res.status(200).json({ blogs });
};

export const addblog = async (req, res, next) => {
const { title, description, Image, user } = req.body;
const blog = new Blog({
title,
description,
Image,
user,
});
try {
await blog.save();
return res.status(200).json({ blog });
} catch (err) {
return res.status(500).json({ message: "error" });
}
};

export const updateblog = async (req, res, next) => {
const { title, description } = req.body;
const blogId = req.params.id;
let blog;
try {
blog = await Blog.findByIdAndUpdate(blogId, {
title,
description,
});
} catch (err) {
return console.log(err);
}
if (!blog) {
return res.status(500).json({ message: "unable to update the blog" });
}
return res.status(200).json({ blog });
};
32 changes: 32 additions & 0 deletions controllers/query-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Query from "../model/query";

export const getAllQuerys = async (req, res, next) => {
let querys;
try {
querys = await Query.find();
} catch (err) {
console.log(err);
}
if (!querys) {
return res.status(404).json({ message: "No contact found" });
}
return res.status(200).json({ querys });
};

export const addQuery = async (req, res, next) => {
const { name, email, phone, company, heading, message } = req.body;
const query = new Query({
name,
email,
phone,
company,
heading,
message,
});
try {
await query.save();
return res.status(201).json({ query });
} catch (err) {
return res.status(500).json({ message: "error" });
}
};
176 changes: 176 additions & 0 deletions controllers/user-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
import User from "../model/user";
import config from "../config/config";
import bcrypt from "bcryptjs"; //using bcryptjs for hashing password
import jwt from "jsonwebtoken";
import { SECRET_KEY } from "./utils/constants.js";
import randomstring from "randomstring";
import nodemailer from "nodemailer";


const sendresetmail = async (email, token, name) => {
try {
const transporter = nodemailer.createTransport({
service: "Gmail",
auth: {
user: config.Useremail,
pass: config.UserPassword,
},
});

const mailOptions = {
from: config.Useremail,
to: email,
subject: "to reset password",
html:
"<p>Hi " +
name +
', click on the link and <a href="http://localhost:4000/api/user/resetpassword?token=' +
token +
'" > reset your paswword </a></p>',
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log("mail has been sent", info.response);
}
});
} catch (error) {
console.log(error);
}
};

export const getAllUser = async (req, res, next) => {
//getting all user from database
try {
const users = await User.find();
return res.status(200).json({ users });
} catch (err) {
console.log(err);
return res
.status(500)
.json({ message: "Failed to get users from database." });
}
};

//SIGNUP

export const signup = async (req, res, next) => {
const { firstname, lastname, email, password } = req.body;

try {
const existingUser = await User.findOne({ email: email });
if (existingUser) {
return res.status(400).json({ message: "user already exist" });
}

const hashedPassword = bcrypt.hashSync(password, 10);
const user = new User({
firstname,
lastname,
email,
password: hashedPassword,
});
const token = jwt.sign({ email: user.email, id: user._id }, SECRET_KEY);

await user.save(); //saving new user

return res.status(201).json({ user, token }); ///////////
} catch (err) {
console.log(err);
return res
.status(500)
.json({ message: "Failed to create user. Please try again later." });
}
};

//LOGIN

export const login = async (req, res, next) => {
const { email, password } = req.body;

let existingUser;
try {
existingUser = await User.findOne({ email });
} catch (err) {
console.log(err);
return res
.status(500)
.json({ message: "Failed to login. Please try again later." });
}
if (!existingUser) {
return res.status(404).json({ message: "couldnot find user by email" });
}

const isPasswordCorrect = bcrypt.compareSync(password, existingUser.password);

var token = jwt.sign(
{ email: existingUser.email, id: existingUser._id },
SECRET_KEY,
{
expiresIn: "24h",
}
);

if (!isPasswordCorrect) {
return res.status(400).json({ message: "incorrect password" });
} else {
return res
.status(200)
.json({ message: "login", user: existingUser, token });
}
};

export const forgotpassword = async (req, res) => {
try {
const email = req.body.email;
const userdata = await User.findOne({ email: email });

if (userdata) {
const token = randomstring.generate();

const filter = { email: email };
const update = { token };

await User.findOneAndUpdate(filter, update, { new: true });

await sendresetmail(email, token, userdata.firstname);

res.status(200).send({
success: true,
msg: "check your inbox and reset your password",
});
} else {
res.status(200).send({ success: true, msg: "user doesn't exist" });
}
} catch (error) {
res.status(400).send({ success: false, msg: error.message });
}
};

export const resetpassword = async (req, res) => {
try {
const token = req.query.token;

const tokenData = await User.findOne({ token: token });
if (tokenData) {
const password = req.body.password;

const newpassword = bcrypt.hashSync(password, 10);

const user = await User.findOneAndUpdate(
{ _id: tokenData._id },
{ $set: { password: newpassword, token: "" } },
{ new: true }
);

res
.status(200)
.send({ success: true, msg: "password updated", data: user });
} else {
res.status(200).send({ success: false, msg: "this link has expired" });
}
} catch (error) {
res.status(400).send({ success: false, msg: error.message });
}
};
1 change: 1 addition & 0 deletions controllers/utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const SECRET_KEY = "code1234"
25 changes: 25 additions & 0 deletions model/blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import mongoose from "mongoose";

const Schema = mongoose.Schema;

const blogSchema = new Schema({
title: {
type: String,
required: true,
},

description: {
type: String,
required: true,
},
Image: {
type: String,

},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
}
});

export default mongoose.model("Blog", blogSchema);
33 changes: 33 additions & 0 deletions model/query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import mongoose from "mongoose";

const Schema = mongoose.Schema;

const querySchema = new Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
phone: {
type: Number,
required: true,
},
company: {
type: String,
required: true,
},
heading: {
type: String,
required: true,
},
message: {
type: String,
required: true,
},
});

export default mongoose.model("Query" , querySchema);

41 changes: 41 additions & 0 deletions model/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import mongoose from "mongoose";

const Schema = mongoose.Schema;

const userSchema = new Schema({
firstname: {
type: String,
required: true,
},
lastname: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
minlength: 4,
},

token: {
type: String,
},
});

// userSchema.methods.generateAuthToken = function () {
// const token = jwt.sign(
// { _id: this._id, name: this.name, isAdmin: this.isAdmin },
// process.env.JWTPRIVATEKEY,
// {
// expiresIn: "24h",
// }
// );
// return token;
// };

export default mongoose.model("User", userSchema); //create modal for us like collection
12 changes: 12 additions & 0 deletions node_modules/.bin/fxparser

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a03c1b2

Please sign in to comment.