4

In my firebase (Angular) app, I'm using firebase authentication to log a user in via their Google Profile. As part of this process, the user gives me permission to access their gmail account (scope 'https://www.googleapis.com/auth/gmail.compose').

After the user has logged in this way, I want to configure the "gapi" google javascript SDK so that the "signed in user" is the user signed in via firebase auth. Here's where I'm having trouble.

  1. It appears that I need to set the client token for the gapi sdk like so gapi.client.setToken(userAccessToken) and the token needs to be set before the gapi client is initialized. Attempting to do this doesn't seem to work however (a call to gapi.auth2.getAuthInstance().isSignedIn.get() returns false when it should return true).

  2. I also can't figure out a way of changing the "signed in user" if the firebase user logs out and a new one logs in. This is because, again, the gapi client seems to require the gapi.client.setToken() be called before the client is initialized, and I can't see any way of re-initializing and already initialized gapi client.

I can get the gapi client working if I use the gapi client's own gapi.auth2.getAuthInstance().signIn() method, but then the user is asked to sign in to my app twice using (from the user's perspective) identical google login popup boxes (one prompt originating from firebase auth and the other from the gapi client).

Does anyone have any suggestions / tips? After someone logs in via firebase auth I can get access to their userAccessToken, I just can't figure out how to programmatically pass that to the gapi client in a clean way.

Ideally:

  • on application load the gapi client would also load and initialize.
  • When a user chose to sign in, I would be able to use Firebase Auth to log someone in via their google profile, then get their access token and pass it to the gapi client to make google api calls.
    • If the firebase user ever logged out, I would clear the gapi client's api token.
    • If a new firebase user logged in, I would re-set the gapi client's api token.
John
  • 9,249
  • 5
  • 44
  • 76
  • Did you make any progress with this? I have exactly the same problem. My app lets users log in via Firebase Auth. The app contains a calendar function, for which the gapi client is needed to retrieve the events and create events to a common Google calendar. Since the users are already logged in via Firebase, and the calendar is common through the app itself, I would like to pass on the access token to the gapi client. – El Fred Jan 06 '19 at 18:20
  • @ElFred I haven't figured out how to achieve my desired solution, but I did come up with something acceptable by following [this S.O. answer](https://stackoverflow.com/a/40108258/5490505). Basically, the GAPI client does not seem to let you pass it an access token, but the firebase auth client *does* let you pass it an access token. So instead of handling authentication with the firebase sdk and passing the token to the GAPI client, you need to do the reverse and handle authentication with the GAPI client and then pass the token to the firebase SDK. – John Jan 06 '19 at 18:29

1 Answers1

1

I have come upon a placeholder (i.e. non-ideal) solution to this problem by following this S.O. answer.

In short, the GAPI client does not seem to let you manually pass it an access token, but the firebase auth client does let you manually pass it an access token. So, instead of handling authentication with the firebase sdk and passing the token to the GAPI client, you need to do the reverse and handle authentication with the GAPI client and then pass the token to the firebase SDK.

John
  • 9,249
  • 5
  • 44
  • 76
  • Problem here is that I want to be able to use the application account's calendar (it has its own "user" account. If the user is logged in via the gapi client, then s/he will see their own calendar, and not the app/common one. – El Fred Jan 06 '19 at 19:19
  • @ElFred ya I don't have a better answer for you, unfortunately. Seems like what you describe would be a pretty common use case. Maybe you can duplicate the GAPI client and instantiate it twice with two separate access tokens? It seems like there simply has to be a way to use the GAPI client with multiple users at the same time... – John Jan 07 '19 at 01:43
  • 1
    The app has already been taken into use, and the calendar requirement was only recently added, hence the reason I want to stick to the Firebase auth as primary login. It indeed must be a very common use case, but it seems impossible to find any good information on this. I'll continue hunting and update this topic if I find anything interesting. Thanks for your time! – El Fred Jan 07 '19 at 11:14
  • Did you get any resolution for this? I think the only way to handle multiple third party auths is to do it on a server. planning to create a serverless function which will take care of the Oauth account linkings – Utkarsh Bhimte May 01 '20 at 10:15
  • 1
    Looking at this method of the gapi client it seems it should be possible to pass a firebase token to be used in gapi requests: https://github.com/google/google-api-javascript-client/blob/master/docs/reference.md#----gapiclientsettokentokenobject-- – ThdK Aug 31 '20 at 20:03