Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import { Subject } from 'rxjs';
  3.  
  4. declare let gapi: any;
  5. @Injectable({
  6.   providedIn: 'root'
  7. })
  8.  
  9. export class GoogleServiceService {
  10.  
  11.   constructor() {
  12.     gapi.load('client:auth2', () => this.initClient());
  13.   }
  14.  
  15.   isSignedInSubject: Subject<any> = new Subject<any>();
  16.   isSignedIn = this.isSignedInSubject.asObservable();
  17.  
  18.   API_KEY = "AIzaSyDT8J0B7L4paTekOAQlergeVHzpxyeflrs";
  19.   CLIENT_ID = "888009890047-g411qo3g52g3c5ackk5oiikses3sb4kl.apps.googleusercontent.com";
  20.   DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/tasks/v1/rest"];
  21.   SCOPES = "https://www.googleapis.com/auth/tasks.readonly";
  22.  
  23.  
  24.   Authenticate()
  25.   {
  26.     gapi.auth2.getAuthInstance().signIn();
  27.     console.log("Authentication handled")
  28.   }
  29.  
  30.   Logout()
  31.   {
  32.     gapi.auth2.getAuthInstance().signOut();
  33.     console.log("logout handled");
  34.   }
  35.  
  36.   initClient() {
  37.     gapi.client.init({
  38.       apiKey: this.API_KEY,
  39.       clientId: this.CLIENT_ID,
  40.       discoveryDocs: this.DISCOVERY_DOCS,
  41.       scope: this.SCOPES
  42.     }).then(function () {
  43.       gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigininStatus);
  44.       updateSigininStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
  45.     });
  46.     console.log("Client initiated");    
  47.   }
  48.  
  49.     updateSigininStatus(isSignedIn): void {
  50.       if (isSignedIn) {
  51.         console.log("signed in");
  52.       } else {
  53.         console.log("not signed in");
  54.       }
  55.     this.isSignedIn = isSignedIn;
  56.   }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement