NetanelBasal
6/4/2017 - 5:59 PM

recaptcha6.directive.ts

export const RECAPTCHA_URL = new InjectionToken('RECAPTCHA_URL');

@Injectable()
class ReCaptchaAsyncValidator {

  constructor( private http : Http, @Inject(RECAPTCHA_URL) private url ) {
  }

  validateToken( token : string ) {
    return ( _ : AbstractControl ) => {
      return this.http.get(this.url, { params: { token } }).map(res => res.json()).map(res => {
        if( !res.success ) {
          return { tokenInvalid: true }
        }
        return null;
      });
    }
  }
}