-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 570e5f1
Showing
15 changed files
with
2,798 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Task-Management-Server | ||
# Task-management-server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.