Viewing profiles works, profiles not yet created

This commit is contained in:
2019-05-10 13:11:07 +10:00
parent 84ce324365
commit 31a59f56ba
8 changed files with 300 additions and 8 deletions
+4
View File
@@ -24,6 +24,10 @@ app.post('/passwordchange', accounts.passwordChange(connection));
app.post('/passwordrecover', accounts.passwordRecover(connection));
app.post('/passwordreset', accounts.passwordReset(connection));
//handle profiles
let profiles = require('./profiles.js');
app.post('/profilerequest', profiles.profileRequest(connection));
//static directories
app.use('/styles', express.static(path.resolve(__dirname + '/../public/styles')) );
+44
View File
@@ -0,0 +1,44 @@
//environment variables
require('dotenv').config();
//libraries
let formidable = require('formidable');
function profileRequest(connection) {
return (req, res) => {
//formidable handles forms
let form = formidable.IncomingForm();
//parse form
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();
});
});
};
}
module.exports = {
profileRequest: profileRequest
}