From 0ff1db6fb46772405a07be9b218e24e0dac2c805 Mon Sep 17 00:00:00 2001 From: Felix Albrigtsen Date: Fri, 22 Apr 2022 11:16:41 +0200 Subject: [PATCH] Started login system --- src/server/index.js | 69 ++++++++++++++++++--- src/server/management/initDB.sql | 40 +++--------- src/server/tmdb.js | 103 +++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+), 40 deletions(-) diff --git a/src/server/index.js b/src/server/index.js index d7770cb..a379581 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -1,6 +1,7 @@ const path = require("path"); const express = require("express"); const session = require('express-session'); +const https = require("https"); require("dotenv").config(); // Our self-written module for handling database operations @@ -8,7 +9,7 @@ let tmdb = require("./tmdb.js"); // #region Express setup const app = express(); -const port = 3000; +const port = 3001; app.listen(port, () => { console.log(`Listening on port ${port}`) }) @@ -29,6 +30,7 @@ api.use(function(req, res, next) { next(); }); api.use(require('express-log-url')); +app.use(require('express-log-url')); // #endregion @@ -78,10 +80,17 @@ app.get('/auth/google', app.get('/auth/google/callback', passport.authenticate('google', { failureRedirect: '/error' }), - function(req, res) { - // Successful authentication, redirect success. - res.redirect('/success'); - }); + async function(req, res) { + // Get user profile from passport + let user = { + id: req.user.id, + name: req.user.displayName, + email: req.user.emails[0].value + } + req.session.user = user; + res.json(user); + } +); // #endregion @@ -89,8 +98,8 @@ app.get('/auth/google/callback', // #region API api.get("/tournament/getTournaments", (req, res) => { tmdb.getTournaments() - .then(tournaments => res.json({"status": "OK", "data": tournaments})) - .catch(err => res.json({"status": "error", "data": err})); + .then(tournaments => res.json({"status": "OK", "data": tournaments})) + .catch(err => res.json({"status": "error", "data": err})); }); // #region tournament/:tournamentId @@ -349,3 +358,49 @@ api.post("/tournament/create", (req, res) => { }); // #endregion + +// #region users + +api.get("/users/getSessionUser", (req, res) => { + if (req.session.user) { + res.json({"status": "OK", "data": req.session.user}); + } else { + res.json({"status": "error", "data": "No user logged in"}); + } +}); + +api.get("/users/getUsers", (req, res) => { + tmdb.getUsers() + .then(users => res.json({"status": "OK", "data": users})) + .catch(err => res.json({"status": "error", "data": err})); +}); + +api.post("/users/createBlank", (req, res) => { + let email = req.body.newUserEmail; + // Check if the user already exists + tmdb.getUserByEmail(email) + .then(user => { + res.json({"status": "error", "data": "User already exists", user: user}); + }) + .catch(err => { + console.log(err); + if (err == "No such user exists") { + // Create a new user + tmdb.createUserBlank(email) + .then(user => { + res.json({"status": "OK", "data": user}); + }) + .catch(err => { + res.json({"status": "error", "data": err}); + }); + } else { + res.json({"status": "error", "data": err}); + } + }); +}); + +api.post("/users/edit", (req, res) => { + +}); + +// #endregion \ No newline at end of file diff --git a/src/server/management/initDB.sql b/src/server/management/initDB.sql index 4bf4bfb..deeaf60 100644 --- a/src/server/management/initDB.sql +++ b/src/server/management/initDB.sql @@ -1,9 +1,9 @@ -- WARNING: Will delete EVERYTHING in the database! -DROP TABLE IF EXISTS players; DROP TABLE IF EXISTS matches; DROP TABLE IF EXISTS teams; DROP TABLE IF EXISTS tournaments; +DROP TABLE IF EXISTS users; -- Create the tables CREATE TABLE tournaments ( @@ -38,12 +38,12 @@ CREATE TABLE matches ( FOREIGN KEY (winnerId) REFERENCES teams (id) ON DELETE SET NULL ); -CREATE TABLE players ( +CREATE TABLE users ( id INTEGER PRIMARY KEY AUTO_INCREMENT, - name TEXT NOT NULL, - teamId INTEGER NOT NULL, - - FOREIGN KEY (teamId) REFERENCES teams (id) ON DELETE CASCADE + googleId INTEGER, + name TEXT, + email TEXT NOT NULL, + isManager BOOLEAN NOT NULL ); -- Example data (Two tournaments, 4 teams, single elimination) @@ -81,30 +81,4 @@ INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, 5, 5, 6, 2); -- 7 INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, 5, 7, 8, 2); -- 8 INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, 6, 9, 10, 2); -- 9 -INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, 6, 11, 12, 2); -- 10 - --- Players -INSERT INTO players (name, teamId) VALUES ('Player 1', 1); -INSERT INTO players (name, teamId) VALUES ('Player 2', 1); -INSERT INTO players (name, teamId) VALUES ('Player 3', 2); -INSERT INTO players (name, teamId) VALUES ('Player 4', 2); -INSERT INTO players (name, teamId) VALUES ('Player 5', 3); -INSERT INTO players (name, teamId) VALUES ('Player 6', 3); -INSERT INTO players (name, teamId) VALUES ('Player 7', 4); -INSERT INTO players (name, teamId) VALUES ('Player 8', 4); -INSERT INTO players (name, teamId) VALUES ('Player 9', 5); -INSERT INTO players (name, teamId) VALUES ('Player 10', 5); -INSERT INTO players (name, teamId) VALUES ('Player 11', 6); -INSERT INTO players (name, teamId) VALUES ('Player 12', 6); -INSERT INTO players (name, teamId) VALUES ('Player 13', 7); -INSERT INTO players (name, teamId) VALUES ('Player 14', 7); -INSERT INTO players (name, teamId) VALUES ('Player 15', 8); -INSERT INTO players (name, teamId) VALUES ('Player 16', 8); -INSERT INTO players (name, teamId) VALUES ('Player 17', 9); -INSERT INTO players (name, teamId) VALUES ('Player 18', 9); -INSERT INTO players (name, teamId) VALUES ('Player 19', 10); -INSERT INTO players (name, teamId) VALUES ('Player 20', 10); -INSERT INTO players (name, teamId) VALUES ('Player 21', 11); -INSERT INTO players (name, teamId) VALUES ('Player 22', 11); -INSERT INTO players (name, teamId) VALUES ('Player 23', 12); -INSERT INTO players (name, teamId) VALUES ('Player 24', 12); +INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES (2, 6, 11, 12, 2); -- 10 \ No newline at end of file diff --git a/src/server/tmdb.js b/src/server/tmdb.js index d83cb34..9578bc0 100644 --- a/src/server/tmdb.js +++ b/src/server/tmdb.js @@ -15,10 +15,16 @@ module.exports = { deleteTournament: deleteTournament, editTournament: editTournament, getTeamsByTournamentId: getTeamsByTournamentId, + getUsers: getUsers, + getUserByEmail: getUserByEmail, + createUserBlank: createUserBlank, + editUser: editUser, } const mysql = require("mysql"); +// #region Database setup + let db_config = { host: process.env.DB_HOST, user: process.env.DB_USER, @@ -55,6 +61,8 @@ function escapeString(str) { return str; } +// #endregion + // #region match // Returns the match of the exact given id. function getMatch(matchId) { @@ -426,3 +434,98 @@ async function assignFirstMatch(teamId, tournamentId) { } // #endregion + + +// #region users + +function getUsers () { + return new Promise(function(resolve, reject) { + connection.query("SELECT * FROM users", (err, users) => { + if (err) { + console.log(err); + reject(err); + } else { + resolve(users); + } + }); + }); +} + +function getUserByGoogleId(googleId) { + return new Promise(function(resolve, reject) { + connection.query("SELECT * FROM users WHERE googleId = ?", [escapeString(googleId)], (err, users) => { + if (err) { + console.log(err); + reject(err); + } else { + if (users.length == 0) { + reject("No such user exists"); + } + resolve(users[0]); + } + }); + }); +} + +function getUserByEmail(email) { + return new Promise(function(resolve, reject) { + connection.query("SELECT * FROM users WHERE email = ?", [escapeString(email)], (err, users) => { + if (err) { + console.log(err); + reject(err); + } else { + if (users.length == 0) { + reject("No such user exists"); + return; + } + resolve(users[0]); + } + }); + }); +} + +function createUserBlank(email) { + return new Promise(function(resolve, reject) { + //Check that the user doesn't already exist + getUserByEmail(email).then(user => { + reject("No such user exists"); + }).catch(err => { + if (err != "No such user exists") { + console.log(err); + reject(err); + return; + } + // Create a user, with only an email address + connection.query("INSERT INTO users (email, isManager) VALUES (?), FALSE", [escapeString(email)], (err, sets) => { + if (err) { + console.log(err); + reject(err); + } else { + resolve({message: "User Created", userId: sets.insertId}); + } + }); + }); + }); +} + +function editUser(email, user) { + return new Promise(function(resolve, reject) { + connection.query("UPDATE users SET googleId = ?, name = ?, isManager = ? WHERE email = ?", [escapeString(user.googleId), escapeString(user.name), escapeString(user.isManager), escapeString(email)], (err, sets) => { + if (err) { + console.log(err); + reject(err); + } else { + console.log(sets); + resolve("User updated"); + } + }); + }); +} + +function userIsManager(userId) { + getUser(userId) + .then(user => { return user.isManager; }) + .catch(err => { console.log(err); return false; }); +} + +// #endregion \ No newline at end of file