From d4e7d324109c555091925b86ec5a282e916f3e93 Mon Sep 17 00:00:00 2001 From: Daniloch Date: Fri, 31 May 2024 03:42:35 -0300 Subject: [PATCH 1/4] feat: add captcha to forgot password requests --- package.json | 3 +-- public/assets/css/forgot-password.css | 5 +++- public/assets/js/forgot-password.js | 24 ------------------ src/routes/account.js | 31 +++++++++++++++++++++--- src/util.js | 13 +++++++++- views/account/forgot-password.handlebars | 26 +++++++++++++++++--- 6 files changed, 67 insertions(+), 35 deletions(-) delete mode 100644 public/assets/js/forgot-password.js diff --git a/package.json b/package.json index 90c96f08..b36ff961 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,8 @@ "scripts": { "start": "node src/server.js", "build": "npm run browserify", - "browserify": "npm run browserify-miieditor && npm run browserify-forgot-password && npm run browserify-reset-password", + "browserify": "npm run browserify-miieditor && npm run browserify-reset-password", "browserify-miieditor": "browserify ./public/assets/js/miieditor.js -o ./public/assets/js/miieditor.bundled.js", - "browserify-forgot-password": "browserify ./public/assets/js/forgot-password.js -o ./public/assets/js/forgot-password.bundled.js", "browserify-reset-password": "browserify ./public/assets/js/reset-password.js -o ./public/assets/js/reset-password.bundled.js" }, "repository": { diff --git a/public/assets/css/forgot-password.css b/public/assets/css/forgot-password.css index 1a0572a5..04320257 100644 --- a/public/assets/css/forgot-password.css +++ b/public/assets/css/forgot-password.css @@ -100,6 +100,9 @@ form.account a.register { .banner-notice.success div { background: var(--green-shade-0); } +.banner-notice.error div { + background: var(--red-shade-1); +} form.account.register { display: grid; @@ -108,7 +111,7 @@ form.account.register { column-gap: 24px; margin-bottom: 48px; } -form.account.register div.h-captcha { +form.account.forgot-password div.h-captcha { grid-column: 1 / span 2; display: flex; justify-content: center; diff --git a/public/assets/js/forgot-password.js b/public/assets/js/forgot-password.js deleted file mode 100644 index 006177c1..00000000 --- a/public/assets/js/forgot-password.js +++ /dev/null @@ -1,24 +0,0 @@ -const input = document.querySelector('#input'); -document.querySelector('form').addEventListener('submit', function (event) { - event.preventDefault(); - - fetch('/account/forgot-password', { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - input: input.value - }) - }) - .then(response => response.json()) - .then(body => { - if (body.error) { - alert(`Error: ${body.error}. TODO: red error message thing`); - } else { - alert('If an account exists with the provided username/email address an email has been sent. TODO: reword this and green success'); - } - }) - .catch(console.log); -}); diff --git a/src/routes/account.js b/src/routes/account.js index a78a6a1c..78b992a2 100644 --- a/src/routes/account.js +++ b/src/routes/account.js @@ -152,12 +152,37 @@ router.get('/logout', async (_request, response) => { }); router.get('/forgot-password', async (request, response) => { - response.render('account/forgot-password'); + const renderData = { + input: request.cookies.input, + success_message: request.cookies.success_message, + error_message: request.cookies.error_message, + } + + response.clearCookie('input', { domain: '.pretendo.network' }); + + response.render('account/forgot-password', renderData); }); router.post('/forgot-password', async (request, response) => { - const apiResponse = await util.apiPostRequest('/v1/forgot-password', {}, request.body); - response.json(apiResponse.body); + const { input, 'h-captcha-response': hCaptchaResponse } = request.body; + + response.cookie('input', input, { domain: '.pretendo.network' }); + + try { + await util.forgotPassword({ + input, + hCaptchaResponse + }) + + response.clearCookie('input', { domain: '.pretendo.network' }); + + response.cookie('success_message', 'An email has been sent.', { domain: '.pretendo.network' }); + + response.redirect(request.redirect || '/account/forgot-password'); + } catch (error) { + response.cookie('error_message', error.message, { domain: '.pretendo.network' }); + return response.redirect('/account/forgot-password'); + } }); router.get('/reset-password', async (request, response) => { diff --git a/src/util.js b/src/util.js index f9d52abf..1c1d06cf 100644 --- a/src/util.js +++ b/src/util.js @@ -130,6 +130,16 @@ async function login(username, password) { return apiResponse.body; } +async function forgotPassword(forgotPasswordData) { + const apiResponse = await apiPostRequest('/v1/forgot-password', {}, forgotPasswordData); + + if (apiResponse.statusCode !== 200) { + throw new Error(apiResponse.body.error); + } + + return apiResponse.body; +} + async function refreshLogin(request, response) { const apiResponse = await apiPostRequest('/v1/login', {}, { refresh_token: request.cookies.refresh_token, @@ -260,7 +270,8 @@ module.exports = { apiPostRequest, apiDeleteRequest, register, - login, + login, + forgotPassword, refreshLogin, getUserAccountData, updateDiscordConnection, diff --git a/views/account/forgot-password.handlebars b/views/account/forgot-password.handlebars index 1a4f540e..6d778b32 100644 --- a/views/account/forgot-password.handlebars +++ b/views/account/forgot-password.handlebars @@ -1,17 +1,21 @@ - +{{#section 'head'}} + + +{{/section}} {{> header}}
- \ No newline at end of file +{{#if success_message}} + +{{/if}} + +{{#if error_message}} + +{{/if}} \ No newline at end of file From 5cc274d25092e30c3910ef6fad96451f963aaead Mon Sep 17 00:00:00 2001 From: Daniloch Date: Fri, 31 May 2024 11:40:35 -0300 Subject: [PATCH 2/4] fix: indentation issues --- src/routes/account.js | 2 +- src/util.js | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/routes/account.js b/src/routes/account.js index 78b992a2..89c5c225 100644 --- a/src/routes/account.js +++ b/src/routes/account.js @@ -165,7 +165,7 @@ router.get('/forgot-password', async (request, response) => { router.post('/forgot-password', async (request, response) => { const { input, 'h-captcha-response': hCaptchaResponse } = request.body; - + response.cookie('input', input, { domain: '.pretendo.network' }); try { diff --git a/src/util.js b/src/util.js index 1c1d06cf..152f21e4 100644 --- a/src/util.js +++ b/src/util.js @@ -262,24 +262,24 @@ function signDiscoursePayload(payload) { } module.exports = { - fullUrl, - getLocale, - getRawDocs, - parseDocs, - apiGetRequest, - apiPostRequest, - apiDeleteRequest, - register, + fullUrl, + getLocale, + getRawDocs, + parseDocs, + apiGetRequest, + apiPostRequest, + apiDeleteRequest, + register, login, forgotPassword, - refreshLogin, - getUserAccountData, - updateDiscordConnection, - removeDiscordConnection, - nintendoPasswordHash, - assignDiscordMemberSupporterRole, - assignDiscordMemberTesterRole, - removeDiscordMemberSupporterRole, - removeDiscordMemberTesterRole, - signDiscoursePayload + refreshLogin, + getUserAccountData, + updateDiscordConnection, + removeDiscordConnection, + nintendoPasswordHash, + assignDiscordMemberSupporterRole, + assignDiscordMemberTesterRole, + removeDiscordMemberSupporterRole, + removeDiscordMemberTesterRole, + signDiscoursePayload }; From 7901b6e10cf5952a262e0cc8eaa8b8dfad6d5217 Mon Sep 17 00:00:00 2001 From: Daniloch Date: Fri, 31 May 2024 14:08:33 -0300 Subject: [PATCH 3/4] fix: indentation problems (again) --- src/routes/account.js | 48 +++++++++++++++++++------------------- src/util.js | 54 +++++++++++++++++++++---------------------- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/routes/account.js b/src/routes/account.js index 89c5c225..3f8f65f1 100644 --- a/src/routes/account.js +++ b/src/routes/account.js @@ -152,37 +152,37 @@ router.get('/logout', async (_request, response) => { }); router.get('/forgot-password', async (request, response) => { - const renderData = { - input: request.cookies.input, - success_message: request.cookies.success_message, - error_message: request.cookies.error_message, - } - - response.clearCookie('input', { domain: '.pretendo.network' }); + const renderData = { + input: request.cookies.input, + success_message: request.cookies.success_message, + error_message: request.cookies.error_message, + } + + response.clearCookie('input', { domain: '.pretendo.network' }); response.render('account/forgot-password', renderData); }); router.post('/forgot-password', async (request, response) => { - const { input, 'h-captcha-response': hCaptchaResponse } = request.body; - - response.cookie('input', input, { domain: '.pretendo.network' }); - - try { - await util.forgotPassword({ - input, - hCaptchaResponse - }) + const { input, 'h-captcha-response': hCaptchaResponse } = request.body; - response.clearCookie('input', { domain: '.pretendo.network' }); + response.cookie('input', input, { domain: '.pretendo.network' }); - response.cookie('success_message', 'An email has been sent.', { domain: '.pretendo.network' }); - - response.redirect(request.redirect || '/account/forgot-password'); - } catch (error) { - response.cookie('error_message', error.message, { domain: '.pretendo.network' }); - return response.redirect('/account/forgot-password'); - } + try { + await util.forgotPassword({ + input, + hCaptchaResponse + }) + + response.clearCookie('input', { domain: '.pretendo.network' }); + + response.cookie('success_message', 'An email has been sent.', { domain: '.pretendo.network' }); + + response.redirect(request.redirect || '/account/forgot-password'); + } catch (error) { + response.cookie('error_message', error.message, { domain: '.pretendo.network' }); + return response.redirect('/account/forgot-password'); + } }); router.get('/reset-password', async (request, response) => { diff --git a/src/util.js b/src/util.js index 152f21e4..8db00209 100644 --- a/src/util.js +++ b/src/util.js @@ -131,13 +131,13 @@ async function login(username, password) { } async function forgotPassword(forgotPasswordData) { - const apiResponse = await apiPostRequest('/v1/forgot-password', {}, forgotPasswordData); - - if (apiResponse.statusCode !== 200) { - throw new Error(apiResponse.body.error); - } - - return apiResponse.body; + const apiResponse = await apiPostRequest('/v1/forgot-password', {}, forgotPasswordData); + + if (apiResponse.statusCode !== 200) { + throw new Error(apiResponse.body.error); + } + + return apiResponse.body; } async function refreshLogin(request, response) { @@ -262,24 +262,24 @@ function signDiscoursePayload(payload) { } module.exports = { - fullUrl, - getLocale, - getRawDocs, - parseDocs, - apiGetRequest, - apiPostRequest, - apiDeleteRequest, - register, - login, - forgotPassword, - refreshLogin, - getUserAccountData, - updateDiscordConnection, - removeDiscordConnection, - nintendoPasswordHash, - assignDiscordMemberSupporterRole, - assignDiscordMemberTesterRole, - removeDiscordMemberSupporterRole, - removeDiscordMemberTesterRole, - signDiscoursePayload + fullUrl, + getLocale, + getRawDocs, + parseDocs, + apiGetRequest, + apiPostRequest, + apiDeleteRequest, + register, + login, + forgotPassword, + refreshLogin, + getUserAccountData, + updateDiscordConnection, + removeDiscordConnection, + nintendoPasswordHash, + assignDiscordMemberSupporterRole, + assignDiscordMemberTesterRole, + removeDiscordMemberSupporterRole, + removeDiscordMemberTesterRole, + signDiscoursePayload }; From de5684812a892e2055cb42888734bcf9413f9768 Mon Sep 17 00:00:00 2001 From: Daniloch Date: Sun, 2 Jun 2024 06:38:15 -0300 Subject: [PATCH 4/4] fix: indentation issues --- src/routes/account.js | 32 ++++++++++++------------ src/util.js | 6 ++--- views/account/forgot-password.handlebars | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/routes/account.js b/src/routes/account.js index 3f8f65f1..bff830fa 100644 --- a/src/routes/account.js +++ b/src/routes/account.js @@ -153,11 +153,11 @@ router.get('/logout', async (_request, response) => { router.get('/forgot-password', async (request, response) => { const renderData = { - input: request.cookies.input, - success_message: request.cookies.success_message, - error_message: request.cookies.error_message, + input: request.cookies.input, + success_message: request.cookies.success_message, + error_message: request.cookies.error_message, } - + response.clearCookie('input', { domain: '.pretendo.network' }); response.render('account/forgot-password', renderData); @@ -169,19 +169,19 @@ router.post('/forgot-password', async (request, response) => { response.cookie('input', input, { domain: '.pretendo.network' }); try { - await util.forgotPassword({ - input, - hCaptchaResponse - }) - - response.clearCookie('input', { domain: '.pretendo.network' }); - - response.cookie('success_message', 'An email has been sent.', { domain: '.pretendo.network' }); - - response.redirect(request.redirect || '/account/forgot-password'); + await util.forgotPassword({ + input, + hCaptchaResponse + }) + + response.clearCookie('input', { domain: '.pretendo.network' }); + + response.cookie('success_message', 'An email has been sent.', { domain: '.pretendo.network' }); + + response.redirect(request.redirect || '/account/forgot-password'); } catch (error) { - response.cookie('error_message', error.message, { domain: '.pretendo.network' }); - return response.redirect('/account/forgot-password'); + response.cookie('error_message', error.message, { domain: '.pretendo.network' }); + return response.redirect('/account/forgot-password'); } }); diff --git a/src/util.js b/src/util.js index 8db00209..00593112 100644 --- a/src/util.js +++ b/src/util.js @@ -132,11 +132,11 @@ async function login(username, password) { async function forgotPassword(forgotPasswordData) { const apiResponse = await apiPostRequest('/v1/forgot-password', {}, forgotPasswordData); - + if (apiResponse.statusCode !== 200) { - throw new Error(apiResponse.body.error); + throw new Error(apiResponse.body.error); } - + return apiResponse.body; } diff --git a/views/account/forgot-password.handlebars b/views/account/forgot-password.handlebars index 6d778b32..101a6d75 100644 --- a/views/account/forgot-password.handlebars +++ b/views/account/forgot-password.handlebars @@ -15,7 +15,7 @@ -
+