Profiles are being created seamlessly
This commit is contained in:
+85
-22
@@ -4,6 +4,53 @@ require('dotenv').config();
|
|||||||
//libraries
|
//libraries
|
||||||
let formidable = require('formidable');
|
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) {
|
function profileRequest(connection) {
|
||||||
return (req, res) => {
|
return (req, res) => {
|
||||||
//formidable handles forms
|
//formidable handles forms
|
||||||
@@ -13,32 +60,48 @@ function profileRequest(connection) {
|
|||||||
form.parse(req, (err, fields) => {
|
form.parse(req, (err, fields) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
|
|
||||||
//TODO: do something with the id and token provided
|
//separate this section so it can be used elsewhere too
|
||||||
|
return profileRequestInner(connection, req, res, fields);
|
||||||
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();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 = {
|
module.exports = {
|
||||||
|
// profileCreate: profileCreate, //NOTE: Not actually used
|
||||||
profileRequest: profileRequest
|
profileRequest: profileRequest
|
||||||
}
|
}
|
||||||
@@ -45,7 +45,7 @@ CREATE TABLE IF NOT EXISTS profiles (
|
|||||||
|
|
||||||
accountId INTEGER UNSIGNED UNIQUE,
|
accountId INTEGER UNSIGNED UNIQUE,
|
||||||
|
|
||||||
gold INTEGER DEFAULT 0,
|
gold INTEGER DEFAULT 100,
|
||||||
recruits INTEGER DEFAULT 0,
|
recruits INTEGER DEFAULT 0,
|
||||||
soldiers INTEGER DEFAULT 0,
|
soldiers INTEGER DEFAULT 0,
|
||||||
spies INTEGER DEFAULT 0,
|
spies INTEGER DEFAULT 0,
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ class Profile extends React.Component {
|
|||||||
<div className='sidePanel'>
|
<div className='sidePanel'>
|
||||||
<p>Return <Link to='/'>home</Link></p>
|
<p>Return <Link to='/'>home</Link></p>
|
||||||
<PasswordChangePanel />
|
<PasswordChangePanel />
|
||||||
<Logout />
|
<Logout onClick={(e) => this.props.history.push('/')} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -174,7 +174,7 @@ class Profile extends React.Component {
|
|||||||
<div className='sidePanel'>
|
<div className='sidePanel'>
|
||||||
<p>Return <Link to='/'>home</Link></p>
|
<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>
|
<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>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
import { logout } from '../../actions/accounts.js';
|
import { logout } from '../../actions/accounts.js';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
|
||||||
class Logout extends React.Component {
|
class Logout extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
@@ -26,9 +27,20 @@ class Logout extends React.Component {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
this.props.logout();
|
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) {
|
function mapStoreToProps(store) {
|
||||||
return {
|
return {
|
||||||
email: store.account.email,
|
email: store.account.email,
|
||||||
|
|||||||
Reference in New Issue
Block a user