Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: recent login requested

I'm dealing with Firebase authentication for web. The documentation states that

Some security-sensitive actions—such as deleting an account, setting a primary email address, and changing a password—require that the user has recently signed in.

If not, the request would fail with error code auth/requires-recent-login and I should manage the case by prompting the user to re-insert her credentials. Once I have done that, I could easily re-authenticate the user with the following code:

firebase.auth().currentUser.reauthenticate(credential)

In the API reference there's some details more. It turns out credential is actually an object of type firebase.auth.AuthCredential. That being said, I still have a bunch of questions to which I couldn't find answer on the docs:

  1. How do I create the AuthCredential object?
  2. More importantly, how do I deal with providers (Google, Facebook, ...). I agree that changing email/password doesn't make sense for providers, because this is not the right place to change them, so re-authentication does not apply in this case. However, deleting a user is still an action requiring re-authentication, and this could be performed regardless of the authentication method. How do I re-authenticate a user that logged in with a provider?
  3. The documentation states that the user must have logged in recently. I couldn't find any definition of recent in the docs.
like image 561
Luca Poddigue Avatar asked Aug 22 '16 19:08

Luca Poddigue


People also ask

How do you check if a user is already signed in Firebase?

var user = firebase. auth(). currentUser; if (user) { // User is signed in. } else { // No user is signed in. }

How long is a Firebase user logged in for?

Firebase ID tokens are short lived and last for an hour; the refresh token can be used to retrieve new ID tokens. Refresh tokens expire only when one of the following occurs: The user is deleted. The user is disabled.

What does Firebase Login do?

You can use Firebase Authentication to allow users to sign in to your app using one or more sign-in methods, including email address and password sign-in, and federated identity providers such as Google Sign-in and Facebook Login.

What is get auth () in Firebase?

Firebase Auth is a service that allows your app to sign up and authenticate a user against multiple providers such as (Google, Facebook, Twitter, GitHub and more).


1 Answers

  1. You can initialize a credential by calling credential static method on any provider (include email/password provider):

firebase.auth.FacebookAuthProvider.credential(fbAccessToken);

  1. To reauthenticate an OAuth provider, you can call in a browser signInWithPopup or redirect. This will return an object with 2 fields: user and credential. You can use that credential directly. Here is a simplified example:

var tempApp = firebase.initializeApp(originalConfig, 'temp');
    var provider = new firebase.auth.FacebookAuthProvider();
    tempApp.signInWithPopup(provider).then(function(result)) {
    tempApp.auth().signOut();
      originalApp.auth().currentUser.reauthenticate(credential);
});
  1. That doesn't matter, as the firebase auth backend could change that. You shouldn't hard code this value. Instead try to catch that error and act appropriately when it happens.
like image 187
bojeil Avatar answered Oct 05 '22 22:10

bojeil