【发布时间】:2018-11-22 00:42:47
【问题描述】:
我在 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>
我启动了应用程序,它运行良好。虽然我在控制台中收到以下错误:
应用程序运行良好,即单击按钮,会弹出一个谷歌登录屏幕,询问我的谷歌凭据。我输入它们并对其进行验证。
我有 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) 我们导出了 function getAuthServiceConfigs(),然后在 app.module.ts 的 Providers 数组中声明了它。那有什么用呢?
3) 我需要在应用程序启动时呈现谷歌登录屏幕,即不点击按钮。我如何做到这一点?
我尝试调用 ngOnInit() 中的方法:
ngOnInit(){
this.socialSignIn('google');
}
但屏幕显示为空白
请帮帮我!
【问题讨论】:
标签: angular google-authentication angular6