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.
It appears that I need to set the client token for the
gapi
sdk like sogapi.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 togapi.auth2.getAuthInstance().isSignedIn.get()
returnsfalse
when it should returntrue
).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 ofre-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.