Stitch Auth: Is GoogleCredentials working in Browser SDK?

Hi there,
I’m still trying to solve this issue: Google OAuth Consent Screen shows mongodb.com.

I’ve noticed that Browser SDK has a GoogleCredentials that expect authCode as a parameter. I can get this authCode from Google API and then I can pass this credentials to loginWithCredentials.

auth2.grantOfflineAccess().then(async (response) => {
     let credential = new GoogleCredential(response.code)
     await this.$stitch.auth.loginWithCredential(credential) // fails with error exchanging access code with OAuth2 provider
})

Unfortunately, Stitch server always responds with “error exchanging access code with OAuth2 provider”.
I know that on browser we are supposed to use GoogleRedirectCredential but if I’m able to use GoogleCredentials this will solve the problem I mentioned above. Any idea why I get this error and if GoogleCredentials work at all with browser?

Thanks.

@Dimitar_Kurtev Can you check your Stitch Auth configuration for Google OAuth? We commonly see this error when the secret is misconfigured.

1 Like

Hi @Ian_Ward,
Thanks for your answer. Stitch Google OAuth works when I use GoogleRedirectCredentials. It does not work when I use GoogleCredentials (with the same Secret and Client id).
Maybe the way I get the AuthCode is not correct, but I could not find a tutorial for Browser SDK.
This is how I use gapi to get AuthCode:

<script src="https://apis.google.com/js/client:platform.js?onload=startGoogleLogIn" async defer></script> 
<script>                                                                                                  
  function startGoogleLogIn() {                                                                           
    gapi.load('auth2', function() {                                                                       
      auth2 = gapi.auth2.init({                                                                           
        client_id: 'myClientId',            
        ux_mode: 'redirect',                                                         
        scope: 'email profile'                                                                            
      });                                                                                                 
    });                                                                                                   
  }                                                                                                       
</script>     

/// later in code...

auth2.grantOfflineAccess().then(async (response) => {
    let credential = new GoogleCredential(response.code)
    await this.$stitch.auth.loginWithCredential(credential)
    /// ..redirect to home page
}).catch( ... )

Is this the correct way to get the AuthCode ?
Thanks.

I believe loginWithRedirect is required.
Docs -
https://docs.mongodb.com/stitch/authentication/google/#authenticate-a-user

SampleApp -