Skip to content

Commit

Permalink
Merge pull request #19 from rivet-gg/feature/hub-165
Browse files Browse the repository at this point in the history
Don't show captcha if captcha not configured on backend
  • Loading branch information
NathanFlurry authored Nov 3, 2023
2 parents 14e357f + d35c0c8 commit 417f359
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 60 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"devDependencies": {
"@aws-sdk/abort-controller": "3.78.0",
"@aws-sdk/protocol-http": "3.78.0",
"@rivet-gg/api-internal": "0.0.1-a7a87dc",
"@rivet-gg/api-internal": "0.0.1-eb2accf",
"@rivet-gg/auth": "file:gen/api-auth",
"@rivet-gg/chat": "file:gen/api-chat",
"@rivet-gg/cloud": "file:gen/api-cloud",
Expand Down
37 changes: 13 additions & 24 deletions src/elements/overlay/register-panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default class RegisterPanel extends LitElement {
emailKeyPress(event: KeyboardEvent) {
// Enter is pressed
if (this.emailError == null && event.key == 'Enter') {
this.requestCaptcha();
this.startEmailVerificatoin();
this.emailInput.blur();
}
}
Expand All @@ -121,35 +121,24 @@ export default class RegisterPanel extends LitElement {
if (this.codeError == null && event.key == 'Enter') this.completeEmailVerification();
}

async requestCaptcha() {
return new Promise<void>(res => {
UIRoot.shared.openCaptcha(
async token => {
// Artificial wait time
await wait(timing.seconds(1));

UIRoot.shared.closeCaptcha();
this.startEmailVerification(token);
res();
},
err => {
this.loadError = err;
res();
}
);
});
}
async startEmailVerificatoin() {
// Wait for captcha
let captchaToken = null;
if (global.bootstrapData.captcha.turnstile) {
captchaToken = await UIRoot.shared.promptCaptcha();
}

async startEmailVerification(clientResponse: string) {
this.wait = true;
this.codeError = null;

try {
let res = await global.auth.startEmailVerification({
email: this.email.trim(),
captcha: {
turnstile: { clientResponse }
},
captcha: captchaToken
? {
turnstile: { clientResponse: captchaToken }
}
: null,
gameId: this.gameId
});

Expand Down Expand Up @@ -325,7 +314,7 @@ export default class RegisterPanel extends LitElement {
>`}
<stylized-button
?disabled=${this.emailError != null}
.trigger=${this.requestCaptcha.bind(this)}
.trigger=${this.startEmailVerificatoin.bind(this)}
>Continue</stylized-button
>
</div>
Expand Down
53 changes: 30 additions & 23 deletions src/elements/root/ui-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,31 +326,38 @@ export default class UIRoot extends LitElement {
document.getElementById('loading').classList.remove('hidden');
}

openCaptcha(cb: (token: string) => void, errCb?: (err: Error) => void) {
let element: HTMLElement = document.body.querySelector('#turnstile');
promptCaptcha(): Promise<string> {
return new Promise((resolve, reject) => {
let element: HTMLElement = document.body.querySelector('#turnstile');

if (element) {
element.style.removeProperty('display');
} else {
element = document.createElement('div');
element.setAttribute('id', 'turnstile');
element.addEventListener('click', e => {
if (e.target === e.currentTarget) this.closeCaptcha();
});

let cancel = new StylizedButton();
cancel.addEventListener('click', this.closeCaptcha.bind(this));
cancel.append(document.createTextNode('Cancel'));
element.append(cancel);

document.body.append(element);
}

if (element) {
element.style.removeProperty('display');
} else {
element = document.createElement('div');
element.setAttribute('id', 'turnstile');
element.addEventListener('click', e => {
if (e.target === e.currentTarget) this.closeCaptcha();
this.turnstileWidgetId = turnstile.render(element, {
sitekey: global.bootstrapData.captcha.turnstile.siteKey,
callback: (token: string) => {
this.closeCaptcha();
resolve(token);
},
'error-callback': err => {
this.closeCaptcha();
reject(err);
}
});

let cancel = new StylizedButton();
cancel.addEventListener('click', this.closeCaptcha.bind(this));
cancel.append(document.createTextNode('Cancel'));
element.append(cancel);

document.body.append(element);
}

console.log('site key', global.bootstrapData.captcha.turnstile.siteKey);
this.turnstileWidgetId = turnstile.render(element, {
sitekey: global.bootstrapData.captcha.turnstile.siteKey,
callback: cb,
'error-callback': errCb
});
}

Expand Down
24 changes: 12 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2088,10 +2088,10 @@
resolved "https://registry.npmjs.org/@protobufjs/utf8/-/utf8-1.1.0.tgz"
integrity sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==

"@rivet-gg/[email protected]a7a87dc":
version "0.0.1-a7a87dc"
resolved "https://registry.yarnpkg.com/@rivet-gg/api-internal/-/api-internal-0.0.1-a7a87dc.tgz#956dfa9c39ff83c5f6e4d017ad985b533154cb56"
integrity sha512-32jc66/uZUPaMGT24Z3Zo9ojp6F87D1rzscCoE2UfACHzf5x9CKJJ6FUq6155xDODzpB1oyDL9AotvSc5WqNbQ==
"@rivet-gg/[email protected]eb2accf":
version "0.0.1-eb2accf"
resolved "https://registry.yarnpkg.com/@rivet-gg/api-internal/-/api-internal-0.0.1-eb2accf.tgz#7614b499e587ad8bb79e07356ceb046170450acc"
integrity sha512-C6OBhnpvCsFwnkKZ/97HmS9FZlMtVExZPZZ6mYe7RLeUnCWg3fVEAKrbt0kdSIun1vH71QFJMfGDj0ae4ZAIZg==
dependencies:
"@types/url-join" "4.0.1"
axios "0.27.2"
Expand Down Expand Up @@ -2130,7 +2130,7 @@
"@aws-sdk/util-user-agent-node" "3.80.0"
"@aws-sdk/util-utf8-browser" "3.55.0"
"@aws-sdk/util-utf8-node" "3.55.0"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]7407d016-da26-417d-a0d7-b393e632eebc-1698799146792/node_modules/lib/rivet-sdk-js"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]11627fa7-952c-456a-bf9b-01ec08de7ddc-1698908021496/node_modules/lib/rivet-sdk-js"
tslib "^2.3.1"

"@rivet-gg/chat@file:gen/api-chat":
Expand Down Expand Up @@ -2165,7 +2165,7 @@
"@aws-sdk/util-user-agent-node" "3.80.0"
"@aws-sdk/util-utf8-browser" "3.55.0"
"@aws-sdk/util-utf8-node" "3.55.0"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]b4b887ee-6fa8-41af-aa48-b85804b616d2-1698799146832/node_modules/lib/rivet-sdk-js"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]5243a449-4820-47d8-940a-99f72d4b38f6-1698908021536/node_modules/lib/rivet-sdk-js"
tslib "^2.3.1"

"@rivet-gg/cloud@file:gen/api-cloud":
Expand Down Expand Up @@ -2200,7 +2200,7 @@
"@aws-sdk/util-user-agent-node" "3.80.0"
"@aws-sdk/util-utf8-browser" "3.55.0"
"@aws-sdk/util-utf8-node" "3.55.0"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]8600cc7b-c7d4-4b02-ab76-9829a9c555a7-1698799146838/node_modules/lib/rivet-sdk-js"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]4e9c7d5d-fcff-4df2-8c81-df67dd45abbd-1698908021543/node_modules/lib/rivet-sdk-js"
tslib "^2.3.1"

"@rivet-gg/common@file:lib/rivet-sdk-js":
Expand Down Expand Up @@ -2241,7 +2241,7 @@
"@aws-sdk/util-user-agent-node" "3.80.0"
"@aws-sdk/util-utf8-browser" "3.55.0"
"@aws-sdk/util-utf8-node" "3.55.0"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]91fd7678-0caa-47e9-8983-f3c99fa8fcde-1698799146846/node_modules/lib/rivet-sdk-js"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]1a9cde87-01b2-41d2-bad1-292bc1ad48cb-1698908021549/node_modules/lib/rivet-sdk-js"
tslib "^2.3.1"

"@rivet-gg/identity@file:gen/api-identity":
Expand Down Expand Up @@ -2276,7 +2276,7 @@
"@aws-sdk/util-user-agent-node" "3.80.0"
"@aws-sdk/util-utf8-browser" "3.55.0"
"@aws-sdk/util-utf8-node" "3.55.0"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]d9b1b1ae-717d-4d0b-b1ea-dc877b2324c8-1698799146853/node_modules/lib/rivet-sdk-js"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]5719a5c0-38d2-414e-b250-4422b1103421-1698908021554/node_modules/lib/rivet-sdk-js"
tslib "^2.3.1"

"@rivet-gg/kv@file:gen/api-kv":
Expand Down Expand Up @@ -2311,7 +2311,7 @@
"@aws-sdk/util-user-agent-node" "3.80.0"
"@aws-sdk/util-utf8-browser" "3.55.0"
"@aws-sdk/util-utf8-node" "3.55.0"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]ad3c0fcf-e176-457f-a6d5-a2cd753c6fa0-1698799146862/node_modules/lib/rivet-sdk-js"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]56a8af72-3234-441e-a212-1e4dfc23b995-1698908021560/node_modules/lib/rivet-sdk-js"
tslib "^2.3.1"

"@rivet-gg/party@file:gen/api-party":
Expand Down Expand Up @@ -2346,7 +2346,7 @@
"@aws-sdk/util-user-agent-node" "3.80.0"
"@aws-sdk/util-utf8-browser" "3.55.0"
"@aws-sdk/util-utf8-node" "3.55.0"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]36fe5a7b-71f8-466e-b80f-5123a64e3b3e-1698799146868/node_modules/lib/rivet-sdk-js"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]d6786f77-51ea-499d-a943-dc92a9084cfe-1698908021566/node_modules/lib/rivet-sdk-js"
tslib "^2.3.1"

"@rivet-gg/portal@file:gen/api-portal":
Expand Down Expand Up @@ -2381,7 +2381,7 @@
"@aws-sdk/util-user-agent-node" "3.80.0"
"@aws-sdk/util-utf8-browser" "3.55.0"
"@aws-sdk/util-utf8-node" "3.55.0"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]b87d0e40-dfb7-479e-a890-c6589577ecd1-1698799146878/node_modules/lib/rivet-sdk-js"
"@rivet-gg/common" "file:../../../usr/local/share/.cache/yarn/v6/[email protected]18bce561-e15b-48d7-b846-fb81ebeabfb3-1698908021572/node_modules/lib/rivet-sdk-js"
tslib "^2.3.1"

"@rollup/plugin-babel@^5.2.0":
Expand Down

0 comments on commit 417f359

Please sign in to comment.