Search in sources :

Example 1 with SyncCredentials

use of io.realm.SyncCredentials 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 SyncCredentials

use of io.realm.SyncCredentials 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 SyncCredentials

use of io.realm.SyncCredentials 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 SyncCredentials

use of io.realm.SyncCredentials 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 SyncCredentials

use of io.realm.SyncCredentials 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)5 SyncCredentials (io.realm.SyncCredentials)5 SyncUser (io.realm.SyncUser)4 Test (org.junit.Test)4 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)3 ProgressDialog (android.app.ProgressDialog)1 ClientResetHandler (io.realm.ClientResetHandler)1 Realm (io.realm.Realm)1 SyncConfiguration (io.realm.SyncConfiguration)1 SyncSession (io.realm.SyncSession)1