Co-Jay
Co-Jay

Reputation: 63

Can you validate whether a user has been recently authenticated on Firebase (Swift)?

Is there a way to check if a user has recently authenticated on Firebase to avoid this message when trying to delete a user: "This operation is sensitive and requires recent authentication. Log in again before retrying this request."

I have been playing around trying to compare lastSignInDate (below) to current time but there seems to be a large margin of error on this which can cause problems:

firebase.auth().currentUser.metadata.lastSignInTime

Are there any functions that can return a simple boolean as to whether a user has recently authenticated so the user.delete() function will work properly?

Thanks so much!

Upvotes: 2

Views: 752

Answers (1)

Pierre Janineh
Pierre Janineh

Reputation: 702

The best way to do this is by checking if the response has an error, like so:

let user = Auth.auth().currentUser
user.delete { error in
  if let error = error {
    if (error.code == "auth/requires-recent-login") {
      // The user's credentials are too old. Prompt Login screen.
    }
  } else {
    // ...
  }
}

According to Firebase Documentation, There's no other approach to this other than comparing the current date with firebase.auth().currentUser.metadata.lastSignInDate (Only if you have the Admin SDK on your app, but you most probably do not need that for enabling a user to delete themselves).

Upvotes: 3

Related Questions