From e0f7137f16972ce275eb283645fe47d95989b645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Gomo=C5=82a?= Date: Sun, 24 Mar 2024 15:50:39 +0100 Subject: [PATCH] Fixed logging in/creating account --- index.js | 4 ++-- public/assets/js/socket-game.js | 3 ++- public/assets/js/test.sql | 2 ++ utils/auth.js | 18 ++++++++++-------- 4 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 public/assets/js/test.sql diff --git a/index.js b/index.js index ab322ea..eba56f9 100644 --- a/index.js +++ b/index.js @@ -376,7 +376,7 @@ io.on('connection', async (socket) => { let UTCTs = Math.floor((new Date()).getTime() / 1000 + 90); io.to(playerGame.id).emit('turn update', { turn: 0, phase: "preparation", timerToUTC: UTCTs }); - GInfo.timer(playerGame.id, 10, async () => { + GInfo.timer(playerGame.id, 90, async () => { const playerGame = await GInfo.getPlayerGameData(socket); for (let i = 0; i < playerGame.data.boards.length; i++) { const ships = playerGame.data.boards[i].ships; @@ -552,4 +552,4 @@ function validateEmail(email) { .match( /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ); -}; \ No newline at end of file +}; diff --git a/public/assets/js/socket-game.js b/public/assets/js/socket-game.js index 08de7d4..9bccb0b 100644 --- a/public/assets/js/socket-game.js +++ b/public/assets/js/socket-game.js @@ -195,4 +195,5 @@ socket.on('turn update', (turnData) => { socket.on('player left', () => { window.location.replace("/"); }); -// SELECT ROUND((1 - AVG(statistics.won)) * 100) AS winrate, COUNT(statistics.match_id) AS alltime_matches, COUNT(CASE WHEN (YEAR(matches.date) = YEAR(NOW()) AND MONTH(matches.date) = MONTH(NOW())) THEN matches.match_id END) AS monthly_matches FROM accounts NATURAL JOIN statistics NATURAL JOIN matches WHERE accounts.nickname = "MaciejkaG"; \ No newline at end of file +// SELECT ROUND((1 - AVG(statistics.won)) * 100) AS winrate, COUNT(statistics.match_id) AS alltime_matches, COUNT(CASE WHEN (YEAR(matches.date) = YEAR(NOW()) AND MONTH(matches.date) = MONTH(NOW())) THEN matches.match_id END) AS monthly_matches FROM accounts NATURAL JOIN statistics NATURAL JOIN matches WHERE accounts.nickname = "MaciejkaG"; +// \ No newline at end of file diff --git a/public/assets/js/test.sql b/public/assets/js/test.sql new file mode 100644 index 0000000..0705c16 --- /dev/null +++ b/public/assets/js/test.sql @@ -0,0 +1,2 @@ +-- SELECT nickname, host_id, board, match_type, date FROM accounts NATURAL JOIN statistics JOIN matches ON matches.host_id = accounts.user_id WHERE nickname = "deeznuts24"; +SELECT accounts.nickname, statistics.board, matches.match_type, matches.duration, date FROM statistics JOIN matches ON statistics.match_id = matches.match_id NATURAL JOIN accounts me JOIN accounts opp ON matches.guest_id = a WHERE me.nickname = "deeznuts24"; \ No newline at end of file diff --git a/utils/auth.js b/utils/auth.js index 0799e22..32767b4 100644 --- a/utils/auth.js +++ b/utils/auth.js @@ -15,8 +15,6 @@ const rl = readline.createInterface({ const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); -const saltRounds = 10; - export class MailAuth { constructor(redis, options, mysqlOptions) { this.redis = redis; @@ -66,15 +64,19 @@ export class MailAuth { const conn = mysql.createConnection(this.mysqlOptions); conn.query(`SELECT user_id, nickname FROM accounts WHERE email = ${conn.escape(email)}`, async (error, response) => { if (error) { reject(error); return; } - let timer = await this.redis.get(`loginTimer:${response[0].user_id}`); - if (response.length !== 0 && timer && timer > 0) { - resolve({ status: -1, uid: response[0].user_id }); - return; + if (response.length !== 0) { + let timer = await this.redis.get(`loginTimer:${response[0].user_id}`); + if (timer && timer > 0) { + resolve({ status: -1, uid: response[0].user_id }); + return; + } } if (response.length === 0 || response[0].nickname == null) { + if (response.length === 0) { + conn.query(`INSERT INTO accounts(email) VALUES (${conn.escape(email)});`, (error) => { if (error) reject(error) }); + } - conn.query(`INSERT INTO accounts(email) VALUES (${conn.escape(email)});`, (error) => { if (error) reject(error) }); conn.query(`SELECT user_id, nickname FROM accounts WHERE email = ${conn.escape(email)}`, async (error, response) => { if (error) reject(error); const row = response[0]; @@ -82,7 +84,7 @@ export class MailAuth { const html = fs.readFileSync(path.join(__dirname, 'mail/auth-code-firsttime.html'), 'utf8'); let authCode = genCode(); - await this.redis.json.set(`codeAuth:${authCode}`, "$", { uid: row.user_id, tid: tId }); + await this.redis.set(`codeAuth:${authCode}`, row.user_id); await this.timer(row.user_id, 600, async () => { await this.redis.json.del(`codeAuth:${authCode}`);