How to implement SignIn with Google in Angular 2 using Typescript

前端 未结 7 977
梦毁少年i
梦毁少年i 2020-11-28 04:11

I have been trying to implement sign in with Google in angular 2 in a separate login component. I am unable to implement it with the documentation available in Google https:

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 04:41

    There is also another way to connect with google :

    Add theses lines in the index.html :

    
    
    

    and then here is a sample code to write on a component (or a service if you want) :

    import {Component} from "@angular/core";
    declare const gapi : any;
    
    
    @Component({ ... })
    export class ComponentClass {
       constructor() {
          gapi.load('auth2', function () {
             gapi.auth2.init()
          });
       }
    
       googleLogin() {
          let googleAuth = gapi.auth2.getAuthInstance();
          googleAuth.then(() => {
             googleAuth.signIn({scope: 'profile email'}).then(googleUser => {
                console.log(googleUser.getBasicProfile());
             });
          });
       }
    }
    

提交回复
热议问题