-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
234 lines (192 loc) · 6.78 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const session = require('express-session');
const passport = require('passport');
const flash = require('connect-flash');
const LocalStrategy = require('passport-local').Strategy;
const bodyParser = require('body-parser');
const { body, validationResult } = require('express-validator');
const app = express();
// Connect to MongoDB
mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('MongoDB Connected');
})
.catch((err) => {
console.error('MongoDB Connection Error:', err);
});
// Set EJS as the view engine
app.set('view engine', 'ejs');
// Specify the folder where your views are located (default is 'views' folder)
app.set('views', path.join(__dirname, 'views'));
// Serve static files from the 'public' folder
app.use(express.static('public', { 'extensions': ['html', 'htm'] }));
// Setup express-session using the session from the environment variable '.env'
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false
}));
// Setup passport
app.use(passport.initialize());
app.use(passport.session());
// Use User Model
const User = require('./models/user');
// Passport Local Strategy
passport.use(new LocalStrategy({
usernameField: 'identifier',
passwordField: 'password'
}, async (identifier, password, done) => {
try {
const user = await User.findOne({
$or: [{ username: identifier }, { email: identifier }]
});
if (!user) {
return done(null, false, { message: 'Incorrect username or email.' });
}
const authenticated = await user.authenticate(password);
if (!authenticated) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
} catch (err) {
return done(err);
}
}));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// Use connect-flash middleware
app.use(flash());
// Use body-parser middleware
app.use(bodyParser.urlencoded({ extended: true }));
// Middleware to check if the user is logged in
function isLoggedIn(req, res, next) {
console.log('Session data:', req.session);
console.log('Deserialized user:', req.user);
console.log('Checking authentication status:', req.isAuthenticated());
if (req.isAuthenticated()) {
return next();
}
// Redirect to login only if the current route is not /login
if (req.originalUrl !== '/login') {
return res.redirect('/login');
}
// If the current route is /login, render the login page
res.render('login');
}
// GET route for '/'
app.get('/', (req, res) => {
res.render('index'); // Replace 'index' with the actual name of your home page or the route you want to render.
});
// GET route to render /views/login.ejs
app.get('/login', (req, res) => {
res.render('login'); // Assuming 'login.ejs' is in your 'views' folder
});
// POST route for handling login form submission
app.post('/login', passport.authenticate('local', {
successRedirect: '/home',
failureRedirect: '/login',
failureFlash: true // Enable flash messages for authentication failures
}), (req, res) => {
console.log('User authenticated:', req.isAuthenticated());
});
// Use routes in 'auth.js'
app.use(require('./routes/auth'));
// GET route to redirect to 'registration-success.ejs' success page
app.get('/registration-success', (req, res) => {
res.render('registration-success');
});
// GET route to render 'home.ejs'
app.get('/home', isLoggedIn, (req, res) => {
res.render('home');
});
// GET route to render 'bookmarked-jobs.ejs'
app.get('/bookmarked-jobs', isLoggedIn, (req, res) => {
res.render('bookmarked-jobs');
});
// GET route to render 'featured-jobs.ejs'
app.get('/featured-jobs', isLoggedIn, (req, res) => {
res.render('featured-jobs');
});
// POST route to edit user personal information
app.post(
'/edit-profile',
isLoggedIn,
[
body('fullName').trim().notEmpty().withMessage('Full Name is required'),
body('surname').trim().notEmpty().withMessage('Surname is required'),
body('username').trim().notEmpty().withMessage('Username is required'),
body('email').trim().isEmail().withMessage('Invalid email address'),
],
async (req, res, next) => {
// Check for validation errors
const errors = validationResult(req);
if (!errors.isEmpty()) {
// If it's an AJAX request, respond with JSON
if (req.xhr) {
return res.status(400).json({ errors: errors.array() });
}
// If it's a regular request, redirect to the profile page with errors
return res.status(400).redirect('/profile?error=true');
}
try {
const { username, email, fullName, surname } = req.body;
const updatedUser = await User.findByIdAndUpdate(
req.user._id,
{ $set: { username, email, fullName, surname } },
{ new: true }
);
req.login(updatedUser, (err) => {
if (err) {
return next(err);
}
console.log('User object in session updated:', req.user);
});
// If it's an AJAX request, respond with success
if (req.xhr) {
return res.status(200).json({ message: 'Profile updated successfully!' });
}
// If it's a regular request, redirect to the profile page
res.redirect('/profile');
} catch (error) {
console.error('Profile update error:', error);
next(error);
}
}
);
// Edit experience
app.get('/edit-experience', isLoggedIn, (req, res) => {
res.render('edit-experience', { user: req.user });
});
// Handle the form submission
app.post('/edit-experience', isLoggedIn, async (req, res, next) => {
try {
const { title, company, startDate, endDate, description } = req.body;
// Update the user's experiences
req.user.experiences.push({
title,
company,
startDate,
endDate,
description,
});
// Save the updated user
await req.user.save();
res.redirect('/profile'); // Redirect to the profile
} catch (error) {
console.error('Edit experience error:', error);
next(error);
}
});
// Route handler for logout
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});