-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
182 lines (149 loc) · 4.93 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
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const Listing = require('/workspaces/Wunderlust/Models/listings.js'); // Adjust path as needed
const app = express();
const exec = require('child_process').exec;
const passport = require('passport');
const localStrategy = require("passport-local");
const port = process.env.PORT || 8081;
const methodOverride = require('method-override');
const engine = require('ejs-mate');
const session = require('express-session');
const User = require('./Models/user.js');
const flash = require('connect-flash');
const cluster_address=process.env.CLUSTER_ADDRESS
const crypto = require('crypto');
const { isLoggedin } = require('./middleware.js');
const secretKey = crypto.randomBytes(32).toString('hex');
// Replace with your MongoDB Atlas connection string
mongoose.connect(cluster_address)
.then(() => console.log('Connected to MongoDB Atlas and using the wanderlust database!'))
.catch(err => console.error('Could not connect to MongoDB Atlas.', err));
// Set views and view engine
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride('_method'));
app.engine('ejs', engine);
app.use(express.static(path.join(__dirname, 'public')));
app.use(flash());
app.use(session({
secret: secretKey,
resave: false,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new localStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use((req, res, next) => {
res.locals.success = req.flash("success");
res.locals.failure = req.flash("failure")
next();
});
app.get('/', (req, res) => {
res.redirect("/listings")
});
app.get("/test",(req,res)=>{
req.flash("failure","Testing Success")
res.redirect("/listings")
})
app.get("/listings",async (req,res)=>{
const allListing=await Listing.find({});
res.render('../views/listings/index.ejs', { allListing });
})
app.get("/signup", (req, res) => {
res.render('../views/users/signup.ejs');
});
app.post("/signup", async (req, res) => {
try {
let { username, email, password } = req.body;
const newUser = new User({ email, username });
const registerUser = await User.register(newUser, password);
console.log(registerUser);
req.flash("success", "Welcome to Wanderlust");
res.redirect("/listings");
} catch (e) {
console.log("error")
req.flash("error", e.message);
res.locals.failure=flash("failure","Invalid Credentials")
res.redirect("/signup");
}
});
app.post("/signin", (req, res, next) => {
passport.authenticate('local', (err, user, info) => {
if (err) {
return next(err);
}
if (!user) {
req.flash("failure", "Invalid username or password");
return res.redirect('/signin');
}
req.logIn(user, (err) => {
if (err) {
return next(err);
}
req.flash("success", "Loggedin");
return res.redirect('/listings');
});
})(req, res, next);
});
app.post("/logout",(req,res)=>{
if (err) {
return next(err);
}
req.flash("success", "Loggedout");
req.logOut;
res.redirect("/listings")
})
app.get("/signin", (req, res) => {
res.render('../views/users/signin.ejs');
});
app.get("/demouser", async(req,res)=>{
const fakeUser = new User({
email:"[email protected]",
username:"delta-student",
})
let registeredUser=await User.register(fakeUser,"helloworld")
res.send(registeredUser)
})
app.get("/listings/new",isLoggedin,(req,res)=>{
res.render("../views/listings/new.ejs")
})
app.post("/listings",async (req,res)=>{
const newlisting=new Listing(req.body);
newlisting.save();
const allListing=await Listing.find({});
res.render("../views/listings/index.ejs", { allListing })
})
app.get("/listings/:id",isLoggedin,async (req,res)=>{
const {id}=req.params;
const listing=await Listing.findById(id);
console.log(listing)
res.render("../views/listings/show.ejs",{listing});
})
app.get("/listings/:id/edit",isLoggedin,async(req,res)=>{
const {id}=req.params;
const listing=await Listing.findById(id);
console.log("Not Working")
res.render("../views/listings/edit.ejs",{listing});
})
app.put("/listings/:id", async (req, res) => {
const { id } = req.params;
// Assuming req.body.listing holds the updated values for the listing, including the image URL as a string
const updatedData = req.body.listing;
await Listing.findByIdAndUpdate(id, updatedData, { new: true }); // Add { new: true } to get the updated document
res.redirect("/listings/" + id); // Redirect to show the updated listing
});
app.delete("/listings/:id",async(req, res) => {
const { id } = req.params;
await Listing.findByIdAndDelete(id);
res.redirect("/listings")
})
// Start server
app.listen(port, () => {
console.log(`Listening at port ${port}`);
});