Merge pull request 'Fix #341 - Store UserName on Login first' (#343) from 6543/GitNex:fix-341 into master

Reviewed-on: https://gitea.com/gitnex/GitNex/pulls/343
This commit is contained in:
M M Arif 2020-04-02 20:11:18 +00:00
commit 8234ad77e3
1 changed files with 90 additions and 25 deletions

View File

@ -92,6 +92,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
protocolSpinner.setAdapter(adapterProtocols);
protocolSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String value = getResources().getStringArray(R.array.protocolValues)[pos];
@ -100,8 +101,10 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
info_button.setOnClickListener(infoListener);
@ -385,8 +388,6 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
private void versionCheck(final String instanceUrl, final String loginUid, final String loginPass, final int loginOTP, final String loginToken_, final int loginType) {
final TinyDB tinyDb = new TinyDB(getApplicationContext());
Call<GiteaVersion> callVersion = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
@ -421,21 +422,17 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
.setMessage(getResources().getString(R.string.versionUnsupportedOld, version.getVersion()))
.setCancelable(true)
.setIcon(R.drawable.ic_warning)
.setNegativeButton(getString(R.string.cancelButton), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
enableProcessButton();
}
.setNegativeButton(getString(R.string.cancelButton), (dialog, which) -> {
dialog.dismiss();
enableProcessButton();
})
.setPositiveButton(getString(R.string.textContinue), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
.setPositiveButton(getString(R.string.textContinue), (dialog, which) -> {
dialog.dismiss();
login(loginType, instanceUrl, loginUid, loginPass, loginOTP, loginToken_);
dialog.dismiss();
login(loginType, instanceUrl, loginUid, loginPass, loginOTP, loginToken_);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
@ -450,17 +447,21 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
else if (responseVersion.code() == 403) {
login(loginType, instanceUrl, loginUid, loginPass, loginOTP, loginToken_);
}
}
private void login(int loginType, String instanceUrl, String loginUid, String loginPass, int loginOTP, String loginToken_) {
if (loginType == 1) {
letTheUserIn(instanceUrl, loginUid, loginPass, loginOTP);
}
else if (loginType == 2) { // token
letTheUserInViaToken(instanceUrl, loginToken_);
}
}
@Override
@ -498,6 +499,7 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
assert userDetails != null;
tinyDb.putString(userDetails.getLogin() + "-token", loginToken_);
tinyDb.putString("loginUid", userDetails.getLogin());
tinyDb.putString("userLogin", userDetails.getUsername());
enableProcessButton();
loginButton.setText(R.string.btnLogin);
@ -530,10 +532,12 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
@Override
public void onFailure(@NonNull Call<UserInfo> call, @NonNull Throwable t) {
Log.e("onFailure", t.toString());
Toasty.info(getApplicationContext(), getResources().getString(R.string.genericError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
});
@ -545,16 +549,20 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
Call<List<UserTokens>> call;
if(loginOTP != 0) {
call = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.getUserTokensWithOTP(credential, loginOTP, loginUid);
}
else {
call = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.getUserTokens(credential, loginUid);
}
call.enqueue(new Callback<List<UserTokens>>() {
@ -607,16 +615,20 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
Call<UserTokens> callCreateToken;
if(loginOTP != 0) {
callCreateToken = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.createNewTokenWithOTP(credential, loginOTP, loginUid, createUserToken);
}
else {
callCreateToken = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.createNewToken(credential, loginUid, createUserToken);
}
callCreateToken.enqueue(new Callback<UserTokens>() {
@ -634,14 +646,66 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
if (!newToken.getSha1().equals("")) {
tinyDb.remove("loginPass");
tinyDb.putBoolean("loggedInMode", true);
tinyDb.putString(loginUid + "-token", newToken.getSha1());
tinyDb.putString(loginUid + "-token-last-eight", appUtil.getLastCharactersOfWord(newToken.getSha1(), 8));
//Log.i("Tokens", "new:" + newToken.getSha1() + " old:" + tinyDb.getString(loginUid + "-token"));
Call<UserInfo> call = RetrofitClient
.getInstance(instanceUrl, getApplicationContext())
.getApiInterface()
.getUserInfo("token " + newToken.getSha1());
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
call.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(@NonNull Call<UserInfo> call, @NonNull retrofit2.Response<UserInfo> response) {
UserInfo userDetails = response.body();
if (response.isSuccessful()) {
if (response.code() == 200) {
tinyDb.remove("loginPass");
tinyDb.putBoolean("loggedInMode", true);
assert userDetails != null;
tinyDb.putString("userLogin", userDetails.getUsername());
tinyDb.putString(loginUid + "-token", newToken.getSha1());
tinyDb.putString(loginUid + "-token-last-eight", appUtil.getLastCharactersOfWord(newToken.getSha1(), 8));
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
}
else if(response.code() == 401) {
String toastError = getResources().getString(R.string.unauthorizedApiError);
Toasty.info(getApplicationContext(), toastError);
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
else {
String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
Toasty.info(getApplicationContext(), toastError);
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
}
@Override
public void onFailure(@NonNull Call<UserInfo> call, @NonNull Throwable t) {
Log.e("onFailure", t.toString());
Toasty.info(getApplicationContext(), getResources().getString(R.string.genericError));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
});
}
@ -660,15 +724,16 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
}
@Override
public void onFailure(@NonNull Call<UserTokens> createUserToken, Throwable t) {
public void onFailure(@NonNull Call<UserTokens> createUserToken, @NonNull Throwable t) {
Log.e("onFailure-token", t.toString());
}
});
}
else {
//Log.i("Current Token", tinyDb.getString(loginUid + "-token"));
tinyDb.putBoolean("loggedInMode", true);
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
@ -689,8 +754,6 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
else {
String toastError = getResources().getString(R.string.genericApiStatusError) + response.code();
//Log.i("error message else4", String.valueOf(response.code()));
Toasty.info(getApplicationContext(), toastError);
enableProcessButton();
loginButton.setText(R.string.btnLogin);
@ -701,10 +764,12 @@ public class LoginActivity extends BaseActivity implements View.OnClickListener
@Override
public void onFailure(@NonNull Call<List<UserTokens>> call, @NonNull Throwable t) {
Log.e("onFailure-login", t.toString());
Toasty.info(getApplicationContext(), getResources().getString(R.string.malformedJson));
enableProcessButton();
loginButton.setText(R.string.btnLogin);
}
});