Skip to content
Snippets Groups Projects

Implement ArkoseLabs sign-in challenge

Merged Paul Gascou-Vaillancourt requested to merge arkose-labs-challenge into master
All threads resolved!
Compare and
6 files
+ 105
5
Compare changes
  • Side-by-side
  • Inline
Files
6
const CHALLENGE_TYPE = 'ML_defence';
export class ArkoseLabs {
constructor() {
this.publicKey = '9F5BDFCD-E895-43B5-8D96-B24E0107B685';
this.loginForm = document.querySelector('.js-login-form');
this.setConfig = this.setConfig.bind(this);
this.onReady = this.onReady.bind(this);
this.onError = this.onError.bind(this);
this.onCompleted = this.onCompleted.bind(this);
this.onSuppress = this.onSuppress.bind(this);
this.onFailed = this.onFailed.bind(this);
window.setupArkoseLabsEnforcement = this.setConfig;
this.attachEventListeners();
}
attachEventListeners() {
this.loginForm.addEventListener('submit', (e) => {
e.preventDefault();
this.addScriptTag();
});
}
addScriptTag() {
const tag = document.createElement('script');
[
['type', 'text/javascript'],
['src', `https://client-api.arkoselabs.com/v2/${this.publicKey}/api.js`],
['nonce', true],
['async', true],
['defer', true],
['data-callback', 'setupArkoseLabsEnforcement'],
].forEach(([attr, value]) => {
tag.setAttribute(attr, value);
});
document.head.appendChild(tag);
}
setConfig(enforcement) {
enforcement.setConfig({
data: { id: CHALLENGE_TYPE },
mode: 'inline',
selector: '.js-arkose-challenge',
onReady: this.onReady,
onError: this.onError,
onCompleted: this.onCompleted,
onSuppress: this.onSuppress,
onFailed: this.onFailed,
});
}
onReady() {
// Challenge loaded
}
onError() {
// Could not load the challenge
}
onCompleted() {
this.submitForm();
}
onSuppress() {
this.submitForm();
}
onFailed() {
// Callback function invoked when a challenge has failed (the user has failed the challenge
// multiple times and is not allowed to continue the session).
}
submitForm() {
this.loginForm.submit();
}
}
Loading