-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
250 lines (224 loc) · 8.19 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var env = require('dotenv').config();
const { v4: uuid } = require('uuid');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
var session = require('express-session');
var flash = require('express-flash');
var pgSession = require('connect-pg-simple')(session);
const { Client } = require('pg');
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
},
});
client.connect(err => {
if (err) {
console.error('Connection error:', err.stack);
} else {
console.log('Connected to PostgreSQL database');
}
});
var bcrypt = require('bcryptjs');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
passport.use(new LocalStrategy({
usernameField: 'username',
passwordField: 'password'
},
function (username, password, done) {
client.query('SELECT * FROM capstone_users WHERE username = $1', [username], function (err, result) {
if (err) {
console.log("SQL error:", err);
return done(null, false, { message: 'SQL error' });
}
if (result.rows.length > 0) {
let matched = bcrypt.compareSync(password, result.rows[0].password);
if (matched) {
console.log("Successful login,", result.rows[0]);
return done(null, result.rows[0]);
}
}
console.log("Bad username or password");
return done(null, false, { message: 'Bad username or password' });
});
})
);
passport.serializeUser(function (user, done) {
return done(null, user.id); // Pass the user ID to done
});
passport.deserializeUser(function (id, done) {
client.query('SELECT * FROM capstone_users WHERE id = $1', [id], function (err, result) {
if (err) {
console.log("SQL error:", err);
return done(err);
}
if (result.rows.length > 0) {
return done(null, result.rows[0]);
} else {
return done(new Error('User not found'));
}
});
});
app.use(session({
store: new pgSession({
pool: client, // Using the existing PostgreSQL client
tableName: 'session', // Optionally provide a table name, default is 'session'
createTableIfMissing: true // Automatically create the table if it doesn't exist
}),
secret: 'WebDev', // Change this to a more secure secret
resave: false,
saveUninitialized: false,
cookie: { secure: false } // Set secure: true in production with HTTPS
}));
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.get('/checkAuth', async function (req, res) {
if (req.isAuthenticated()) {
const user = req.user;
try {
const query = 'SELECT country FROM tournaments WHERE tournament = $1';
const result = await client.query(query, [user.tournament]);
const userCountry = result.rows[0].country;
res.json({
isAuthenticated: true,
preferredTournament: user.tournament,
preferredCountry: userCountry,
preferredTeam: user.team,
logoUrl: user.team_logo
});
} catch (err) {
console.error('Error constructing user preferences response:', err);
res.json({ isAuthenticated: true });
}
} else {
res.json({ isAuthenticated: false });
}
});
app.get('/getUserInfo', function (req, res) {
if (req.isAuthenticated()) {
const user = req.user;
res.json({ username: user.username, tournament: user.tournament, team: user.team, team_logo: user.team_logo });
} else {
res.status(401).json({ message: 'Not authenticated' });
}
});
app.post('/saveUser', function (req, res) {
if (req.isAuthenticated()) {
const user = req.user;
const { username, password, tournament, team, team_logo } = req.body;
console.log(req.body);
const hashedPassword = bcrypt.hashSync(password, 10);
client.query('UPDATE capstone_users SET username = $1, password = $2, tournament = $3, team = $4, team_logo = $5 WHERE id = $6', [username, hashedPassword, tournament, team, team_logo, user.id], function (err) {
if (err) {
console.error('Error updating user:', err);
res.status(500).send('Failed to update user');
} else {
res.redirect('/');
}
});
} else {
res.status(401).send('User not authenticated');
}
});
app.get('/logout', function (req, res) {
req.logout(function () {
res.sendStatus(200);
});
});
app.get('/signUp', function (req, res) {
res.sendFile(path.join(__dirname, 'public', 'signUp.html'));
});
app.post('/signUp', async function (req, res) {
try {
const createTableQuery = `CREATE TABLE IF NOT EXISTS capstone_users (
id UUID PRIMARY KEY NOT NULL,
username VARCHAR(50) NOT NULL,
password VARCHAR(100) NOT NULL,
tournament VARCHAR(50) NOT NULL,
team VARCHAR(80) NOT NULL,
team_logo VARCHAR(200) NOT NULL,
account_created_on DATE NOT NULL
)`;
await client.query(createTableQuery);
const { username, password, tournament, team, team_logo } = req.body;
const result = await client.query('SELECT * FROM capstone_users WHERE username = $1', [username]);
if (result.rows.length > 0) {
req.flash('error', 'Username already exists');
res.redirect('/signUp');
return;
}
const hashedPassword = bcrypt.hashSync(password, 10);
const id = uuid();
await client.query('INSERT INTO capstone_users (id, username, password, tournament, team, team_logo, account_created_on) VALUES ($1, $2, $3, $4, $5, $6, CURRENT_DATE)', [id, username, hashedPassword, tournament, team, team_logo]);
const user = { id, username, password: hashedPassword, tournament, team };
req.login(user, function (err) {
if (err) {
console.error('Error logging in:', err);
req.flash('error', 'Failed to log in user');
return res.redirect('/signUp');
}
return res.redirect('/');
});
} catch (error) {
console.error('Error creating user:', error);
req.flash('error', 'Failed to create user');
res.redirect('/signUp');
}
});
app.get('/login', function (req, res) {
res.sendFile(path.join(__dirname, 'public', 'login.html'));
});
app.post('/login', passport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login',
failureFlash: true
}));
app.get('/myProfile', function (req, res) {
if (req.isAuthenticated()) {
res.sendFile(path.join(__dirname, 'public', 'myProfile.html'));
} else {
res.redirect('/login');
}
});
app.get('/standings', function (req, res) {
if (req.isAuthenticated()) {
res.sendFile(path.join(__dirname, 'public', 'scoreboard.html'));
} else {
res.redirect('/login');
}
});
app.get('/api/tournaments', async (req, res) => {
try {
const result = await client.query('SELECT tournament, logo FROM tournaments');
res.json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('Server error');
}
});
app.get('/api/:tournamentName', async (req, res) => {
const tournamentName = req.params.tournamentName;
try {
const query = `SELECT name, logo FROM "${tournamentName}"`;
const result = await client.query(query);
res.json(result.rows);
} catch (err) {
console.error(err);
res.status(500).send('Server error');
}
});
module.exports = app;