Skip to content

Commit

Permalink
feat(component): allow to specify a base url for loading recaptcha
Browse files Browse the repository at this point in the history
"google.com" domain might be unavailable in certain countries, so we're adding the ability to specify a different base url for the script.

Closes #101
  • Loading branch information
DethAriel committed Oct 24, 2018
1 parent 9395b82 commit df505fd
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -24,6 +24,7 @@ A simple, configurable, easy-to-start component for handling reCAPTCHA.
* [Working with invisible reCAPTCHA](#example-invisible)
* [Resizing](#example-resizing)
* [SystemJS configuration](#example-systemjs)
* [Loading from a different location](#example-different-url)

## <a name="installation"></a>Installation

Expand Down Expand Up @@ -339,3 +340,20 @@ To configure the package to work with SystemJS, you would configure it approxima
});
})();
```

### <a name="example-different-url"></a>Loading from a different location

Since `"google.com"` domain might be unavailable in some countries, reCAPTCHA core team has a solution for that - using `"recaptcha.net"` domain. You can configure the component to use that by providing the `RECAPTCHA_BASE_URL` token, for example:

```javascript
import { RECAPTCHA_BASE_URL } from 'ng-recaptcha';

@NgModule({
providers: [
{
provide: RECAPTCHA_BASE_URL,
useValue: 'https://recaptcha.net/recaptcha/api.js', // use recaptcha.net script source for some of our users
},
],
}) export class MyModule { }
```
2 changes: 1 addition & 1 deletion index.ts
@@ -1,4 +1,4 @@
export { RecaptchaComponent } from './recaptcha/recaptcha.component';
export { RecaptchaLoaderService, RECAPTCHA_LANGUAGE } from './recaptcha/recaptcha-loader.service';
export { RecaptchaLoaderService, RECAPTCHA_LANGUAGE, RECAPTCHA_BASE_URL } from './recaptcha/recaptcha-loader.service';
export { RecaptchaModule } from './recaptcha/recaptcha.module';
export { RECAPTCHA_SETTINGS, RecaptchaSettings } from './recaptcha/recaptcha-settings';
8 changes: 7 additions & 1 deletion recaptcha/recaptcha-loader.service.ts
Expand Up @@ -9,6 +9,7 @@ import {
import { BehaviorSubject, Observable, of } from 'rxjs';

export const RECAPTCHA_LANGUAGE = new InjectionToken<string>('recaptcha-language');
export const RECAPTCHA_BASE_URL = new InjectionToken<string>('recaptcha-base-url');

@Injectable()
export class RecaptchaLoaderService {
Expand All @@ -22,13 +23,17 @@ export class RecaptchaLoaderService {

/** @internal */
private language: string;
/** @internal */
private baseUrl: string;

constructor(
// tslint:disable-next-line:no-any
@Inject(PLATFORM_ID) private readonly platformId: any,
@Optional() @Inject(RECAPTCHA_LANGUAGE) language?: string,
@Optional() @Inject(RECAPTCHA_BASE_URL) baseUrl?: string,
) {
this.language = language;
this.baseUrl = baseUrl;
this.init();
this.ready = isPlatformBrowser(this.platformId) ? RecaptchaLoaderService.ready.asObservable() : of();
}
Expand All @@ -46,7 +51,8 @@ export class RecaptchaLoaderService {
const script = document.createElement('script') as HTMLScriptElement;
script.innerHTML = '';
const langParam = this.language ? '&hl=' + this.language : '';
script.src = `https://www.google.com/recaptcha/api.js?render=explicit&onload=ng2recaptchaloaded${langParam}`;
const baseUrl = this.baseUrl || 'https://www.google.com/recaptcha/api.js';
script.src = `${baseUrl}?render=explicit&onload=ng2recaptchaloaded${langParam}`;
script.async = true;
script.defer = true;
document.head.appendChild(script);
Expand Down

0 comments on commit df505fd

Please sign in to comment.