Angular 6 中的 Google 身份验证

问题描述 投票:0回答:2

我在 Angular 6 中创建了一个项目,它使用 Angular-6-social-login 展示了谷歌身份验证。以下是安装命令:

npm install --save angular-6-social-login

此后,我对 app.module.ts 文件进行了以下更改:

import {SocialLoginModule,AuthServiceConfig,GoogleLoginProvider} from "angular-6-social-login";


// Configs 
export function getAuthServiceConfigs() {
  let config = new AuthServiceConfig(
  [
    {
      id: GoogleLoginProvider.PROVIDER_ID,
      provider: new GoogleLoginProvider("Your-Google-Client-Id")
    }
  ];
 );
 return config;
}

@NgModule({
  imports: [
   ...
  SocialLoginModule
 ],
 providers: [
   ...
  {
  provide: AuthServiceConfig,
  useFactory: getAuthServiceConfigs
  }
 ],
 bootstrap: [...]
})


export class AppModule { }

以及 app.component.ts 中的以下更改:

import {Component, OnInit} from '@angular/core';
import {
 AuthService,
 GoogleLoginProvider
} from 'angular-6-social-login';

@Component({
  selector: 'app-signin',
  templateUrl: './signin.component.html',
  styleUrls: ['./signin.component.css']
})


export class SigninComponent implements OnInit {

constructor( private socialAuthService: AuthService ) {}

public socialSignIn(socialPlatform : string) {
  let socialPlatformProvider;
  if(socialPlatform == "facebook"){
      socialPlatformProvider = FacebookLoginProvider.PROVIDER_ID;
   }else if(socialPlatform == "google"){
     socialPlatformProvider = GoogleLoginProvider.PROVIDER_ID;
   }else if (socialPlatform == "linkedin") {
     socialPlatformProvider = LinkedinLoginProvider.PROVIDER_ID;
   }

this.socialAuthService.signIn(socialPlatformProvider).then(
  (userData) => {
    console.log(socialPlatform+" sign in data : " , userData);
    // Now sign-in with userData
    // ...

     }
   );
 }

 }

以下是我的app.component.html

<button (click)="socialSignIn('google')">Sign in with Google</button> 

我启动了该应用程序,它运行良好。虽然我在控制台中收到以下错误:

Error that the console displays when i start the application. Basically when i run ng serve --open and open the console window of the application

应用程序运行良好,只需单击按钮,就会弹出谷歌登录屏幕,询问我的谷歌凭据。我输入它们并进行验证。

我有 3 个问题或者更确切地说是疑问: 1)为什么会出现这个错误?

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'signIn' 
of undefined
TypeError: Cannot read property 'signIn' of undefined
at angular-6-social-login.umd.js:250
at new ZoneAwarePromise (zone.js:891)
at GoogleLoginProvider.push../node_modules/angular-6-social-login/angular-6- 
social-login.umd.js.GoogleLoginProvider.signIn (angular-6-social- 
login.umd.js:249)

2) 我们导出了 函数 getAuthServiceConfigs() 并随后在 app.module.ts 的 Providers 数组中声明了它。那有什么用?

3)我需要在应用程序启动时呈现谷歌登录屏幕,即无需单击按钮。我如何实现这一目标?

我尝试调用ngOnInit()中的方法:

ngOnInit(){
    this.socialSignIn('google');
}

但是屏幕显示为空白

请帮我解决这个问题!

angular google-authentication angular6
2个回答
0
投票

问题是这样的

ngOnInit(){
 this.socialSignIn('google');
}

socialAuthService
注入的
SigninComponent
ngOnInit()
上不可用。您可以在Constructor和ngOnInit之间的区别

看到更多内容

因为我在 app.component.html 上看到一个按钮。如果您尝试模拟鼠标单击,我建议使用适当的 Lifecycle Hooks

另外,删除

FacebookLoginProvider
中的
LinkedInLoginProvider
app.component.ts
,因为
app.module.ts

中没有导入且没有提及

0
投票

如果您为 app.module.ts 发布的代码确实是您要运行的代码,请注意

GoogleLoginProvider()
中的
getAuthServiceConfigs() {...new GoogleLoginProvider("Your-Google-Client-Id")...}
的构造函数希望您使用您的 Google 客户端 ID 填充它。

更具体地说,请注意,客户端 ID 类似于

YOUR_CLIENT_ID.apps.googleusercontent.com
,其中
YOUR_CLIENT_ID
是 Google API 控制台向您提供的一长串数字和数字。整个字符串
YOUR_CLIENT_ID.apps.googleusercontent.com
必须再次提供给
GoogleLoginProvider() constructor
....,并将
YOUR_CLIENT_ID
替换为来自 Google API 控制台的网站客户端 ID。

如果您需要从 Google 获取客户端 ID,可以点击以下链接并单击蓝色的配置项目按钮:https://developers.google.com/identity/sign-in/web/sign-in

我希望有帮助。 最美好的祝愿。

© www.soinside.com 2019 - 2024. All rights reserved.