Updated Approach to Reauthenticate a User

Updated approach to Reauthenticate a user

let user = Auth.auth().currentUser
var credential: AuthCredential
user?.reauthenticateAndRetrieveData(with: credential, completion: {(authResult, error) in
if let error = error {
// An error happened.
}else{
// User re-authenticated.
}
})

You are getting error because you haven't initialize credential
You have to initialize AuthCredential first

These credential can vary from

EmailAuthCredential

FacebookAuthCredential

GithubAuthCredential

GoogleAuthCredential

PhoneAuthCredential

PlayGamesAuthCredential

TwitterAuthCredential

Initiziale the variable according to your authentication method e.g.
if you have chosen email you can use like

var credential: AuthCredential = EmailAuthProvider.credential(withEmail: "email", password: "pass")

Error reauthenticating user with Firebase / Xcode (for user email update)

I think you're missing the result parameter

let user = Auth.auth().currentUser
var credential: AuthCredential = EmailAuthProvider.credential(withEmail: "email", password: "pass")

user?.reauthenticate(with: credential) { result, error in
if let error = error {
// error handled here
} else {
// success
}
}

where result is an AuthDataResult that contains the result of a successful signin.

How to re-authenticate a user on Firebase with Google Provider?

I know this is old question but I did not found complete answer to this. This is how to do it on Android.

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
// Get the account
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(context);
if (acct != null) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Reauthenticated.");
}
}
});
}

Link Firebase user with another provider

You have to re-authenticate the user. Using the current credential

if 
let user = Auth.auth().currentUser,
let currentAccessToken = AccessToken.current
{
// Prompt the user to re-provide their sign-in credentials
let result = try await user.reauthenticate(
with: FacebookAuthProvider.credential(withAccessToken: currentAccessToken.tokenString)
)

// Then link the user
try await user.link(with: newCredential)
// User can be unlinked from Facebook
try await Auth.auth().currentUser?.unlink(fromProvider: "facebook.com")
}

This is needed for several operations such as updating the user's email, password or deleting the user.

Firebase reauthenticate social provider

You need to use the Facebook access token and not the Firebase ID token.
firebase.auth.currentUser.getToken(); returns the Firebase ID token.

You need to pass the facebook access token when initializing a facebook auth credential.

const credential = firebase.auth.FacebookAuthProvider.credential(facebookAccessToken);

In order to reauthenticate a social provider, you can the following:
Firebase: recent login requested

Building Credential object needed for Firebase Reauthentication

Assuming that this.app.auth is an instance of the firebase.auth.Auth class, EmailAuthProvider won't be present on that object, as it is not part of the prototype for the firebase.auth.Auth class.

EmailAuthProvider is instead part of the firebase.auth namespace which means it can only be accessed using firebase.auth.EmailAuthProvider (note how auth is not called as a function).

If using imports, you could also use

import { auth as FirebaseAuth } from 'firebase';

FirebaseAuth.EmailAuthProvider.credential(...)

Firebase auth - how to relogin the user?

Firebase automatically persists the user credentials, and restores them (asynchronously) when the app is restarted. You don't need to do anything for that. Just listen for auth state changes to get notified when the user info is restored, or (in rare cases) when it failed to restore.



Related Topics



Leave a reply



Submit