-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
206 lines (181 loc) · 5.24 KB
/
app.js
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
require("dotenv").config();
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const passport = require("passport");
const session = require("express-session");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const findOrCreate = require("mongoose-findorcreate");
const cors = require("cors");
const MongoStore = require("connect-mongo");
const app = express();
// const corsOptions = {
// origin: "http://localhost:3001",
// optionsSuccessStatus: 204,
// credentials: true,
// };
const corsOptions = {
origin: process.env.FRONTEND_URL || "http://localhost:3001", // Use your frontend's URL or localhost for development
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true, // Allow session cookie from browser to pass through
// allowedHeaders: ["Content-Type", "Authorization"],
// exposedHeaders: ["set-cookie"],
};
app.use(cors(corsOptions));
app.set("trust proxy", 1);
const PORT = process.env.PORT || 3000;
app.use(express.static("public"));
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
// setup database connection
try {
mongoose.connect(
process.env.MONGODB_URI,
// || "mongodb://localhost:27017/notesDB"
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
console.log("Database connected successfully");
} catch (error) {
console.log(error);
}
const noteSchema = new mongoose.Schema({
title: String,
content: String,
});
const userSchema = new mongoose.Schema({
// email: String,
googleId: String,
notes: [noteSchema],
});
app.use(
session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.MONGODB_URI }),
cookie: {
secure: true,
sameSite: "none",
},
})
);
// setup passport for authentication
app.use(passport.initialize());
app.use(passport.session());
userSchema.plugin(findOrCreate);
const User = new mongoose.model("User", userSchema);
passport.serializeUser(function (user, cb) {
process.nextTick(function () {
return cb(null, {
id: user._id,
username: user.username,
picture: user.picture,
});
});
});
passport.deserializeUser(function (user, cb) {
process.nextTick(function () {
return cb(null, user);
});
});
passport.use(
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL:
process.env.GOOGLE_CALLBACK_URL ||
"http://localhost:3000/auth/google/secrets",
},
function (accessToken, refreshToken, profile, cb) {
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return cb(err, user);
});
}
)
);
//load balancer health check
app.get("/health", (req, res) => {
res.status(200).send("OK");
});
app
.route("/auth/google")
.get(passport.authenticate("google", { scope: ["profile"] }));
app
.route("/auth/google/secrets")
.get(
passport.authenticate("google", { failureRedirect: "/login" }),
(req, res) => {
console.log("redirecting to home page");
res.redirect(process.env.FRONTEND_URL || "http://localhost:3001");
}
);
app.get("/isAuthenticated", (req, res) => {
if (req.isAuthenticated()) {
res.json({ isAuthenticated: true });
} else {
res.json({ isAuthenticated: false });
}
});
// Server code
app.post("/logout", (req, res) => {
req.session.destroy((err) => {
if (err) {
console.log(err);
return res.status(500).send("An error occurred while logging out");
}
res.clearCookie("connect.sid"); // If you're using 'connect.sid' as the cookie name
res.status(200).send("Logged out successfully");
});
});
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) {
return next();
} else {
res.redirect("/login");
}
}
// Get all notes
app.get("/notes", ensureAuthenticated, async (req, res) => {
try {
const user = await User.findById(req.user.id);
res.json(user.notes);
} catch (err) {
console.error(err);
res.status(500).send("An error occurred");
}
});
// Create a new note
app.post("/notes", ensureAuthenticated, async (req, res) => {
try {
const { title, content } = req.body; // extract title and content from the request
const note = { title, content }; // create a new note object
// Find the user and update their notes array
const user = await User.findById(req.user.id);
user.notes.push(note);
await user.save();
res.status(201).json(note); // send the created note back
} catch (err) {
console.error(err);
res.status(500).send("An error occurred");
}
});
// Delete a note
app.delete("/notes/:id", ensureAuthenticated, async (req, res) => {
const noteId = req.params.id;
try {
const user = await User.findById(req.user.id);
user.notes = user.notes.filter((note) => note._id.toString() !== noteId);
await user.save();
res.status(200).json({ message: "Note deleted successfully" });
} catch (err) {
console.error(err);
res.status(500).send("An error occurred");
}
});
app.listen(PORT, () => {
console.log(`Server started on port ${PORT}`);
});