Firebase Auth - get provider ID

2020-02-26 05:01发布

I'm using the following code, to detect auth provider and log out properly

static func logOut() {
    let auth = FIRAuth.auth()!
    let provider = auth.currentUser?.providerID
    switch provider! {
        case "Facebook": FBSDKLoginManager().logOut()
        case "Google": GIDSignIn.sharedInstance().signOut()
        case "Twitter": Twitter.sharedInstance().sessionStore.logOutUserID(TWTRAPIClient.withCurrentUser().userID!)
        default:
            print("Unknown provider ID: \(provider!)")
            return
    }
    try! auth.signOut()
}

But the provider is always "Firebase". What am I doing wrong? 0_o Once that code throw "Facebook" when I was logged in twitter. Thanks in advance

UPD: Yeah, I actually can store auth provider in UserDefaults, but maybe it's Firebase bug. I'm using Firebase SDK 3.5.2

4条回答
趁早两清
2楼-- · 2020-02-26 05:36

To logout there is a simpler method:

let authUI = FUIAuth.defaultAuthUI()
do {
    try authUI?.signOut()
} catch let err {
    print(err);
}

On the other hand, if you want to find the provider AND determine if the user is logged in via that provider, check the accessToken. To get the accessToken you need the specific provider instance you provided to providers.

I find this is best achieved by first declaring your providers in your class this way:

lazy var facebookProvider = FUIFacebookAuth()
lazy var googleProvider = FUIGoogleAuth()

Then when you provide the providers:

let providers: [FUIAuthProvider] = [ facebookProvider, googleProvider ]

When you want the specific provider data:

if let providerData = Auth.auth().currentUser?.providerData {
    for userInfo in providerData {
        switch userInfo.providerID {
        case "facebook.com":
            if !facebookProvider.accessToken.isEmpty {
                print("user is signed in with facebook")
            }
        case "google.com":
            if !googleProvider.accessToken.isEmpty {
                 print("user is signed in with google")
            }
        default:
            print("user is signed in with \(userInfo.providerID)")
        }
    }
}

Otherwise you will get info on each provider regardless of whether the user is actually logged in.

查看更多
再贱就再见
3楼-- · 2020-02-26 05:46

I had to JSON.stringify(currentUser.providerData) in order to see how it's organized:

Stringify result

And i finally found the Auth Provider like this:

currentUser.providerData[0].providerId

Cheers, gl with your code : )

查看更多
闹够了就滚
4楼-- · 2020-02-26 05:50

Since a user can sign into their Firebase Authentication account with multiple providers, the top-level provider ID will now (usually) be Firebase.

But the currentUser has a providerData property that provides information on the speciic providers. Looping over FIRAuth.auth()!.currentUser.providerData will give you the FIRUserInfo.providerID you're looking for.

See also this question about UIDs, which are in a similar situation: Firebase returns multiple IDs, Which is unique one?

查看更多
\"骚年 ilove
5楼-- · 2020-02-26 05:50

Swift 4 solution:

   if let providerData = Auth.auth().currentUser?.providerData {
        for userInfo in providerData {
            switch userInfo.providerID {
            case "facebook.com":
                print("user is signed in with facebook")
            case "google.com":
                print("user is signed in with google")
            default:
                print("user is signed in with \(userInfo.providerID)")
            }
        }
    }
查看更多
登录 后发表回答