From 676cd455895380849b5d0e8809b7c67454ff6989 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sat, 11 May 2019 12:51:20 +1000 Subject: [PATCH] Profiles are being created seamlessly --- server/profiles.js | 107 ++++++++++++++++++++++++------ sql/create_database_structure.sql | 2 +- src/components/pages/profile.jsx | 4 +- src/components/panels/logout.jsx | 12 ++++ 4 files changed, 100 insertions(+), 25 deletions(-) diff --git a/server/profiles.js b/server/profiles.js index 68daf30..2fe104f 100644 --- a/server/profiles.js +++ b/server/profiles.js @@ -4,6 +4,53 @@ require('dotenv').config(); //libraries let formidable = require('formidable'); +function profileCreate(connection) { + return (req, res) => { + //formidable handles forms + let form = formidable.IncomingForm(); + + //parse form + form.parse(req, (err, fields) => { + if (err) throw err; + + //separate this section so it can be used elsewhere too + return profileCreateInner(connection, req, res, fields); + }); + }; +} + +function profileCreateInner(connection, req, res, fields) { + let query = 'SELECT accountId FROM profiles WHERE accountId IN (SELECT accounts.id FROM accounts WHERE username = ?);'; + connection.query(query, [fields.username], (err, results) => { + if (err) throw err; + + if (results.length === 1) { + res.status(400).write('That profile already exists'); + res.end(); + return; + } + + //check ID, username and token match + let query = 'SELECT accountId FROM sessions WHERE accountId IN (SELECT id FROM accounts WHERE username = ?) AND token = ?;'; + connection.query(query, [fields.username, fields.token], (err, results) => { + if (err) throw err; + + if (results.length !== 1 || results[0].accountId != fields.id) { + res.status(400).write('Invalid profile creation credentials'); + res.end(); + return; + } + + let query = 'INSERT INTO profiles (accountId) SELECT accounts.id FROM accounts WHERE username = ?;'; + connection.query(query, [fields.username], (err) => { + if (err) throw err; + + return profileRequestInner(connection, req, res, fields); + }); + }); + }); +} + function profileRequest(connection) { return (req, res) => { //formidable handles forms @@ -13,32 +60,48 @@ function profileRequest(connection) { form.parse(req, (err, fields) => { if (err) throw err; - //TODO: do something with the id and token provided - - let query = 'SELECT * FROM profiles WHERE accountId IN (SELECT accounts.id FROM accounts WHERE username = ?);'; - connection.query(query, [fields.username], (err, results) => { - if (err) throw err; - - if (results.length !== 1) { - res.status(400).write(`Failed to find that profile: ${fields.username}`); - res.end(); - return; - } - - res.status(200).json({ - username: fields.username, - gold: results[0].gold, - recruits: results[0].recruits, - soldiers: results[0].soldiers, - spies: results[0].spies, - scientists: results[0].scientists - }); - res.end(); - }); + //separate this section so it can be used elsewhere too + return profileRequestInner(connection, req, res, fields); }); }; } +function profileRequestInner(connection, req, res, fields) { + //TODO: do something with the id and token provided + + let query = 'SELECT * FROM profiles WHERE accountId IN (SELECT accounts.id FROM accounts WHERE username = ?);'; + connection.query(query, [fields.username], (err, results) => { + if (err) throw err; + + if (results.length !== 1) { + //pass it off to the profile creation process, IF the user is requesting their own profile + let query = 'SELECT id FROM accounts WHERE id = ? AND id IN (SELECT accountId FROM sessions WHERE token = ?);'; + connection.query(query, [fields.id, fields.token], (err, results) => { + if (err) throw err; + + if (results.length === 1) { + return profileCreateInner(connection, req, res, fields); + } else { + res.status(404).write('Profile not found'); + res.end(); + } + }); + } else { + //results.length === 1 + res.status(200).json({ + username: fields.username, + gold: results[0].gold, + recruits: results[0].recruits, + soldiers: results[0].soldiers, + spies: results[0].spies, + scientists: results[0].scientists + }); + res.end(); + } + }); +} + module.exports = { +// profileCreate: profileCreate, //NOTE: Not actually used profileRequest: profileRequest } \ No newline at end of file diff --git a/sql/create_database_structure.sql b/sql/create_database_structure.sql index c0996c8..2b9e3c6 100644 --- a/sql/create_database_structure.sql +++ b/sql/create_database_structure.sql @@ -45,7 +45,7 @@ CREATE TABLE IF NOT EXISTS profiles ( accountId INTEGER UNSIGNED UNIQUE, - gold INTEGER DEFAULT 0, + gold INTEGER DEFAULT 100, recruits INTEGER DEFAULT 0, soldiers INTEGER DEFAULT 0, spies INTEGER DEFAULT 0, diff --git a/src/components/pages/profile.jsx b/src/components/pages/profile.jsx index ae9431f..068196e 100644 --- a/src/components/pages/profile.jsx +++ b/src/components/pages/profile.jsx @@ -163,7 +163,7 @@ class Profile extends React.Component {

Return home

- + this.props.history.push('/')} />
); } @@ -174,7 +174,7 @@ class Profile extends React.Component {

Return home

Go to { e.preventDefault(); this.requestProfileData(this.props.username); this.props.history.push('/profile'); }}>your profile

- + this.props.history.push('/')} />
); } diff --git a/src/components/panels/logout.jsx b/src/components/panels/logout.jsx index 64e00fa..a848bc4 100644 --- a/src/components/panels/logout.jsx +++ b/src/components/panels/logout.jsx @@ -1,6 +1,7 @@ import React from 'react'; import { connect } from 'react-redux'; import { logout } from '../../actions/accounts.js'; +import PropTypes from 'prop-types'; class Logout extends React.Component { constructor(props) { @@ -26,9 +27,20 @@ class Logout extends React.Component { })); this.props.logout(); + + if (this.props.onClick) { + this.props.onClick(); + } } } +Logout.propTypes = { + email: PropTypes.string.isRequired, + token: PropTypes.number.isRequired, + logout: PropTypes.func.isRequired, + onClick: PropTypes.func +} + function mapStoreToProps(store) { return { email: store.account.email,