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
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)")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With