Search code examples
swiftrealmrealm-mobile-platform

Realm Sync Facebook Authentication not working


I am trying to enable Facebook authentication in Realm Sync, but keep getting an error on login.

I have been using these guides:

  1. https://docs.realm.io/sync/v/3.x/using-synced-realms/user-authentication/additional-providers#facebook

  2. https://docs.realm.io/sync/v/3.x/self-hosted/customize/authentication/included-third-party-auth-providers/facebook-authentication

I have the Access Token provided by the Facebook API/SDK enabling me to log in/sign up a user.

When I use Realm's libraries to log in a user with the Facebook Access Token, I get an error stating the 'provider' parameter is invalid, but this parameter is defined by Realm's own classes.

I have successfully authenticated a user with an email & password so do I need to set up something else on Facebook/Realm Sync? It seems Facebook authentication just doesn't work in Realm Sync and the above help files are pretty useless.

Authentication code

func authenticateWithFacebook(facebookToken: String, completion: @escaping (RealmAuthenticationResult) -> ()) {
    let credentials = SyncCredentials.facebook(token: facebookToken)

    print("------FACEBOOK LOGIN-------")
    print("Token: \(facebookToken)")

    login(credentials) { (result, userId) in
        completion(result)
    }
}

private func login(_ credentials: SyncCredentials, completion: @escaping (RealmAuthenticationResult, String?) -> ()) {

    SyncUser.logIn(with: credentials, server: RealmConnection.AUTH_URL, onCompletion: { (user, err) in
        if let _ = user {
            print("User has logged in/signed up")
            return completion(RealmAuthenticationResult.success(true), user?.identity)
        } else if let error = err {
            print(error.localizedDescription)
            return completion(RealmAuthenticationResult.failure(error), user?.identity)
        }
    })
}

The error

Error Domain=io.realm.sync.auth Code=601 "Your request parameters did not validate. provider: Invalid parameter 'provider'!;" UserInfo={NSLocalizedDescription=Your request parameters did not validate. provider: Invalid parameter 'provider'!;}

Other things I have tried

I have tried directly instantiating the base provider class 'RLMIdentityProvider' and creating SyncCredentials with that, but no dice.

A workaround is to get the account information from the Facebook API/SDK and use the account's email to login/signup with a username/password setup. However, it seems to make Facebook authentication redundant.


Solution

  • That Realm Documentation link is outdated. See the 3.16.0 Documentation (or later) as a lot was changed.

    At the moment Password, JWT & Firebase are the only auth options with Firebase Authentication being a very solid solution. Integrating Firebase is also covered in the Realm Documentation in the Using Sync'd Realms -> Authentication section. I won't link it as the documentation is being frequently updated now days.

    As stated by the Realm Team (several times) extensive auth options are not a priority as other companies (like Firebase) handle it very well.

    There were a number of posts on the Realm forums speaking to this but Ian's response to this question is very succinct.

    we have and will continue to prioritize synchronization features for mobile

    and then

    This is why we recommend that a production app should outsource user management and authentication to a company which specialized in these features.