3
var user = firebase.auth().currentUser;
var credential;
// Prompt the user to re-provide their sign-in credentials
user.reauthenticate(credential).then(function() {

With the v3 Firebase client, how should I create this credential object for Google auth provider (Not email & Password).

How to do it with email and password was answered here: Email&Password.

I've tried var credential = firebase.auth.GoogleAuthProvider.credential(); but according to the docs it's needs an "Google Id Token" which I have no idea how to get.

Community
  • 1
  • 1
  • open https://console.firebase.google.com -> goto "add firebase webapp" copy paste snippet. there should be you token – Paulquappe Apr 16 '17 at 11:52
  • I think you may have misunderstood. The config you copy and paste is to initialise firebase. `firebase.initializeApp()`. I'm looking for what I need to pass to `firebase.auth.GoogleAuthProvider.credential()` so I can re-authenticate a loggedin user. –  Apr 16 '17 at 12:04
  • How did you sign in that user to begin with? Were you using signInWithPopup/Redirect? – bojeil Apr 17 '17 at 03:08
  • signInWithPopup –  Apr 17 '17 at 12:05
  • 4
    Possible duplicate of [How to create "credential" object needed by Firebase web user.reauthenticate() method?](https://stackoverflow.com/questions/37811684/how-to-create-credential-object-needed-by-firebase-web-user-reauthenticate-m) – nicokant May 27 '17 at 22:38

2 Answers2

2

You can create an AuthCredential by using firebase.auth.EmailAuthProvider.credential.

Then you can reauthenticate with firebase.User.reauthenticateWithCredential.

var user = firebase.auth().currentUser;
var credential = firebase.auth.EmailAuthProvider.credential(
  user.email,
  'yourpassword'
);
user.reauthenticateWithCredential(credential);
tim-phillips
  • 997
  • 1
  • 11
  • 22
1

You can reauthenticate users that use GooglaAuth Provider without tokens and credentials using reauthenticateWithPopup(provider) or reauthenticateWithRedirect(provider) method:

// Create provider as usual
function createGoogleProvider() {
  const provider = new auth.GoogleAuthProvider()
  provider.addScope('profile')
  provider.addScope('email')
  return provider
}
// Reauthenticate with popup:
user.reauthenticateWithPopup(createGoogleProvider()).then(function(result) {
  // The firebase.User instance:
  var user = result.user;
}, function(error) {
  // An error happened.
});
Kseniia
  • 179
  • 1
  • 1
  • 9