Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if a user has signed in to my app using Firebase with Facebook, email, or Google using Swift

I want to detect if a user has signed in using Facebook or email, etc...

I found an answer for Android, but I am programming in Swift for iOS and I am not sure how to translate the code entirely.

The android/java code is :

for (UserInfo user:FirebaseAuth.getInstance().getCurrentUser().getProviderData()) {
if (user.getProviderId().equals("facebook.com")) {
System.out.println("User is signed in with Facebook");
  }
}

I have tried to translate it, but I can't seem to figure out how to access the values. I keep getting a memory address instead.

Here is my swift code:

let authenticatedWith = FIRAuth.auth()?.currentUser?.providerData
like image 538
Taylor Simpson Avatar asked Dec 24 '22 02:12

Taylor Simpson


1 Answers

According to the docs, providerData is an array of FIRUserInfo structures.

The (mostly) equivalent Swift code for the Android code you posted looks like this:

if let providerData = FIRAuth.auth()?.currentUser?.providerData {
    for userInfo in providerData {
        switch userInfo.providerID {
        case "facebook.com":
            print("user is signed in with facebook")
        default:
            print("user is signed in with \(userInfo.providerID)")
    }
}

Note that the providerID property is also available directly on the FIRUser structure returned by the currentUser property, so you may be able to just do this:

if let providerID = FIRAuth.auth()?.currentUser?.providerID {
    switch providerID {
    default:
        print("user is signed in with \(providerID)")
    }
}
like image 197
par Avatar answered Dec 28 '22 08:12

par