|
| 1 | +const axios = require('axios') |
| 2 | +const appConfig = require('../../../../config/app') |
| 3 | +const { validationResult } = require('express-validator') |
1 | 4 | exports = module.exports = function (req, res) {
|
2 |
| - const locals = res.locals |
| 5 | + const errors = validationResult(req) |
3 | 6 |
|
4 |
| - // locals.section is used to set the currently selected |
5 |
| - // item in the header navigation. |
6 |
| - locals.section = 'account' |
| 7 | + // A render/redirect ignores this if not async and renders the confirm |
| 8 | + // Theres probably a better way to do this |
| 9 | + if (!errors.isEmpty()) { |
| 10 | + return renderRequestPasswordReset(req, res, errors) |
| 11 | + } |
7 | 12 |
|
8 |
| - locals.formData = req.body || {} |
| 13 | + res.render('account/confirmPasswordReset', { |
| 14 | + section: 'account', |
| 15 | + formData: req.body || {}, |
| 16 | + username: req.query.username, |
| 17 | + token: req.query.token |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +const renderRequestPasswordReset = async (req, res, errors) => { |
| 22 | + axios.post(appConfig.apiUrl + '/users/buildSteamPasswordResetUrl', {}, { maxRedirects: 0 }).then(response => { |
| 23 | + if (response.status !== 200) { |
| 24 | + throw new Error('java-api error') |
| 25 | + } |
9 | 26 |
|
10 |
| - const flash = null |
| 27 | + errors.errors[errors.errors.length - 1].msg += '. You may request a new link here' |
11 | 28 |
|
12 |
| - // Render the view |
13 |
| - locals.username = req.query.username |
14 |
| - locals.token = req.query.token |
15 |
| - res.render('account/confirmPasswordReset', { flash }) |
| 29 | + return res.render('account/requestPasswordReset', { |
| 30 | + section: 'account', |
| 31 | + errors: { |
| 32 | + class: 'alert-danger', |
| 33 | + messages: errors, |
| 34 | + type: 'Error!' |
| 35 | + }, |
| 36 | + steamReset: response.data.steamUrl, |
| 37 | + formData: {}, |
| 38 | + recaptchaSiteKey: appConfig.recaptchaKey |
| 39 | + }) |
| 40 | + }).catch(error => { |
| 41 | + console.error(error.toString()) |
| 42 | + return res.render('account/requestPasswordReset', { |
| 43 | + section: 'account', |
| 44 | + errors: { |
| 45 | + class: 'alert-danger', |
| 46 | + messages: error.toString(), |
| 47 | + type: 'Error!' |
| 48 | + }, |
| 49 | + formData: {}, |
| 50 | + recaptchaSiteKey: appConfig.recaptchaKey |
| 51 | + }) |
| 52 | + }) |
16 | 53 | }
|
0 commit comments