【问题标题】:Google auth logout issue in angular 2Angular 2 中的 Google 身份验证注销问题
【发布时间】:2018-01-15 21:51:34
【问题描述】:

我是 Angular js 的新手。我正在开发一个版本 2 的 Angular 项目。 我已经按照https://www.npmjs.com/package/angular2-social-login 使用angular2-social-login 实现了google auth login。 谷歌登录工作正常,但是当我点击登录按钮退出后,我仍然得到以前登录的用户详细信息,并且没有显示谷歌身份验证登录页面。 这是我的实现细节。

安装 通过 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() 的组件配置: 对于 login(provider: string) 提供者是必需的,它应该是任何人(区分大小写)“facebook”、“google”、“linkedin”。

...
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.} 
    )
  }

  ...

}

提前谢谢。

【问题讨论】:

标签: angular


【解决方案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 文件中,给一个元素并绑定一个点击函数:

<div id="googleBtn2" (click)="googleLogout()">Google SignOut</div>

别忘了在 index.html 中包含 google api:

  <script src="https://apis.google.com/js/platform.js" async defer></script>

【讨论】:

    猜你喜欢
    • 2017-04-03
    • 1970-01-01
    • 2016-05-14
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 2022-07-10
    • 1970-01-01
    相关资源
    最近更新 更多