Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: How to convert an anonymous account to a permanent google account

I've been trying to convert an anonymous account to a permanent google account following the instructions on the firebase docs (https://firebase.google.com/docs/auth/web/anonymous-auth#google-sign-in)

It shows the following snippet of code:

import { GoogleAuthProvider } from "firebase/auth";

const credential = GoogleAuthProvider.credential(googleUser.getAuthResponse().id_token);

However, the snippet of code provided displays a variable called "googleUser" which has the method googleUser.getAuthResponse().id_token

I do not know where this variable comes from and how to get it. I've looked around the docs and internet for hours and had no luck.

I'm currently using linkWithPopup to do the job, however, if the google user that I'm trying to link the anonymous account to already exists on the app it just fails.

like image 361
Heloi Neto Avatar asked Oct 15 '25 09:10

Heloi Neto


1 Answers

you should use the method linkWithPopup for that. here is an example:

import {
  getAuth,
  linkWithPopup,
  GoogleAuthProvider,
} from 'firebase/auth'

export function linkWithGoogleAccount() {
  var anonymousUser = getAuth().currentUser
  // Authenticate with a second method and get a credential
  var provider = new GoogleAuthProvider()
  if (anonUser)
    return linkWithPopup(anonymousUser, provider)
  else return null
}
like image 168
Kevin Colin Avatar answered Oct 16 '25 22:10

Kevin Colin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!