-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(component): improve reCAPTCHA v2 and v3 interoperability
If one wants to diplay both v2 and v3 inside the same app, then they should always inject `RECAPTCHA_V3_SITE_KEY`. closes #152
- Loading branch information
Showing
8 changed files
with
75 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
export { RecaptchaComponent } from './recaptcha/recaptcha.component'; | ||
export { | ||
RecaptchaLoaderService, | ||
RECAPTCHA_LANGUAGE, | ||
RECAPTCHA_BASE_URL, | ||
RECAPTCHA_NONCE, | ||
} from './recaptcha/recaptcha-loader.service'; | ||
export { RecaptchaModule } from './recaptcha/recaptcha.module'; | ||
export { RECAPTCHA_SETTINGS, RecaptchaSettings } from './recaptcha/recaptcha-settings'; | ||
export { RecaptchaSettings } from './recaptcha/recaptcha-settings'; | ||
|
||
export { RecaptchaV3Module } from './recaptcha/recaptcha-v3.module'; | ||
export { | ||
OnExecuteData, | ||
OnExecuteErrorData, | ||
ReCaptchaV3Service, | ||
RECAPTCHA_V3_SITE_KEY, | ||
} from './recaptcha/recaptcha-v3.service'; | ||
|
||
export { RecaptchaFormsModule } from './recaptcha/recaptcha-forms.module'; | ||
export { RecaptchaValueAccessorDirective } from './recaptcha/recaptcha-value-accessor.directive'; | ||
|
||
export { | ||
RECAPTCHA_LANGUAGE, | ||
RECAPTCHA_BASE_URL, | ||
RECAPTCHA_NONCE, | ||
RECAPTCHA_SETTINGS, | ||
RECAPTCHA_V3_SITE_KEY, | ||
} from './recaptcha/tokens'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
declare global { | ||
interface Window { | ||
ng2recaptchaloaded: () => void; | ||
} | ||
} | ||
|
||
export function loadScript( | ||
renderMode: 'explicit' | string, | ||
onLoaded: (grecaptcha: ReCaptchaV2.ReCaptcha) => void, | ||
urlParams: string, | ||
url?: string, | ||
nonce?: string, | ||
) { | ||
window.ng2recaptchaloaded = () => { | ||
onLoaded(grecaptcha); | ||
}; | ||
const script = document.createElement('script'); | ||
script.innerHTML = ''; | ||
const baseUrl = url || 'https://www.google.com/recaptcha/api.js'; | ||
|
||
script.src = `${baseUrl}?render=${renderMode}&onload=ng2recaptchaloaded${urlParams}`; | ||
if (nonce) { | ||
// tslint:disable-next-line:no-any Remove "any" cast once we upgrade Angular to 7 and TypeScript along with it | ||
(script as any).nonce = nonce; | ||
} | ||
script.async = true; | ||
script.defer = true; | ||
document.head.appendChild(script); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { | ||
InjectionToken, | ||
} from '@angular/core'; | ||
|
||
import { RecaptchaSettings } from './recaptcha-settings'; | ||
|
||
export const RECAPTCHA_LANGUAGE = new InjectionToken<string>('recaptcha-language'); | ||
export const RECAPTCHA_BASE_URL = new InjectionToken<string>('recaptcha-base-url'); | ||
export const RECAPTCHA_NONCE = new InjectionToken<string>('recaptcha-nonce-tag'); | ||
export const RECAPTCHA_SETTINGS = new InjectionToken<RecaptchaSettings>('recaptcha-settings'); | ||
export const RECAPTCHA_V3_SITE_KEY = new InjectionToken<string>('recaptcha-v3-site-key'); |