Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handleAuthClick not returning any Promise #89

Open
noblessem opened this issue Jul 21, 2022 · 6 comments
Open

handleAuthClick not returning any Promise #89

noblessem opened this issue Jul 21, 2022 · 6 comments

Comments

@noblessem
Copy link

handleAuthClick doesnt return any promise, so it is imposible to use it with other methods in lib

@ghost
Copy link

ghost commented Dec 22, 2022

Hi @noblessem , had the same problem, give it a try:

let GTokenClient;
 //...

  useEffect(() => {
    apiCalendar.onLoad(() => {
      console.info('> GAPI loaded');
      GTokenClient = apiCalendar.tokenClient;
    });
  }, []);

  const handleAuthClick = () => {
    return new Promise((resolve, _) => {
      GTokenClient.callback = (resp: any) => {
        resolve(resp);
      };

      if (gapi && GTokenClient) {
        if (gapi.client.getToken() === null) {
          GTokenClient.requestAccessToken({ prompt: 'consent' });
        } else {
          GTokenClient.requestAccessToken({
            prompt: ''
          });
        }
      } else {
        console.error('gapi not loaded');
      }
    });
  };

@Kubessandra
Copy link
Owner

Hi,
Yes because requestAccessToken(the google method used in the package) does not return a Promise unfortunately.

If you want @carlos-molero you can make this change to the package, if this is working for you.

Screenshot 2022-12-22 at 18 14 49

@ghost
Copy link

ghost commented Dec 22, 2022

As soon as I have some time I will submit a PR. I’m gonna rely heavily on this for a production project so I will be keeping an eye on the repo 😊

@Kubessandra
Copy link
Owner

As soon as I have some time I will submit a PR. I’m gonna rely heavily on this for a production project so I will be keeping an eye on the repo 😊

I will not have the time in the next two weeks to do it, but I will be able to review and approve it 😇

@oscarcusin
Copy link
Contributor

oscarcusin commented Apr 30, 2023

Hi @Kubessandra, I have a working version which returns a promise that resolves if authentication is successful and rejects if unsuccessful:

  /**
   * Sign in Google user account
   * @returns {any} Promise resolved if authentication is successful, rejected if unsuccessful.
   */
  public handleAuthClick(): any {
    if (gapi && this.tokenClient) {
      return new Promise((resolve, reject) => {
        this.tokenClient!.callback = (resp: any) => {
          if (resp.access_token) {
            resolve(resp);
          } else {
            reject(resp);
          }
        }
        this.tokenClient!.error_callback = (resp: any) => {
          reject(resp);
        }
        if (gapi.client.getToken() === null) {
          this.tokenClient!.requestAccessToken({ prompt: "consent" });
        } else {
          this.tokenClient!.requestAccessToken({ prompt: "" });
        }
      })
    } else {
      console.error("Error: this.gapi not loaded")
      return Promise.reject(new Error("Error: this.gapi not loaded"))
    }
  }

In order for this to work I had to extend google.accounts.oauth2.TokenClient to include callback and error_callback as such:

interface ExtendedTokenClient extends google.accounts.oauth2.TokenClient {
  callback?: (resp: any) => void;
  error_callback?: (resp: any) => void;
}

And define tokenClient as of type ExtendedTokenClient | null = null;.

This works flawlessly for my project, and I will gladly submit a PR if this is of interest :).

@Kubessandra
Copy link
Owner

Hi,
Yes totally, lets submit a PR for this 🚀👌

Kubessandra pushed a commit that referenced this issue Aug 10, 2023
* handleAuthClick() now returns a promise

The promise resolves if authentication is successful, rejects otherwise.
Token client is extended to include callbacks.

RGCA #89

* Changed return type from any to Promise<void>
Changed return type from any to Promise<void> in handleAuthClick().
RGCA #89
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants