Ad

How Do I Grab Credential Values For Re Authentication? (Question Updated)

- 1 answer

I am trying to allow users to change their email or password. I did some research on how re authentication work, most of the questions I had were answered however when creating the credential how do I grab the users email / password in the .credentialWithEmail(email, password: password) section? I'm not sure what to enter in those fields.

When I take a look at the quick help tab, it explains the Parameters:

email The user's email address. password The user's password.

Here is the code


   let credential = FIREmailPasswordAuthProvider.credentialWithEmail(email, password: password)

  func updateEmail() {



        guard let updatedEmail = self.updatedEmail else { return }

        let user = Auth.auth().currentUser
        guard let currentUid = Auth.auth().currentUser?.uid else { return }
        // re authenticate the user
        user?.reauthenticate(with: credential, completion: { (result, error) in
            if let error = error {
                // An error happened.
                print(error._code)
                self.handleError(error)
            } else {
                guard self.emailChanged == true else { return }

                user?.updateEmail(to: self.emailTextField.text!, completion: { (error) in
                                        if let error = error {
                                            self.handleError(error)
                                        } else {
                                            print("Email Change Success")
                                            USER_REF.child(currentUid).child("email").setValue(updatedEmail) { (err, ref) in

                                                 self.dismiss(animated: true, completion: nil)

            }
        }

    }

        )}



    }
    )}
Ad

Answer

If you are asking why you can't get the email or password after you set FIREmailPasswordAuthProvider, it is because it is set only. There is no function to retrieve email/password after you set FIREmailPasswordAuthProvider. Firebase does not have a way to retrieve set passwords from their Auth class. The user email can be retrieved with let userEmail = Auth.auth().currentUser?.email

You may need to redesign you code to allow the app to save passwords locally using NSUserDefaults or simply make the user reenter them.

Ad
source: stackoverflow.com
Ad