baumblatt / capacitor-firebase-auth

Capacitor Firebase Authentication Plugin
MIT License
261 stars 129 forks source link

Reauthenticate users for security-critical actions #156

Open mauriceackel opened 3 years ago

mauriceackel commented 3 years ago

As described in the firebase docs, some actions require the user to be re-authenticated if the sign-in date is too old.

Is this library capable of offering this functionality? If not, are there plans to add this?

mauriceackel commented 3 years ago

For everyone who is interested, I solved this in the following way:

const { CapacitorFirebaseAuth } = Capacitor.Plugins;

const result = (await CapacitorFirebaseAuth?.signIn({
  providerId: authProvider,
})) as AppleSignInResult;

const credential = new firebase.auth.OAuthProvider(
  'apple.com',
).credential(result.idToken);

await user.reauthenticateWithCredential(credential);

This has to be adapted for other auth providers like google by changing the way the credential is set.

It would be really nice to have this as a native function though 🙃🚀

farcondee commented 3 years ago

@mauriceackel This is a life saver. But I can't for the life of me figure out how to modify it to work with provider Google. the GoogleAuthProvider() doesn't have .credential method... Any ideas?

mauriceackel commented 3 years ago

Hi @farcondee and nice to hear that this is helping you out! I got this working for google auth with the following code:

const result = (await CapacitorFirebaseAuth?.signIn({
  providerId: authProvider,
})) as GoogleSignInResult;

const credential = firebase.auth.GoogleAuthProvider.credential(
  result.idToken,
);

await user.reauthenticateWithCredential(credential);

Hope this helps.

P.S.: I am using firebase ^8.4.2

farcondee commented 3 years ago

@mauriceackel Thanks for the snippet. I noticed the issue I have is Xcode is throwing this error on the CapacitorFirebaseAuth.signIn step:

{"code":"auth/popup-blocked","message":"Unable to establish a connection with the popup. It may have been blocked by the browser."}

mauriceackel commented 3 years ago

Sorry I can't help with that. For me it is working as shown