Skip to content

Commit

Permalink
Fixup token passthrough and remove leftover code
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed May 23, 2020
1 parent 966b63e commit d93d6fc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 94 deletions.
26 changes: 9 additions & 17 deletions routes/views/accounts/get/register.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
exports = module.exports = function(req, res) {
exports = module.exports = function (req, res) {

var locals = res.locals;
var locals = res.locals;

// locals.section is used to set the currently selected
// item in the header navigation.
locals.section = 'account';
// locals.section is used to set the currently selected
// item in the header navigation.
locals.section = 'account';

locals.formData = req.body || {};
locals.formData = req.body || {};

var flash = null;
var flash = null;

if (req.originalUrl == '/account_activated') {
flash = {};

flash.class = 'alert-success';
flash.messages = [{msg: 'You have activated your account successfully! Please try to login with the new account.'}];
flash.type = 'Success!';
}

// Render the view
res.render('account/register', {flash: flash});
// Render the view
res.render('account/register', {flash: flash});

};
26 changes: 9 additions & 17 deletions routes/views/accounts/get/requestPasswordReset.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
exports = module.exports = function(req, res) {
exports = module.exports = function (req, res) {

var locals = res.locals;
var locals = res.locals;

// locals.section is used to set the currently selected
// item in the header navigation.
locals.section = 'account';
// locals.section is used to set the currently selected
// item in the header navigation.
locals.section = 'account';

locals.formData = req.body || {};
locals.formData = req.body || {};

var flash = null;
var flash = null;

if (req.originalUrl == '/password_resetted') {
flash = {};

flash.class = 'alert-success';
flash.messages = [{msg: 'You have successfully reset your password! Please try to login with the new password.'}];
flash.type = 'Success!';
}

// Render the view
res.render('account/requestPasswordReset', {flash: flash});
// Render the view
res.render('account/requestPasswordReset', {flash: flash});

};
1 change: 1 addition & 0 deletions routes/views/accounts/post/activate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports = module.exports = function (req, res) {

let locals = res.locals;
locals.username = req.query.username
locals.token = req.query.token

locals.formData = req.body || {};

Expand Down
2 changes: 1 addition & 1 deletion routes/views/accounts/post/changeEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ exports = module.exports = function (req, res) {

// Successfully changed email
flash.class = 'alert-success';
flash.messages = [{msg: 'Your email was set successfully. Please use the new email to log in!'}];
flash.messages = [{msg: 'Your email was set successfully.'}];
flash.type = 'Success!';

overallRes.render('account/changeEmail', {flash: flash});
Expand Down
92 changes: 34 additions & 58 deletions routes/views/accounts/post/changeUsername.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,50 @@ let request = require('request');

exports = module.exports = function (req, res) {

let locals = res.locals;
let locals = res.locals;

locals.formData = req.body || {};
locals.formData = req.body || {};

// validate the input
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('username', 'Username must be three or more characters').isLength({min: 3});
// validate the input
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('username', 'Username must be three or more characters').isLength({min: 3});

// check the validation object for errors
let errors = req.validationErrors();
// check the validation object for errors
let errors = req.validationErrors();

//Must have client side errors to fix
if (errors) {
//Must have client side errors to fix
if (errors) {

// failure
flash.class = 'alert-danger';
flash.messages = errors;
flash.type = 'Error!';
// failure
flash.class = 'alert-danger';
flash.messages = errors;
flash.type = 'Error!';

res.render('account/changeUsername', {flash: flash});
res.render('account/changeUsername', {flash: flash});

} else {
} else {

let username = req.body.username;
let overallRes = res;
let username = req.body.username;
let overallRes = res;

//Run post to reset endpoint
request.post({
url: process.env.API_URL + '/users/changeUsername',
headers: {'Authorization': 'Bearer ' + req.user.data.attributes.token},
form: {newUsername: username}
}, function (err, res, body) {
//Run post to reset endpoint
request.post({
url: process.env.API_URL + '/users/changeUsername',
headers: {'Authorization': 'Bearer ' + req.user.data.attributes.token},
form: {newUsername: username}
}, function (err, res, body) {

let resp;
let errorMessages = [];
if (res.statusCode !== 200) {
error.parseApiErrors(body, flash);
return overallRes.render('account/changeUsername', {flash: flash});
}

if (res.statusCode !== 200) {
try {
resp = JSON.parse(body);
} catch (e) {
errorMessages.push({msg: 'Invalid change username. Please try again later.'});
flash.class = 'alert-danger';
flash.messages = errorMessages;
flash.type = 'Error!';
// Successfully changed username
flash.class = 'alert-success';
flash.messages = [{msg: 'Your username was changed successfully. Please use the new username to log in!'}];
flash.type = 'Success!';

return overallRes.render('account/changeUsername', {flash: flash});
}

// Failed changing username
for (let i = 0; i < resp.errors.length; i++) {
let error = resp.errors[i];

errorMessages.push({msg: error.detail});
}

flash.class = 'alert-danger';
flash.messages = errorMessages;
flash.type = 'Error!';

return overallRes.render('account/changeUsername', {flash: flash});
}

// Successfully changed username
flash.class = 'alert-success';
flash.messages = [{msg: 'Your username was changed successfully. Please use the new username to log in!'}];
flash.type = 'Success!';

overallRes.render('account/changeUsername', {flash: flash});
});
}
overallRes.render('account/changeUsername', {flash: flash});
});
}
};
3 changes: 2 additions & 1 deletion routes/views/accounts/post/confirmPasswordReset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ exports = module.exports = function (req, res) {

let locals = res.locals;
locals.username = req.query.username
locals.token = req.query.token

locals.formData = req.body || {};

Expand All @@ -21,7 +22,7 @@ exports = module.exports = function (req, res) {
if (errors) {
flash.class = 'alert-danger';
flash.messages = errors;
flash.type = 'Error!';
flash.type = 'Error!';

res.render('account/confirmPasswordReset', {flash: flash});
} else {
Expand Down

0 comments on commit d93d6fc

Please sign in to comment.