我刚接触角js。我正在使用版本2进行角度项目。 我已按照https://www.npmjs.com/package/angular2-social-login使用angular2-social-login实施了google auth登录。 谷歌登录工作正常,但登出后点击登录按钮我仍然得到以前登录的用户详细信息和谷歌身份验证登录页面没有显示。 以下是我的实施细节。
安装 通过npm
npm install angular2-social-login --save
将angular2-social-login添加到您的项目中 在systemjs.config
中添加angular2-social-login的地图'angular2-social-login': 'node_modules/angular2-social-login/dist/bundles/angular2-social-login.min.js'
主模块配置
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { BrowserModule } from '@angular/platform-browser';
import { Angular2SocialLoginModule } from "angular2-social-login";
let providers = {
"google": {
"clientId": "GOOGLE_CLIENT_ID"
},
"linkedin": {
"clientId": "LINKEDIN_CLIENT_ID"
},
"facebook": {
"clientId": "FACEBOOK_CLIENT_ID",
"apiVersion": "<version>" //like v2.4
}
};
@NgModule({
imports: [
BrowserModule,
Angular2SocialLoginModule
],
declarations: [AppComponent],
bootstrap: [ AppComponent ]
})
export class AppModule {
constructor(){}
}
Angular2SocialLoginModule.loadProvidersScripts(providers);
login()和logout()的组件配置: 对于登录(提供者:字符串)提供者是必需的,它应该是任何人(区分大小写)&#34; facebook&#34;,&#34; google&#34;,&#34; linkedin&#34;
...
import { AuthService } from "angular2-social-login";
...
@Component({
...
})
export class AppComponent implements OnDestroy {
...
constructor(public _auth: AuthService){ }
signIn(provider){
this.sub = this._auth.login(provider).subscribe(
(data) => {
console.log(data);
//user data
//name, image, uid, provider, uid, email, token (accessToken for Facebook & google, no token for linkedIn), idToken(only for google)
}
)
}
logout(){
this._auth.logout().subscribe(
(data)=>{//return a boolean value.}
)
}
...
}
预感谢。
答案 0 :(得分:1)
我正在使用Angular 6,这是我退出Google帐户的代码:
在我的TS文件中,声明变量auth2,变量gapi和函数googleLogout:
declare const gapi: any;//this line should be at the very top of your TS file
public auth2: any;
public googleLogout() {
gapi.load('auth2', () => {
this.auth2 = gapi.auth2.getAuthInstance();
this.auth2.signOut().then(function() {
console.log("User signed out");
});
//this.attachSignout(document.getElementById('googleBtn2'));
});
}
然后,在html文件中,提供一个元素并使用click函数进行绑定:
<div id="googleBtn2" (click)="googleLogout()">Google SignOut</div>
别忘了在index.html中包含google api:
<script src="https://apis.google.com/js/platform.js" async defer></script>