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