-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
172 lines (148 loc) · 4.71 KB
/
server.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
const express = require('express');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const helmet = require('helmet');
const rateLimit = require('express-rate-limit');
const routes = require('./routes');
require('dotenv').config();
const port = 3000;
const PORT = process.env.PORT || 3000;
//live on gcloud https://red-seeker-308213.uk.r.appspot.com/login
// Passport configuration
const users = JSON.parse(process.env.USERS || '[]');
passport.use(new LocalStrategy(
(username, password, done) => {
const user = users.find(u => u.username === username && u.password === password);
if (!user) {
return done(null, false, { message: 'Incorrect username or password.' });
}
return done(null, user);
}
));
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
const user = users.find(u => u.id === id);
done(null, user);
});
const app = express();
// Security headers with helmet
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
scriptSrcAttr: ["'unsafe-inline'"],
},
},
}));
// Rate limiting
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
app.use(limiter);
app.use(express.urlencoded({ extended: false }));
app.use(session({
secret: process.env.SESSION_SECRET || 'default_session_secret',
resave: false,
saveUninitialized: false,
cookie: {
maxAge: 6000,
httpOnly: true,
secure: process.env.NODE_ENV === 'production' && PORT !== port
}
}));
app.use(passport.initialize());
app.use(passport.session());
app.set('trust proxy', 1);
app.use((req, res, next) => {
if (req.isAuthenticated() || req.path === '/login') {
next();
} else {
res.redirect('/login');
}
});
app.post('/login', passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login',
}));
app.use(routes);
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
// this stuff works great im not deleting it EVER 1/21/24
// const express = require('express');
// const session = require('express-session');
// const passport = require('passport');
// const LocalStrategy = require('passport-local').Strategy;
// const path = require('path');
// const users = [{ id: 1, username: 'user', password: 'password' }];
// passport.use(new LocalStrategy(
// (username, password, done) => {
// console.log('LocalStrategy', username, password);
// const user = users.find(u => u.username === username && u.password === password);
// if (!user) {
// return done(null, false);
// }
// return done(null, user);
// }
// ));
// passport.serializeUser((user, done) => {
// console.log('serializeUser', user);
// done(null, user.id);
// });
// passport.deserializeUser((id, done) => {
// console.log('deserializeUser', id);
// const user = users.find(u => u.id === id);
// done(null, user);
// });
// const app = express();
// app.use(express.urlencoded({ extended: false }));
// app.use(session({
// secret: 'secret',
// resave: false,
// saveUninitialized: false,
// cookie: {
// maxAge: 6000
// }
// }));
// app.use(passport.initialize());
// app.use(passport.session());
// app.use((req, res, next) => {
// console.log('Request URL:', req.url);
// next();
// });
// app.get('/', (req, res) => {
// console.log('Accessing /');
// if (req.isAuthenticated()) {
// console.log('User is authenticated');
// res.sendFile(path.join(__dirname, 'public/index.html'));
// } else {
// console.log('User is not authenticated');
// res.redirect('/login');
// }
// });
// app.get('/login', (req, res) => {
// console.log('Accessing /login');
// res.sendFile(path.join(__dirname, 'public/login.html'));
// });
// app.post('/login', passport.authenticate('local', {
// successRedirect: '/',
// failureRedirect: '/login'
// }));
// function isAuthenticated(req, res, next) {
// if (req.isAuthenticated()) {
// return next();
// }
// res.status(401).send('Unauthorized');
// }
// app.get('/style.css', isAuthenticated, (req, res) => {
// res.sendFile(path.join(__dirname, 'public/style.css'));
// });
// app.get('/script.js', isAuthenticated, (req, res) => {
// res.sendFile(path.join(__dirname, 'public/script.js'));
// });
// app.listen(3000, () => console.log('Server started on port 3000'));