Clifton Labrum
Clifton Labrum

Reputation: 14168

Sync Authentication in Realm Swift Version 2.0.4

I upgraded to Realm 2.0.4 in my Swift project and now the authenticate method doesn't work. I can't create new users or sign in to my Realm sync server anymore.

What changed?

Upvotes: 2

Views: 672

Answers (1)

Clifton Labrum
Clifton Labrum

Reputation: 14168

There were some breaking changes in Realm Swift 2.0.4 and there is now a single SyncUser.logIn method to use. Whether you sign in or sign up is determined by the kind of SyncCredentials you pass in.

Here's an example using Swift 3.0.1:

//Create Account
let signUpCredentials = SyncCredentials.usernamePassword(username: "username", password: "password", register: true)

SyncUser.logIn(with: signUpCredentials, server: serverURL) { user, error in
  if user == nil {
    //Error
  }else{
    //Success
  }
}

 //Log in
 let logInCredentials = SyncCredentials.usernamePassword(username: "username", password: "password")

 SyncUser.logIn(with: logInCredentials, server: serverURL) { user, error in
  if user == nil {
    //Error
  }else{
    //Success
  }
}

Note how the register flag is added for account creation. This code is easier to understand and more DRY than the old way, so kudos to the Realm Swift team.

I hope this helps someone else.

Upvotes: 3

Related Questions