All files / app/components facebook-callback.component.ts

68% Statements 17/25
16.66% Branches 1/6
100% Functions 1/1
66.66% Lines 16/24

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                                                                                        1x 9x 9x 9x       4x 4x 4x 4x 4x 4x     4x 4x                 4x                 4x               4x       4x          
import { Component, OnInit, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Router, ActivatedRoute } from '@angular/router';
import { OAuthService } from '../services/oauth.service';
import { oauthConfig } from '../config/oauth.config';
 
@Component({
  selector: 'app-facebook-callback',
  standalone: true,
  imports: [CommonModule],
  template: `
    <div class="callback-container">
      <div class="callback-content">
        <div class="spinner"></div>
        <p>Connexion en cours...</p>
      </div>
    </div>
  `,
  styles: [`
    .callback-container {
      min-height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      background: var(--very-light-green);
    }
    .callback-content {
      text-align: center;
    }
    .spinner {
      border: 4px solid #f3f3f3;
      border-top: 4px solid var(--secondary-green);
      border-radius: 50%;
      width: 40px;
      height: 40px;
      animation: spin 1s linear infinite;
      margin: 0 auto 1rem;
    }
    @keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }
  `]
})
export class FacebookCallbackComponent implements OnInit {
  private router = inject(Router);
  private route = inject(ActivatedRoute);
  private oauthService = inject(OAuthService);
 
  ngOnInit() {
    // Récupérer le token depuis le fragment URL (#access_token=...)
    const fragment = window.location.hash.substring(1);
    const params = new URLSearchParams(fragment);
    const accessToken = params.get('access_token');
    const state = params.get('state');
    const error = params.get('error');
    const errorReason = params.get('error_reason');
 
    // Vérifier le state
    const storedState = sessionStorage.getItem('facebook_oauth_state');
    Iif (state !== storedState) {
      window.opener?.postMessage({
        type: 'facebook-oauth-error',
        error: 'État de sécurité invalide'
      }, window.location.origin);
      window.close();
      return;
    }
 
    Iif (error) {
      window.opener?.postMessage({
        type: 'facebook-oauth-error',
        error: errorReason || 'Erreur lors de la connexion Facebook'
      }, window.location.origin);
      window.close();
      return;
    }
 
    Iif (accessToken) {
      // Envoyer le token à la fenêtre parente
      window.opener?.postMessage({
        type: 'facebook-oauth-success',
        accessToken: accessToken
      }, window.location.origin);
      window.close();
    } else {
      window.opener?.postMessage({
        type: 'facebook-oauth-error',
        error: 'Token d\'accès non reçu'
      }, window.location.origin);
      window.close();
    }
  }
}