-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
160 lines (135 loc) · 4.35 KB
/
Copy pathapp.js
File metadata and controls
160 lines (135 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const express = require('express');
const app = express();
const path = require('path');
const cookieParser = require('cookie-parser');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
// Models ko bulana (Importing Models)
const User = require('./models/User');
const Note = require('./models/Note');
// Database Connection
mongoose.connect("mongodb://127.0.0.1:27017/devflow")
.then(() => console.log("Connected to MongoDB..."))
.catch(err => console.error("Could not connect to MongoDB...", err));
// Middlewares
app.set('view engine', 'ejs');
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
//----------------------
// ROUTES
//----------------------
// Register Page
app.get("/", function(req, res){
res.render("register");
});
// Register Logic
app.post("/register", async function(req, res){
let { name, email, password } = req.body;
let user = await User.findOne({ email });
if(user){
return res.send("User already exists");
}
const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(password, salt);
let newUser = await User.create({ name, email, password: hash });
let token = jwt.sign({ email: email, userid: newUser._id }, "shhhhhh");
res.cookie("token", token);
res.redirect("/dashboard"); // Register ke baad seedha dashboard
});
// Login Page
app.get("/login", function(req, res){
res.render("login");
});
// Login Logic
app.post("/login", async function (req, res){
let { email, password } = req.body;
let user = await User.findOne({ email });
if(!user) return res.send("User not found");
let isMatch = await bcrypt.compare(password, user.password);
if(isMatch){
let token = jwt.sign({ email: email, userid: user._id }, "shhhhhh");
res.cookie("token", token);
res.redirect("/dashboard");
}
else{
res.send("Incorrect email or password");
}
});
// Middleware: isLoggedIn
function isLoggedIn (req, res, next){
let token = req.cookies.token;
if(!token) return res.redirect("/login");
try {
let data = jwt.verify(token, "shhhhhh");
req.user = data;
next();
} catch(err) {
res.redirect("/login");
}
}
// Dashboard with Search Logic (Only one route)
app.get("/dashboard", isLoggedIn, async function(req, res){
let searchQuery = req.query.search || "";
let user = await User.findOne({ email: req.user.email }).populate({
path: 'notes',
match: {
$or: [
{ title: { $regex: searchQuery, $options: 'i' } },
{ content: { $regex: searchQuery, $options: 'i' } },
{ category: { $regex: searchQuery, $options: 'i' } }
]
}
});
res.render("dashboard", { user: user, searchVal: searchQuery });
});
// Create Note
app.post("/create-note", isLoggedIn, async function (req, res) {
let user = await User.findOne({ email: req.user.email });
let { title, content, codeSnippet, category } = req.body;
let newNote = await Note.create({
user: user._id,
title,
content,
codeSnippet,
category
});
user.notes.push(newNote._id);
await user.save();
res.redirect("/dashboard");
});
// Delete Note
app.post("/delete/:id", isLoggedIn, async function(req, res){
await User.findOneAndUpdate(
{ email: req.user.email },
{ $pull: { notes: req.params.id } }
);
await Note.findOneAndDelete({ _id: req.params.id });
res.redirect("/dashboard");
});
// Edit Page
app.get("/edit/:id", isLoggedIn, async function (req, res) {
let note = await Note.findOne({ _id: req.params.id });
res.render("edit", { note: note });
});
// Update Logic
app.post("/update/:id", isLoggedIn, async function (req, res) {
let { title, content, codeSnippet, category } = req.body;
await Note.findOneAndUpdate(
{ _id: req.params.id },
{ title, content, codeSnippet, category },
{ new: true }
);
res.redirect("/dashboard");
});
// Logout
app.get("/logout", function (req, res) {
res.cookie("token", "");
res.redirect("/login");
});
// Server Start
app.listen(3000, function () {
console.log("Server is running on port 3000");
});