Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add captcha to Forgot Password requests #306

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
5 changes: 4 additions & 1 deletion public/assets/css/forgot-password.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
24 changes: 0 additions & 24 deletions public/assets/js/forgot-password.js

This file was deleted.

31 changes: 28 additions & 3 deletions src/routes/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,37 @@ router.get('/logout', async (_request, response) => {
});

router.get('/forgot-password', async (request, response) => {
response.render('account/forgot-password');
const renderData = {
DanilochTop marked this conversation as resolved.
Show resolved Hide resolved
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) => {
Expand Down
13 changes: 12 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -260,7 +270,8 @@ module.exports = {
apiPostRequest,
apiDeleteRequest,
register,
login,
login,
forgotPassword,
refreshLogin,
getUserAccountData,
updateDiscordConnection,
Expand Down
26 changes: 22 additions & 4 deletions views/account/forgot-password.handlebars
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
<link rel="stylesheet" href="/assets/css/forgot-password.css" />
{{#section 'head'}}
<script src="https://js.hcaptcha.com/1/api.js" async defer></script>
<link rel="stylesheet" href="/assets/css/forgot-password.css" />
{{/section}}

{{> header}}

<div class="wrapper">

<div class="account-form-wrapper">
<form method="post" class="account">
<form action="/account/forgot-password" method="post" class="account forgot-password">
<h2>{{ locale.account.forgotPassword.header }}</h2>
<p>{{ locale.account.forgotPassword.sub }}</p>
<div>
<label for="input">{{ locale.account.forgotPassword.input }}</label>
<input name="input" id="input" required>
<input name="input" id="input" value="{{ input }}" required>
</div>
<div class="h-captcha" data-sitekey="cf3fd74e-93ca-47e6-9fa0-5fc439de06d4"></div>
<div class="buttons">
<button type="submit">{{ locale.account.forgotPassword.submit }}</button>
</div>
</form>
</div>
</div>

<script src="/assets/js/forgot-password.bundled.js"></script>
{{#if success_message}}
<div class="banner-notice success">
<div>
<p>{{ success_message }}</p>
</div>
</div>
{{/if}}

{{#if error_message}}
<div class="banner-notice error">
<div>
<p>{{ error_message }}</p>
</div>
</div>
{{/if}}