diff --git a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java index d03c039..0454cfd 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java @@ -32,6 +32,8 @@ import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Arrays; import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI; @@ -139,15 +141,27 @@ public class LoginActivity extends AppCompatActivity { } instance = host = binding.loginInstance.getText().toString().trim().toLowerCase(); } + + if( instance.startsWith("http")) { + try { + URL url = new URL(instance); + instance = url.getHost(); + host = instance; + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } if (!Patterns.WEB_URL.matcher("https://"+instance).matches()) { Toasty.error(LoginActivity.this, getString(R.string.not_valide_instance)).show(); binding.loginButton.setEnabled(true); return; } + String finalInstance = instance; + String finalHost = host; if (Arrays.asList(Helper.openid).contains(host) && !BuildConfig.full_instances) { new Thread(() -> { try { - Oauth oauth = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).oauthClient(null, null, null, null); + Oauth oauth = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).oauthClient(null, null, null, null); if (oauth == null) { runOnUiThread(() -> { binding.loginButton.setEnabled(true); @@ -164,7 +178,7 @@ public class LoginActivity extends AppCompatActivity { editor.apply(); Intent intent = new Intent(LoginActivity.this, WebviewConnectActivity.class); Bundle b = new Bundle(); - b.putString("url", "https://" + Helper.getPeertubeUrl(host) + "/plugins/auth-openid-connect/0.0.1/auth/openid-connect"); + b.putString("url", "https://" + Helper.getPeertubeUrl(finalHost) + "/plugins/auth-openid-connect/0.0.1/auth/openid-connect"); intent.putExtras(b); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); @@ -179,7 +193,7 @@ public class LoginActivity extends AppCompatActivity { }).start(); } else { new Thread(() -> { - Oauth oauth = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).oauthClient(Helper.CLIENT_NAME_VALUE, Helper.WEBSITE_VALUE, Helper.OAUTH_SCOPES_PEERTUBE, Helper.WEBSITE_VALUE); + Oauth oauth = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).oauthClient(Helper.CLIENT_NAME_VALUE, Helper.WEBSITE_VALUE, Helper.OAUTH_SCOPES_PEERTUBE, Helper.WEBSITE_VALUE); if (oauth == null) { runOnUiThread(() -> { binding.loginButton.setEnabled(true); @@ -206,13 +220,13 @@ public class LoginActivity extends AppCompatActivity { oauthParams.setPassword(binding.loginPasswd.getText().toString()); } try { - Token token = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).manageToken(oauthParams); - proceedLogin(token, host); + Token token = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).manageToken(oauthParams); + proceedLogin(token, finalHost); } catch (final Exception | Error e) { oauthParams.setUsername(binding.loginUid.getText().toString().toLowerCase().trim()); try { - Token token = new RetrofitPeertubeAPI(LoginActivity.this, instance, null).manageToken(oauthParams); - proceedLogin(token, host); + Token token = new RetrofitPeertubeAPI(LoginActivity.this, finalInstance, null).manageToken(oauthParams); + proceedLogin(token, finalHost); } catch (Error error) { Error.displayError(LoginActivity.this, error); error.printStackTrace();