最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - google.accounts.id.initialize callback not firing - Stack Overflow

programmeradmin4浏览0评论

I'm trying to setup the ability for the Authenticated User to manage the OAuth accounts they have linked. Here's the code I'm using in my Angular Component:

linkGoogle(): void {
    debugger; // this pauses 1st
    const googleAuthButton = document.createElement('div');
    googleAuthButton.setAttribute('id', 'google-button');

    // Inject Google button dynamically
    document.body.appendChild(googleAuthButton);

    const client = google.accounts.id;
    client.initialize({
      client_id: environment.googleClientId,
      callback: (response: any) => {
        const idToken = response.credential;
        console.log('Google Sign-In callback triggered:', response);
        debugger; // this never pauses
        // Use the new CustomerService method
        this.customerService.linkGoogleAccount(idToken).subscribe({
          next: () => {
            this.snackBar.open('Google account linked successfully!', 'Close', {
              duration: 3000,
            });
            document.body.removeChild(googleAuthButton); // Cleanup
            this.authService.initializeAuth().subscribe(); // Refresh current user
          },
          error: (err) => {
            console.error('Error linking Google account:', err);
            this.snackBar.open('Failed to link Google account.', 'Close', {
              duration: 3000,
            });
          },
        });
      },
      auto_select: false,
      cancel_on_tap_outside: true,
    });
    console.log('Client initialized:', client);
    client.prompt();
  }

When I invoke this function I can see the debugger; statements pause but not the one that I have set inside the defined callback function.

I expect the callback method to be invoked so I can see the token and manage the linked account.

发布评论

评论列表(0)

  1. 暂无评论