Search code examples
iosswiftxcodefirebase-authenticationfbsdk

Log Out Facebook User When Authenticated With Firebase Auth


I have the following code that authenticates a user with Facebook and then with Firebase:

func authenticateWithFacebook() {
    FBSDKLoginManager().logIn(withReadPermissions: ["public_profile"], from: self) { (user, error) in // Signs up user with Facebook
        if let error = error {
            print(error.localizedDescription)
        } else if (user!.isCancelled) { // User cancels sign up
            print("User Cancelled")
        } else {
            self.authenticateFacebookUserWithFirebase(credential: FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString))
        }
    }
}

func authenticateFacebookUserWithFirebase(credential: AuthCredential) {
    Auth.auth().signInAndRetrieveData(with: credential) { (user, error) in
        if let error = error {
            print(error.localizedDescription)
        } else {
            print("Success")
        }
    }
}

This code works as expected. Once the user is authenticated with Firebase, what do I do with the Facebook user that has been "created" in the app? Do I need to keep track of the currentAccessToken and alert Firebase auth when the token expires? If not, do I just leave the code as is or should I log the Facebook user out of my app using the FBSDK? I just don't want a Facebook token floating around in my app.


Solution

  • The user is not logged in to Firebase with Facebook as such. Your app does not get the user's facebook email and password credentials in order to log them into your app's Firebase. Instead it gets the access token for that user and then that token is used to authenticate the user with Firebase. Therefore you cannot log out your user from Facebook but what you can do is invalidate the access token.