Search code examples
react-nativerealm

How can you logout with Realm javascript?


I'm using the Realm self hosted platform for javascript for a react native App. The login code works fine, as show below...

Realm.Sync.User.login('http://0.0.0.0:9080', creds)
.then(result => resolve(result))
.catch(error => reject(error))
...

However, when I try to logout, I get an error that says Realm.Sync.User.logout is not a function. Here is the logout code...

logout = () => {
    Realm.Sync.User.logout()
}

I've gone through the docs a few times, but it seems this function simply doesn't exist. https://realm.io/docs/javascript/latest/api/Realm.Sync.User.html

I'm using realm 2.18.0. Any ideas?


Solution

  • The issue is that you are trying to use logout as a static method, but it is an instance method. You need to store a reference to your logged in User instance and call logout on that instance.

    Realm.Sync.User.login('http://0.0.0.0:9080', creds)
    .then(user => resolve(user))
    .catch(error => reject(error))
    ...
    

    Make sure that you define user to be in scope for logout and also make sure that you only call logout once login is resolved.

    logout = () => {
        user.logout()
    }