Skip to content

Commit

Permalink
feat:added credentials verificaion layer
Browse files Browse the repository at this point in the history
  • Loading branch information
ivary43 committed Apr 3, 2018
1 parent c2a3743 commit eda5788
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ protected void onCreate(Bundle savedInstanceState) {
void onLogin() {

String tenantIdentifier = etTenant.getEditableText().toString();
if (!TextUtils.isEmpty(tenantIdentifier)) {
if (!TextUtils.isEmpty(tenantIdentifier.trim())) {
preferencesHelper.putTenantIdentifier(tenantIdentifier);
} else {
etTenant.setError(getString(R.string.error_tenant_identifier_required));
return;
}

String username = etUsername.getEditableText().toString();
if (TextUtils.isEmpty(username)) {
if (TextUtils.isEmpty(username.trim())) {
etUsername.setError(getString(R.string.error_username_required));
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.apache.fineract.data.remote.BaseApiManager.retrofit;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;

import org.apache.fineract.R;
Expand Down Expand Up @@ -62,53 +63,77 @@ public void detachView() {
@Override
public void login(String username, String password) {
checkViewAttached();
getMvpView().showProgressDialog();
compositeDisposable.add(dataManagerAuth.login(username, password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<Authentication>() {
@Override
public void onNext(Authentication user) {
getMvpView().hideProgressDialog();
getMvpView().showUserLoginSuccessfully(user);
}

@Override
public void onError(Throwable throwable) {
getMvpView().hideProgressDialog();
if (throwable instanceof NoConnectivityException) {
getMvpView().showNoInternetConnection();
if (isCredentialsValid(username, password)) {
getMvpView().showProgressDialog();
compositeDisposable.add(dataManagerAuth.login(username, password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<Authentication>() {
@Override
public void onNext(Authentication user) {
getMvpView().hideProgressDialog();
getMvpView().showUserLoginSuccessfully(user);
}

MifosError mifosError = new MifosError();
Converter<ResponseBody, MifosError> errorConverter =
retrofit.responseBodyConverter(MifosError.class, new Annotation[0]);
if (throwable instanceof HttpException) {
HttpException httpException = (HttpException) throwable;
Response response = httpException.response();

if (response.errorBody() != null) {
try {
mifosError = errorConverter.convert(response.errorBody());
} catch (IOException e) {
Log.d(LOG_TAG, e.getLocalizedMessage());
}
@Override
public void onError(Throwable throwable) {
getMvpView().hideProgressDialog();
if (throwable instanceof NoConnectivityException) {
getMvpView().showNoInternetConnection();
}

if (mifosError.getMessage() == null) {
getMvpView().showError(
context.getString(R.string.wrong_username_or_password));
} else {
getMvpView().showError(mifosError.getMessage());
MifosError mifosError = new MifosError();
Converter<ResponseBody, MifosError> errorConverter =
retrofit.responseBodyConverter(MifosError.class,
new Annotation[0]);
if (throwable instanceof HttpException) {
HttpException httpException = (HttpException) throwable;
Response response = httpException.response();

if (response.errorBody() != null) {
try {
mifosError = errorConverter.convert(response.errorBody());
} catch (IOException e) {
Log.d(LOG_TAG, e.getLocalizedMessage());
}
}

if (mifosError.getMessage() == null) {
getMvpView().showError(
context.getString(R.string.wrong_username_or_password));
} else {
getMvpView().showError(mifosError.getMessage());
}
}
}
}

@Override
public void onComplete() {
@Override
public void onComplete() {

}
})
);
}
}

}
})
);
private boolean isCredentialsValid(String username, String password) {

final Resources resources = context.getResources();
if (username.length() < 5) {
getMvpView().showError(context.getString(R.string.error_validation_minimum_chars,
context.getString(R.string.username), resources
.getInteger(R.integer.username_minimum_length)));
} else if (username.trim().contains(" ")) {
getMvpView().showError(context.getString(R.string.error_validation_cannot_contain_spaces
, username.trim(), context.getString(R.string.spaces) ));
return false;
} else if (password.length() < 6) {
getMvpView().showError(context.getString(R.string.error_validation_minimum_chars,
context.getString(R.string.password), resources
.getInteger(R.integer.password_minimum_length)));
return false;
}

return true;
}
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
<string name="payment_cycle_week">Repay every %1$d %2$s on %3$s</string>
<string name="payment_cycle_year_day">Repay every %1$d %2$s on the %3$s day in %4$s</string>
<string name="payment_cycle_year_day_week">Repay every %1$d %2$s on the %3$s %4$s in %5$s</string>
<string name="error_validation_minimum_chars">%1$s cannot be less than %2$d characters</string>
<string name="error_validation_cannot_contain_spaces">%1$s cannot contain %2$s</string>
<string name="spaces">Spaces</string>

<string name="account">Account</string>
<string name="balance">Balance</string>
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/values/validation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<integer name="username_minimum_length">5</integer>
<integer name="password_minimum_length">6</integer>

</resources>

0 comments on commit eda5788

Please sign in to comment.