Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
emaduddin678 committed Nov 7, 2023
0 parents commit 570e5f1
Show file tree
Hide file tree
Showing 15 changed files with 2,798 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
.env
/day-1-picture
day-1.json
index.html
db.js

.vercel
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Task-Management-Server
# Task-management-server
18 changes: 18 additions & 0 deletions connectionDb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import mongoose from "mongoose";

const connect = async () => {
try {
console.log(process.env.MONGODB_TOKEN);
await mongoose.connect(process.env.MONGODB_TOKEN);
// console.log(process.env.MONGODB_TOKEN);
console.log("MongoDB connected!");
} catch (error) {
throw error;
}
};
const ifDisconnect = async () => {
mongoose.connection.on("disconnected", () => {
console.log("mongoDB disconnected!");
});
};
export { connect, ifDisconnect };
49 changes: 49 additions & 0 deletions controllers/TaskController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Task from "../models/TaskModel.js";

export const addTask = async (req, res, next) => {
try {
const task = new Task(req.body);
const savedTask = await task.save();
res.status(200).json(savedTask);
} catch (error) {
next(error);
}
};

export const getAllTask = async (req, res, next) => {
try {
const tasks = await Task.find();
res.status(200).json(tasks);
} catch (error) {
next(error);
}
};

export const getSingleTask = async (req, res, next) => {
try {
const task = await Task.findById(req.params.id);
res.status(200).json(task);
} catch (error) {
next(error);
}
};

export const updateTask = async (req, res, next) => {
try {
const updatedTask = await Task.findByIdAndUpdate(req.params.id, req.body, {
new: true,
});
res.status(200).json(updatedTask);
} catch (error) {
next(error);
}
};

export const deleteTask = async (req, res, next) => {
try {
const deletedTask = await Task.findByIdAndDelete(req.params.id);
res.status(200).json(deletedTask);
} catch (error) {
next(error);
}
};
80 changes: 80 additions & 0 deletions controllers/authController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import User from "../models/UserModel.js";
import { createError } from "../utils/error.js";
import jwt from "jsonwebtoken";
// import bcrypt from
import bcrypt from "bcrypt";

export const register = async (req, res, next) => {
try {
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync(req.body.password, salt);

const newUser = new User({
username: req.body.username,
email: req.body.email,
password: hash,
});

await newUser.save();

const token = jwt.sign(
{ username: newUser.username, id: newUser._id },
process.env.JWT_SECRET,
{
expiresIn: "1h",
}
);

res
.cookie("access_token", token, {
httpOnly: true,
secure: true,
sameSite: "none",
})
.status(200)
.send({
user: newUser,
message: "User has been created.",
token: token,
success: true,
});
} catch (error) {
next(error);
}
};

export const login = async (req, res, next) => {
try {
const user = await User.findOne({ email: req.body.email });
if (!user) return next(createError(404, "User not found"));

const isPasswordCorrect = await bcrypt.compare(
req.body.password,
user.password
);

if (!isPasswordCorrect)
return next(createError(400, "Wrong password for username"));

const token = jwt.sign(
{ username: user.username, id: user._id },
process.env.JWT_SECRET,
{
expiresIn: "1h",
}
);

const { password, ...otherDetails } = user._doc;

res
.cookie("access_token", token, {
httpOnly: true,
secure: true,
sameSite: "none",
})
.status(200)
.json({ details: { ...otherDetails }, token: token, success: true });
} catch (error) {
next(error);
}
};
76 changes: 76 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import express from "express";
import dotenv from "dotenv";
import cors from "cors";
import { connect, ifDisconnect } from "./connectionDb.js";
import cookieParser from "cookie-parser";
// here all routes are importing
import TaskRouter from "./routes/TaskRoute.js";
import AuthRouter from "./routes/AuthRouter.js";
import User from "./models/UserModel.js";
// here all routes are importing

const app = express();
connect();
ifDisconnect();

app.use(
cors({
origin: [
"https://task-management-app-9f99e.web.app",
"https://task-management-app-9f99e.firebaseapp.com",
"https://task-management-app-9f99e.firebaseapp.com/?_gl=1*ked0vo*_ga*MTk4OTA4NDE1OC4xNjkyNzI5MzIy*_ga_CW55HF8NVT*MTY5OTM3MzE1OC4yMS4xLjE2OTkzNzMyMDcuMTEuMC4w",
],
credentials: true,
})
);
dotenv.config();
app.use(express.json());
app.use(cookieParser());

const port = process.env.PORT || 8000;


app.get("/", (req, res) => {
res.json({ value: "Congrats Server is working" });
});

app.post("/test", async (req, res) => {
const newUser = new User({
username: req.body.username,
email: req.body.email,
password: req.body.password,
});

await newUser.save();

res.send({
user: newUser,
});
});

app.use(TaskRouter);
app.use("/auth", AuthRouter);

app.get("/cookie", (req, res) => {
res.send(req.cookies.access_token);
});

// app.get("/", (req, res) => {
// res.send("Welcome to the Task Management API");
// });

app.use((err, req, res, next) => {
const errorStatus = err.status || 500;
const errorMessage = err.message || "Something went wrong";

return res.status(errorStatus).json({
success: false,
status: errorStatus,
message: errorMessage,
stack: err.stack,
});
});

app.listen(port, () => {
console.log(`Server is running on port http://localhost:${port}`);
});
17 changes: 17 additions & 0 deletions models/TaskModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mongoose from "mongoose";
const { Schema, model } = mongoose;

const TaskSchema = new Schema({
title: String,
description: String,
status: String,
dueDate: Date,
assignedUser: String,
tags: [String],
priority: String,
createdDate: { type: Date, default: Date.now },
updatedDate: { type: Date, default: Date.now },
});

const Task = model("Task", TaskSchema);
export default Task;
25 changes: 25 additions & 0 deletions models/UserModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import mongoose from "mongoose";
const { Schema, model } = mongoose;

const userSchema = new Schema(
{
username: {
type: String,
required: true,
unique: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
},
{ timestamps: true }
);

const User = model("User", userSchema);
export default User;
Loading

0 comments on commit 570e5f1

Please sign in to comment.