-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
189 lines (154 loc) · 4.94 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
const express = require("express");
const path = require("path");
const bodyparser = require('body-parser');
const mongoose = require('mongoose');
const session = require('express-session');
const flash = require('express-flash');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcrypt');
const User = require('./models/user');
const Blog = require('./models/blog');
const app = express();
app.use(flash());
const port = 80;
app.use("/static", express.static("static"));
app.use(express.urlencoded());
var cons = require('consolidate');
// view engine setup
app.engine('html', cons.swig)
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://127.0.0.1:27017/goblogs');//"goblogs" is my local dbs name in mongosh
};
// Configure express-session middleware
app.use(session({
secret: '124421', // Replace with a secret key for session encryption
resave: false,
saveUninitialized: false
}));
// Initialize passport and session
app.use(passport.initialize());
app.use(passport.session());
// Configure Passport to use a local strategy for user authentication
passport.use(new LocalStrategy(
{ usernameField: 'email' }, // Use email as username
async (email, password, done) => {
try {
const user = await User.findOne({ email });
if (!user) {
return done(null, false, { message: 'Incorrect email' });
}
const passwordMatch = await bcrypt.compare(password, user.password);
if (!passwordMatch) {
return done(null, false, { message: 'Incorrect password' });
}
return done(null, user); // Authentication successful
} catch (error) {
return done(error);
}
}
));
// Serialize and deserialize user for session management
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
try {
const user = await User.findById(id);
done(null, user);
} catch (error) {
done(error);
}
});
// Signup route
app.post('/signup', async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const newUser = new User({
name: req.body.name,
email: req.body.email,
password: hashedPassword
});
await newUser.save();
req.flash('success', 'Successfully signed up!');
res.redirect('/login'); // Redirect to login page after signup
} catch (error) {
console.error(error);
res.status(500).send('Error signing up');
}
});
// Login route
app.post('/login', passport.authenticate('local', {
successRedirect: '/welcome',
failureRedirect: '/login',
failureFlash: true
}));
app.get('/welcome', (req, res) => {
Blog.find({}).then(blogs => {
res.render('welcome', { blogs }); // Render the welcome page with blog data
}).catch(err => {
console.error(err);
});
});
app.get('/blog/:id', (req, res) => {
const blogId = req.params.id;
if (!mongoose.Types.ObjectId.isValid(blogId)) {
return res.status(400).send('Invalid blog ID');
}
Blog.findById(blogId)
.then(blog => {
if (!blog) {
return res.status(404).send('Blog not found');
}
res.render('blog', { blog }); // Render the individual blog page
})
.catch(err => {
console.error(err);
res.status(500).send('Error fetching blog');
});
});
app.post('/create', (req, res) => {
const newBlog = new Blog({
title: req.body.title,
body: req.body.body,
author: req.user._id // Assuming you have user data in the session
});
newBlog.save()
.then(() => {
res.redirect('/welcome');
})
.catch(err => {
console.error(err);
res.status(500).send('Error signing up');
});
});
app.get('/', (req, res) => {
const params = {}
res.status(200).render("home.html", params);
});
app.get('/help', (req, res) => {
const params = {}
res.status(200).render("help.html", params);
});
app.get('/login', (req, res) => {
const params = {}
res.status(200).render("login.html", params);
});
app.get('/welcome', (req, res) => {
const params = {}
res.status(200).render("welcome.html", params);
});
app.get('/create', (req, res) => {
const params = {}
res.status(200).render("create.html", params);
});
app.get('/signup', (req, res) => {
const params = {}
res.status(200).render("signup.html", params);
});
app.listen(port, () => {
console.log(`app started successfully on port ${port}`)
});