rubenCodeforges / ng-gapi

ng-gapi a Google api module for Angular 6+

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Access Token expires~

codemaster730 opened this issue · comments

Hi thanks for the great lib,
I was able to implement google authentication using this lib but Access tokens typically expire after 60 minutes.
Is there a way to refresh token so I can get a new (valid) access token?

Any help would be appreciated!
Thanks

Remember ng-gapi give you control over gapi ( Google API) in a angular environment.
So you can freely refer to gapi docs.
Hope that was helpful , if not feel free to ask.

@axiom88-guru аа Олена , если что пиши в телегу @codeforges , подскажу

Sorry for not responding quickly ..
But i still have a problem with your library.
When I tried to run this: gapi.auth2.getAuthInstance().isSignedIn.get()
I got an error:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'getAuthInstance' of undefined TypeError: Cannot read property 'getAuthInstance' of undefined

Not sure why i am getting this error as it was written in your lib.
Thanks

i need more context, can you show your code ?

That is all, i was able to signin with this code snippet
public signIn() { return new Promise((resolve, reject) => { this.googleAuthService.getAuth().subscribe((auth) => { auth.signIn().then(res => { this.signInSuccessHandler(res).then(_=> { resolve(); }) }, err => reject(this.signInErrorHandler(err))); }); }) }
After signin with google, I tried to check if users is logged in or not.
Is there any way to check this?

Hi according to google api docs gapi.auth2.getAuthInstance().isSignedIn.get() will return boolean

@axiom88-guru you need to run in side the callback :

gapiService.onLoad().subscribe(()=> {
          gapi.auth2.getAuthInstance().isSignedIn.get()
           
        });

or use GoogleAuthService:

constructor(authService: GoogleAuthService) {
   authService.getAuth().subscribe((auth) => {
     auth.getAuthInstance().isSignedIn.get()
  }) 
}

@axiom88-guru im not getting your point , a constructor is a method of a class that is executed when the class is instantiated.

In Angular we use constuctors as point of DependencyInjection.

So you can inject Injectable class instances in components , services etc.

@Injectable()
export class MySuperService {
   constructor(private authService: GoogleAuthService) {
   }

   public doSomething(): void {
      authService.getAuth().subscribe((auth) => {
       auth.getAuthInstance().isSignedIn.get()
    }) 
  ]
}

Or in component

 @Component({
  selector: 'my-super-cmp',
  template: '<button (click)="doSomething()"> click me</h2>'
})

export class MySuperComponent {
   constructor(private authService: GoogleAuthService) {
   }

  public doSomething(): void {
     authService.getAuth().subscribe((auth) => {
       auth.getAuthInstance().isSignedIn.get()
    }) 
  }
}

You can refer to Angular.io as a point of reference to learn more about DI https://angular.io/guide/dependency-injection

do you know auth.getAuthInstance().isSignedIn.get() doesn't work because getAuthInstance is not defined in GoogleAuthService.

right , my bad if you use AuthService.getAuth() you need this


authService.getAuth().subscribe((auth) => {
       auth.isSignedIn.get()
    }) 

Please check above screenshot. i just wrote a codesnippet in your demo sample . but it never worked out .
I was expecting a response but it retrieves nothing...
image

@axiom88-guru ive modified my demo for you https://stackblitz.com/edit/ng-gapi-example
works flawless

thanks. it worked like a charm 👍

you are welcome

somehow i still want to manage to refresh token without letting users signin again.
https://developers.google.com/api-client-library/javascript/help/faq#how-do-i-refresh-the-auth-token-and-how-often-should-i-do--------it
this is the doc you recommended but i was unable to figure out how to do that.
would you recommend a way to refresh token based on your ng-gapi lib?
Thanks

@axiom88-guru i think you are looking for a Angular solution , ng-gapi is just a wrapper for google api , so
this https://medium.com/@ryanchenkie_40935/angular-authentication-using-the-http-client-and-http-interceptors-2f9d1540eb8 should show you a approach for token refresh .
And in your case in that interceptor you can do what google docs says : Refresh the token by calling gapi.auth.authorize with the client ID, the scope and immediate:true as parameters.

and gapi you can get with ng-gapi injected using DI

@rubenCodeforges Thanks for opening this issue again. Btw, i don't think gapi.auth.authorize would work for refreshing token. i solved this problem by using reloadAuthResponse() of GoogleUser .
here is the codesnippet that allows refreshing tokens.

    public refreshToken() {
        return new Promise((resolve, reject) => {
            this.googleAuthService.getAuth().subscribe((auth) => {
                let currUser = auth.currentUser.get();
                currUser.reloadAuthResponse().then((resp) => {
                    resolve(resp.access_token);
                }, (err) => {
                    reject(err);
                });
            }, (err) => reject(err));
        })
    }

Thank you for your support so far.
I really appreciate it 👍