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);
}
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();
}
});
}
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);
}
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);
}
});
}
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());
}
}
Aggregations