Profiles are being created seamlessly

This commit is contained in:
2019-05-11 12:51:20 +10:00
parent b6aa2cfc6e
commit 676cd45589
4 changed files with 100 additions and 25 deletions
+69 -6
View File
@@ -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,6 +60,13 @@ function profileRequest(connection) {
form.parse(req, (err, fields) => {
if (err) throw err;
//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 = ?);';
@@ -20,11 +74,20 @@ function profileRequest(connection) {
if (err) throw err;
if (results.length !== 1) {
res.status(400).write(`Failed to find that profile: ${fields.username}`);
res.end();
return;
}
//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,
@@ -34,11 +97,11 @@ function profileRequest(connection) {
scientists: results[0].scientists
});
res.end();
}
});
});
};
}
module.exports = {
// profileCreate: profileCreate, //NOTE: Not actually used
profileRequest: profileRequest
}
+1 -1
View File
@@ -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,
+2 -2
View File
@@ -163,7 +163,7 @@ class Profile extends React.Component {
<div className='sidePanel'>
<p>Return <Link to='/'>home</Link></p>
<PasswordChangePanel />
<Logout />
<Logout onClick={(e) => this.props.history.push('/')} />
</div>
);
}
@@ -174,7 +174,7 @@ class Profile extends React.Component {
<div className='sidePanel'>
<p>Return <Link to='/'>home</Link></p>
<p>Go to <Link to='/profile' onClick={(e) => { e.preventDefault(); this.requestProfileData(this.props.username); this.props.history.push('/profile'); }}>your profile</Link></p>
<Logout />
<Logout onClick={(e) => this.props.history.push('/')} />
</div>
);
}
+12
View File
@@ -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,7 +27,18 @@ 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) {