Google Auth linking with Anon account

So, I have issue with linking my anonymous user accounts with Google provider. My process is following:

// 1) Anon user is created
if (!client.auth.isLoggedIn) {
 const user = await this._auth.loginWithCredential(new AnonymousCredential())
}

// 2) When user decide to login, I try to link them like that
this._auth.user.linkUserWithRedirect(new GoogleRedirectCredential()).then(user => console.log("I AM BACK", user)).catch(console.error)
// ^ this fails with following error
//{
// error: "error exchanging access code with OAuth2 provider",
// error_code: "AuthError",
// link: "https://stitch.mongodb.com/groups/5d61344a9ccf6410569421c3/apps/5d617ecc552b569688b8acf8/logs?co_id=5ebe9bec77b63e267d604a01"
// }

// 3) I've found out on stack overflow that adding url to provider could solve that but I get CORS error if I try to do this
this._auth.user.linkUserWithRedirect(new GoogleRedirectCredential(window.location.href)).then(user => console.log("I AM BACK", user)).catch(console.error)
  1. I’ve also read that it may be because client IDs and secret IDs doesn’t match but I made sure 2 times that value here
    https://console.cloud.google.com/apis/credentials/oauthclient/XXX
    Are exactly the same as ones here
    App Services

Any idea what could still be wrong?

Hi Ondrej, I am currently trying to achieve the same. I am struggling as well.

Passing window.location.href to the provider already helped me a step further. However, after logging in to my google account I currently get redirected to https://eu-west-1.aws.stitch.mongodb.com/api/client/v2.0/auth/callback?.... with an error message:

{"error":"must start a new auth request"}

To avoid a CORS error I had to set “Allowed Request Origins” under “Settings” as mentioned here

I’ve actually gave up on that and moved to developing other features but your post gave me a little extra guidance on how to do it. Thanks for the CORS help.

1 Like