All files / app/services turnstile.service.ts

83.33% Statements 20/24
91.66% Branches 11/12
71.42% Functions 5/7
82.6% Lines 19/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109                        1x 12x           11x                             2x 1x     1x 1x                                               3x 1x     2x 1x   1x                 2x 1x     1x                 2x 1x     1x 1x                      
import { Injectable } from '@angular/core';
import { turnstileConfig } from '../config/recaptcha.config';
 
declare var turnstile: any;
 
/**
 * Service pour gérer Cloudflare Turnstile
 * Alternative moderne et gratuite à Google reCAPTCHA
 */
@Injectable({
  providedIn: 'root'
})
export class TurnstileService {
  private readonly SITE_KEY = turnstileConfig.siteKey;
 
  /**
   * Vérifie si Turnstile est chargé
   */
  isLoaded(): boolean {
    return typeof turnstile !== 'undefined' && turnstile !== null;
  }
 
  /**
   * Rendu du widget Turnstile
   * @param elementId ID de l'élément HTML où afficher le widget
   * @param callback Fonction appelée quand la vérification réussit
   * @param errorCallback Fonction appelée en cas d'erreur
   * @returns Widget ID
   */
  render(
    elementId: string,
    callback: (token: string) => void,
    errorCallback?: (error: any) => void
  ): string {
    if (!this.isLoaded()) {
      throw new Error('Turnstile n\'est pas chargé');
    }
 
    try {
      return turnstile.render(`#${elementId}`, {
        sitekey: this.SITE_KEY,
        callback: callback,
        'error-callback': errorCallback || (() => {}),
        'expired-callback': () => {
          // Le token a expiré
          Iif (errorCallback) {
            errorCallback({ message: 'La vérification a expiré' });
          }
        },
        theme: 'light',
        size: 'normal',
        language: 'fr'
      });
    } catch (error) {
      throw new Error(`Erreur lors du rendu de Turnstile: ${error}`);
    }
  }
 
  /**
   * Réinitialise le widget Turnstile
   * @param widgetId ID du widget (optionnel, réinitialise tous les widgets si non fourni)
   */
  reset(widgetId?: string): void {
    if (!this.isLoaded()) {
      return;
    }
 
    if (widgetId) {
      turnstile.reset(widgetId);
    } else {
      turnstile.reset();
    }
  }
 
  /**
   * Supprime le widget Turnstile
   * @param widgetId ID du widget
   */
  remove(widgetId: string): void {
    if (!this.isLoaded()) {
      return;
    }
 
    turnstile.remove(widgetId);
  }
 
  /**
   * Récupère la réponse du widget (token)
   * @param widgetId ID du widget (optionnel)
   * @returns Token de vérification
   */
  getResponse(widgetId?: string): string {
    if (!this.isLoaded()) {
      return '';
    }
 
    if (widgetId) {
      return turnstile.getResponse(widgetId);
    }
    return turnstile.getResponse();
  }
}