How to check after login user is login through Facebook in firebase for ios swift?

Viewed 2324

My app is having facebook & email password login with firebase. Now for those user who are signed with Facebook, I don't want to verify their emails. but in Auth.auth().currentUser?.isEmailVerified its return false always. so is there any other method to detect user is logged in with facebook. I know i can store value inside user default before login but after uninstall & reinstall app i will lost that userdefault. while firebase keep user logged in. i can use keychain for that but if firebase directly provide that then that will make easy for coding.

4 Answers

I find one solution with firebase methods:

if let providerData = Auth.auth().currentUser?.providerData {
    for userInfo in providerData {
        switch userInfo.providerID {
        case "facebook.com":
            print("Facebook Login")
            //isVerifiededUser = true
        default:
            print("provider is \(userInfo.providerID)")
        }
    }
}

You can use:

    if let user = Auth.auth().currentUser {

       if FBSDKAccessToken.current() != nil {
           // logged in using facebook
       }
       else {
           // logged in using manual email/password method
       }
    }

so you can send verification emails to only those who are logged in using email/password method.

Firebase doesn't provide a method to show which was the first method used to create Firebase account. You will have a list of the all the providers attached to a firebase user with their emails/ phone number attached to them.

Firebase by default only set emails verified for Google SignIn, for other providers Firebase behavior is to set false (although some times it do set the email verified true randomly). The reason is that Firebase cannot guarantee that email is verified by facebook on their platform but incase of Google firebase has trust.

One option is you always send verification email no matter facebook or Email Auth. Second is that you ditch Email password login and instead use new Email Link Authentication, which eliminates the email auth needed in password login.

EDIT:

if you only allow one method to be used at a time then you can get the providers list from the firebase user and check if the providers list has 'password' signin method present, send a verification email after checking email verified, else don't send the email and continue the app

Here is the resource to email link authentication: https://firebase.google.com/docs/auth/ios/email-link-auth

This code means that at some point the user registered using Facebook, but it does not mean that at this point the user is using this provider for access. If the user has these providers ["google.com", "facebook.com"] it will also return true .

fileprivate func userHasFBProvider() -> Bool{
    
    var fBProvider = false
    
    guard let providerData = Auth.auth().currentUser?.providerData else {
        return fBProvider
    }
    
    for userInfo in providerData {
        switch userInfo.providerID {
        case "facebook.com":
            
            fBProvider = true
            
        default:

            fBProvider = false
            
        }
    }
    
    return fBProvider
}
Related