Search in sources :

Example 1 with LogoutResponse

use of io.realm.internal.network.LogoutResponse in project realm-java by realm.

the class SyncUser method logout.

/**
     * Logs out the user from the Realm Object Server. Once the Object Server has confirmed the logout any registered
     * {@link AuthenticationListener} will be notified and user credentials will be deleted from this device.
     *
     * @throws IllegalStateException if any Realms owned by this user is still open. They should be closed before
     *         logging out.
     */
/* FIXME: Add this back to the javadoc when enable SyncConfiguration.Builder#deleteRealmOnLogout()
     <p>
     Any Realms owned by the user will be deleted if {@link SyncConfiguration.Builder#deleteRealmOnLogout()} is
     also set.
     */
public void logout() {
    // Acquire lock to prevent users creating new instances
    synchronized (Realm.class) {
        if (!syncUser.isLoggedIn()) {
            // Already local/global logout status
            return;
        }
        // Ensure that we can log out. If any Realm file is still open we should abort before doing anything
        // else.
        Collection<SyncSession> sessions = syncUser.getSessions();
        for (SyncSession session : sessions) {
            SyncConfiguration config = session.getConfiguration();
            if (Realm.getGlobalInstanceCount(config) > 0) {
                throw new IllegalStateException("A Realm controlled by this user is still open. Close all Realms " + "before logging out: " + config.getPath());
            }
        }
        SyncManager.getUserStore().remove(syncUser.getIdentity());
        // Delete all Realms if needed.
        for (ObjectServerUser.AccessDescription desc : syncUser.getRealms()) {
            // disabled. Make sure this works for Realm opened in the client thread/other processes.
            if (desc.deleteOnLogout) {
                File realmFile = new File(desc.localPath);
                if (realmFile.exists() && !Util.deleteRealm(desc.localPath, realmFile.getParentFile(), realmFile.getName())) {
                    RealmLog.error("Could not delete Realm when user logged out: " + desc.localPath);
                }
            }
        }
        // Remove all local tokens, preventing further connections.
        final Token userToken = syncUser.getUserToken();
        syncUser.clearTokens();
        syncUser.localLogout();
        // Finally revoke server token. The local user is logged out in any case.
        final AuthenticationServer server = SyncManager.getAuthServer();
        ThreadPoolExecutor networkPoolExecutor = SyncManager.NETWORK_POOL_EXECUTOR;
        networkPoolExecutor.submit(new ExponentialBackoffTask<LogoutResponse>() {

            @Override
            protected LogoutResponse execute() {
                return server.logout(userToken, syncUser.getAuthenticationUrl());
            }

            @Override
            protected void onSuccess(LogoutResponse response) {
                SyncManager.notifyUserLoggedOut(SyncUser.this);
            }

            @Override
            protected void onError(LogoutResponse response) {
                RealmLog.error("Failed to log user out.\n" + response.getError().toString());
            }
        });
    }
}
Also used : LogoutResponse(io.realm.internal.network.LogoutResponse) Token(io.realm.internal.objectserver.Token) AuthenticationServer(io.realm.internal.network.AuthenticationServer) ObjectServerUser(io.realm.internal.objectserver.ObjectServerUser) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) File(java.io.File)

Aggregations

AuthenticationServer (io.realm.internal.network.AuthenticationServer)1 LogoutResponse (io.realm.internal.network.LogoutResponse)1 ObjectServerUser (io.realm.internal.objectserver.ObjectServerUser)1 Token (io.realm.internal.objectserver.Token)1 File (java.io.File)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1