-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
179 lines (153 loc) · 4.48 KB
/
index.js
File metadata and controls
179 lines (153 loc) · 4.48 KB
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
import dotenv from "dotenv";
import express from "express";
import bodyParser from "body-parser";
import session from "express-session";
import passport from "passport";
import { Strategy } from "passport-local";
import flash from "connect-flash";
// Import our refactored modules
import { CONFIG } from './config.js';
import db from './database.js';
import { fetchClashRoyaleCard } from './apiService.js';
import { checkAnswer, getUserHighscore, getLeaderboard } from './gameService.js';
import { createUser, findUserByUsername, verifyPassword } from './authService.js';
dotenv.config();
const app = express();
// View engine setup
app.set('view engine', 'ejs');
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
// Session configuration
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
cookie: {
maxAge: CONFIG.SESSION_MAX_AGE
}
}));
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Flash messages
app.use(flash());
app.use((req, res, next) => {
res.locals.message = req.flash("error");
next();
});
// Passport configuration
passport.use(new Strategy(async function verify(username, password, cb) {
try {
const user = await findUserByUsername(username);
if (user) {
const isValidPassword = await verifyPassword(password, user.password);
if (isValidPassword) {
return cb(null, user);
} else {
return cb(null, false, { message: "Incorrect password" });
}
} else {
return cb(null, false, { message: "User not found" });
}
} catch (error) {
return cb(error);
}
}));
passport.serializeUser((user, cb) => {
cb(null, user);
});
passport.deserializeUser((user, cb) => {
cb(null, user);
});
// http routes
app.get("/", (req, res) => {
res.render("login.ejs");
});
app.get("/register", (req, res) => {
res.render("register.ejs");
});
app.post("/register", async (req, res) => {
const { username, password } = req.body;
try {
await createUser(username, password);
res.redirect("/");
} catch (error) {
res.send(error.message);
}
});
app.post("/login", (req, res, next) => {
passport.authenticate("local", (err, user, info) => {
if (err) return next(err);
if (!user) {
req.flash("error", info.message);
return res.redirect("/");
}
req.logIn(user, (err) => {
if (err) return next(err);
return res.redirect("/guess");
});
})(req, res, next);
});
app.post('/logout', (req, res, next) => {
req.logout((err) => {
if (err) return next(err);
res.redirect('/');
});
});
app.get("/guess", async (req, res) => {
if (req.isAuthenticated()) {
try {
// initialize session variables
if (req.session.feedback === undefined) {
req.session.feedback = null;
}
if (req.session.score === undefined) {
req.session.score = 0;
}
const user = req.user;
const highscore = await getUserHighscore(user.username);
const card = await fetchClashRoyaleCard();
if (!card) {
return res.status(500).send('Failed to fetch card');
}
req.session.cardName = card.name;
res.render("index.ejs", {
card: card,
currentScore: req.session.score,
highscore: highscore,
feedback: req.session.feedback
});
} catch (error) {
console.error('Error in /guess:', error);
res.status(500).send('Something went wrong');
}
} else {
res.redirect("/");
}
});
app.post("/guess", async (req, res) => {
const { guess } = req.body;
const cardName = req.session.cardName;
const currentUser = req.user;
try {
await checkAnswer(req, guess, cardName, currentUser.username);
res.redirect("/guess");
} catch (error) {
console.error('Error processing guess:', error);
res.status(500).send('Something went wrong');
}
});
app.get("/leaderboard", async (req, res) => {
try {
const leaderboard = await getLeaderboard(CONFIG.LEADERBOARD_LIMIT);
res.render("leaderboard.ejs", { leaderboard });
} catch (error) {
console.error('Error getting leaderboard:', error);
res.status(500).send('Failed to load leaderboard');
}
});
// Start server
app.listen(CONFIG.PORT, () => {
console.log(`Server running on port ${CONFIG.PORT}`);
});