Search in sources :

Example 1 with ObjectServerError

use of io.realm.ObjectServerError in project realm-java by realm.

the class LoginActivity method login.

public void login(boolean createUser) {
    if (!validate()) {
        onLoginFailed("Invalid username or password");
        return;
    }
    createUserButton.setEnabled(false);
    loginButton.setEnabled(false);
    final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
    progressDialog.setIndeterminate(true);
    progressDialog.setMessage("Authenticating...");
    progressDialog.show();
    String username = this.username.getText().toString();
    String password = this.password.getText().toString();
    SyncCredentials creds = SyncCredentials.usernamePassword(username, password, createUser);
    String authUrl = "http://" + BuildConfig.OBJECT_SERVER_IP + ":9080/auth";
    SyncUser.Callback callback = new SyncUser.Callback() {

        @Override
        public void onSuccess(SyncUser user) {
            progressDialog.dismiss();
            onLoginSuccess();
        }

        @Override
        public void onError(ObjectServerError error) {
            progressDialog.dismiss();
            String errorMsg;
            switch(error.getErrorCode()) {
                case UNKNOWN_ACCOUNT:
                    errorMsg = "Account does not exists.";
                    break;
                case INVALID_CREDENTIALS:
                    errorMsg = "User name and password does not match";
                    break;
                default:
                    errorMsg = error.toString();
            }
            onLoginFailed(errorMsg);
        }
    };
    SyncUser.loginAsync(creds, authUrl, callback);
}
Also used : SyncUser(io.realm.SyncUser) SyncCredentials(io.realm.SyncCredentials) ProgressDialog(android.app.ProgressDialog) ObjectServerError(io.realm.ObjectServerError)

Example 2 with ObjectServerError

use of io.realm.ObjectServerError in project realm-java by realm.

the class AuthTests method loginAsync_userNotExist.

@Test
@RunTestInLooperThread
public void loginAsync_userNotExist() {
    SyncCredentials credentials = SyncCredentials.usernamePassword("IWantToHackYou", "GeneralPassword", false);
    SyncUser.loginAsync(credentials, Constants.AUTH_URL, new SyncUser.Callback() {

        @Override
        public void onSuccess(SyncUser user) {
            fail();
        }

        @Override
        public void onError(ObjectServerError error) {
            assertEquals(ErrorCode.INVALID_CREDENTIALS, error.getErrorCode());
            looperThread.testComplete();
        }
    });
}
Also used : SyncUser(io.realm.SyncUser) SyncCredentials(io.realm.SyncCredentials) ObjectServerError(io.realm.ObjectServerError) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 3 with ObjectServerError

use of io.realm.ObjectServerError in project realm-java by realm.

the class AuthTests method loginAsync_errorHandlerThrows.

// The error handler throws an exception but it is ignored (but logged). That means, this test should not
// pass and not be stopped by an IllegalArgumentException.
@Test
@RunTestInLooperThread
public void loginAsync_errorHandlerThrows() {
    // set log level to info to make sure the IllegalArgumentException
    // thrown in the test is visible in Logcat
    final int defaultLevel = RealmLog.getLevel();
    RealmLog.setLevel(LogLevel.INFO);
    SyncCredentials credentials = SyncCredentials.usernamePassword("IWantToHackYou", "GeneralPassword", false);
    SyncUser.loginAsync(credentials, Constants.AUTH_URL, new SyncUser.Callback() {

        @Override
        public void onSuccess(SyncUser user) {
            fail();
        }

        @Override
        public void onError(ObjectServerError error) {
            assertEquals(ErrorCode.INVALID_CREDENTIALS, error.getErrorCode());
            throw new IllegalArgumentException("BOOM");
        }
    });
    looperThread.postRunnableDelayed(new Runnable() {

        @Override
        public void run() {
            RealmLog.setLevel(defaultLevel);
            looperThread.testComplete();
        }
    }, 1000);
}
Also used : SyncUser(io.realm.SyncUser) SyncCredentials(io.realm.SyncCredentials) ObjectServerError(io.realm.ObjectServerError) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 4 with ObjectServerError

use of io.realm.ObjectServerError in project realm-java by realm.

the class AuthTests method login_withAccessToken.

@Test
@RunTestInLooperThread
public void login_withAccessToken() {
    SyncUser admin = UserFactory.createAdminUser(Constants.AUTH_URL);
    SyncCredentials credentials = SyncCredentials.accessToken(admin.getAccessToken().value(), "custom-admin-user");
    SyncUser.loginAsync(credentials, Constants.AUTH_URL, new SyncUser.Callback() {

        @Override
        public void onSuccess(SyncUser user) {
            final SyncConfiguration config = new SyncConfiguration.Builder(user, Constants.SYNC_SERVER_URL).errorHandler(new SyncSession.ErrorHandler() {

                @Override
                public void onError(SyncSession session, ObjectServerError error) {
                    fail("Session failed: " + error);
                }

                @Override
                public void onClientResetRequired(SyncSession session, ClientResetHandler handler) {
                    fail("Client Reset");
                }
            }).build();
            final Realm realm = Realm.getInstance(config);
            looperThread.testRealms.add(realm);
            // FIXME: Right now we have no Java API for detecting when a session is established
            // So we optimistically assume it has been connected after 1 second.
            looperThread.postRunnableDelayed(new Runnable() {

                @Override
                public void run() {
                    assertTrue(SyncManager.getSession(config).getUser().isValid());
                    looperThread.testComplete();
                }
            }, 1000);
        }

        @Override
        public void onError(ObjectServerError error) {
            fail("Login failed: " + error);
        }
    });
}
Also used : SyncUser(io.realm.SyncUser) ClientResetHandler(io.realm.ClientResetHandler) SyncSession(io.realm.SyncSession) SyncCredentials(io.realm.SyncCredentials) ObjectServerError(io.realm.ObjectServerError) Realm(io.realm.Realm) SyncConfiguration(io.realm.SyncConfiguration) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 5 with ObjectServerError

use of io.realm.ObjectServerError in project realm-java by realm.

the class AuthTests method login_userNotExist.

@Test
public void login_userNotExist() {
    SyncCredentials credentials = SyncCredentials.usernamePassword("IWantToHackYou", "GeneralPassword", false);
    try {
        SyncUser.login(credentials, Constants.AUTH_URL);
        fail();
    } catch (ObjectServerError expected) {
        assertEquals(ErrorCode.INVALID_CREDENTIALS, expected.getErrorCode());
    }
}
Also used : SyncCredentials(io.realm.SyncCredentials) ObjectServerError(io.realm.ObjectServerError) Test(org.junit.Test)

Aggregations

ObjectServerError (io.realm.ObjectServerError)9 SyncUser (io.realm.SyncUser)7 Test (org.junit.Test)7 SyncCredentials (io.realm.SyncCredentials)5 ClientResetHandler (io.realm.ClientResetHandler)4 Realm (io.realm.Realm)4 SyncConfiguration (io.realm.SyncConfiguration)4 SyncSession (io.realm.SyncSession)4 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)4 RealmChangeListener (io.realm.RealmChangeListener)3 RealmResults (io.realm.RealmResults)3 Ignore (org.junit.Ignore)3 Context (android.content.Context)2 Intent (android.content.Intent)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 ProgressDialog (android.app.ProgressDialog)1 ErrorCode (io.realm.ErrorCode)1 Dog (io.realm.entities.Dog)1 ProcessInfo (io.realm.objectserver.model.ProcessInfo)1