Home > Software engineering >  How to update email and sensitive data properly in Firebase?
How to update email and sensitive data properly in Firebase?

Time:10-17

I have a code where the user can update his credentials/personal information however I encounter a problem and managed to fix it, it was saying first argument had to be an string and I found a solution however I got an error afterwards saying "This operation is sensitive and requires recent authentication. Log in again before retrying...

enter image description here

Afterwards I found in some of the comments where I found my enter image description here

Code

I get my auth from my firebase.js

const db = firebase.firestore();
const auth = firebase.auth();
const storage = firebase.storage();

I get my user from my App.js and then if I need it I just send it just like this:

function App() {

const [user, setUser] = useState([]);

  useEffect(() => {
    auth.onAuthStateChanged((authUser) => {

      if (authUser) {
        setUser(authUser);
      } else {
        setUser(false);
      }
    })
  }, [])

return (
....
<Route path = "/Update_Profile">
            <Inicio user={user}/>
            <UpdateProfile user={user}/>
          </Route>
...
)}

export default App;

const updateEmail = (e) => {
        e.preventDefault();
        
        if (user && (email != "")) {
            user.reauthenticateWithCredential(auth.EmailAuthProvider.credential(user.email, user.password))
            .then(() => user.updateEmail(email))
            
            const ref = db.collection("usuarios").doc(user.uid)
            ref.update({
                email: email
            })
        } else {
            //User is not logged in, handle that case here
        }
    }

CodePudding user response:

Because auth is an instance of the Auth module and not the namespace of the Auth module from the Firebase Web SDK (because you've used const auth = firebase.auth()), the following value is undefined:

auth.EmailAuthProvider

This is because the firebase.auth.Auth class does not define a property EmailAuthProvider. This then means JavaScript tries to call undefined.credential(...) and throws an error.

To access the EmailAuthProvider class, you need to access it from the firebase.auth namespace directly:

firebase.auth.EmailAuthProvider

In short,

firebase.auth !== firebase.auth()
  • Related