Started login system
This commit is contained in:
parent
8641500414
commit
0ff1db6fb4
|
@ -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
|
|
@ -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)
|
||||
|
@ -82,29 +82,3 @@ INSERT INTO matches (tournamentId, parentMatchId, team1Id, team2Id, tier) VALUES
|
|||
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);
|
||||
|
|
|
@ -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
|
Loading…
Reference in New Issue