login issue
This commit is contained in:
parent
b1d1111142
commit
6689a9c3b4
|
@ -2,6 +2,7 @@ package app.fedilab.fedilabtube;
|
|||
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.text.SpannableString;
|
||||
|
@ -9,11 +10,15 @@ import android.text.Spanned;
|
|||
import android.text.style.ForegroundColorSpan;
|
||||
import android.text.style.UnderlineSpan;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
|
@ -24,10 +29,14 @@ import java.io.UnsupportedEncodingException;
|
|||
import java.net.URLEncoder;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import app.fedilab.fedilabtube.asynctasks.UpdateAccountInfoAsyncTask;
|
||||
import app.fedilab.fedilabtube.client.HttpsConnection;
|
||||
import app.fedilab.fedilabtube.client.entities.Account;
|
||||
import app.fedilab.fedilabtube.helper.Helper;
|
||||
import app.fedilab.fedilabtube.sqlite.AccountDAO;
|
||||
import app.fedilab.fedilabtube.sqlite.Sqlite;
|
||||
import es.dmoral.toasty.Toasty;
|
||||
|
||||
|
||||
|
@ -72,6 +81,57 @@ public class LoginActivity extends AppCompatActivity {
|
|||
login_passwd = findViewById(R.id.login_passwd);
|
||||
connectionButton = findViewById(R.id.login_button);
|
||||
|
||||
LinearLayout connected = findViewById(R.id.connected);
|
||||
LinearLayout not_connected = findViewById(R.id.not_connected);
|
||||
|
||||
if( Helper.isLoggedIn(LoginActivity.this)) {
|
||||
not_connected.setVisibility(View.GONE);
|
||||
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
|
||||
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
||||
String instance = sharedpreferences.getString(Helper.PREF_INSTANCE, Helper.getLiveInstance(LoginActivity.this));
|
||||
|
||||
TextView instanceView = findViewById(R.id.instance);
|
||||
Account account = new AccountDAO(LoginActivity.this, db).getUniqAccount(userId, instance);
|
||||
if( account == null) {
|
||||
account = new AccountDAO(LoginActivity.this, db).getUniqAccount(userId, Helper.getPeertubeUrl(instance));
|
||||
}
|
||||
if( account != null) {
|
||||
ImageView profile_picture = findViewById(R.id.profile_picture);
|
||||
TextView username = findViewById(R.id.username);
|
||||
TextView displayname = findViewById(R.id.displayname);
|
||||
|
||||
Helper.loadGiF(LoginActivity.this, account, profile_picture);
|
||||
username.setText(String.format("@%s", account.getUsername()));
|
||||
displayname.setText(account.getDisplay_name());
|
||||
|
||||
instanceView.setText(account.getInstance());
|
||||
|
||||
Button logout_button = findViewById(R.id.logout_button);
|
||||
Account finalAccount = account;
|
||||
logout_button.setOnClickListener(v -> {
|
||||
AlertDialog.Builder dialogBuilderLogoutAccount = new AlertDialog.Builder(LoginActivity.this);
|
||||
dialogBuilderLogoutAccount.setMessage(getString(R.string.logout_account_confirmation, finalAccount.getUsername()));
|
||||
dialogBuilderLogoutAccount.setPositiveButton(R.string.action_logout, (dialog, id) -> {
|
||||
Helper.logoutCurrentUser(LoginActivity.this, finalAccount);
|
||||
dialog.dismiss();
|
||||
});
|
||||
dialogBuilderLogoutAccount.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
|
||||
AlertDialog alertDialogLogoutAccount = dialogBuilderLogoutAccount.create();
|
||||
alertDialogLogoutAccount.show();
|
||||
});
|
||||
}else{
|
||||
Helper.logoutCurrentUser(LoginActivity.this, null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}else{
|
||||
connected.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
|
||||
connectionButton.setOnClickListener(v -> {
|
||||
|
||||
|
@ -136,11 +196,10 @@ public class LoginActivity extends AppCompatActivity {
|
|||
JSONObject resobjLogin;
|
||||
try {
|
||||
resobjLogin = new JSONObject(responseLogin);
|
||||
String token = resobjLogin.get("access_token").toString();
|
||||
String refresh_token = null;
|
||||
if (resobjLogin.has("refresh_token"))
|
||||
refresh_token = resobjLogin.getString("refresh_token");
|
||||
String token = resobjLogin.getString("access_token");
|
||||
String refresh_token = resobjLogin.getString("refresh_token");
|
||||
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token);
|
||||
editor.putString(Helper.PREF_INSTANCE, host);
|
||||
editor.apply();
|
||||
//Update the account with the token;
|
||||
new UpdateAccountInfoAsyncTask(LoginActivity.this, token, client_id, client_secret, refresh_token, host).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
|
|
|
@ -41,7 +41,7 @@ public class UpdateAccountInfoAsyncTask extends AsyncTask<Void, Void, Void> {
|
|||
return null;
|
||||
}
|
||||
String instance = Helper.getPeertubeUrl(host);
|
||||
account = new PeertubeAPI(this.contextReference.get(), instance, null).verifyCredentials();
|
||||
account = new PeertubeAPI(this.contextReference.get()).verifyCredentials(token, instance);
|
||||
if (account == null)
|
||||
return null;
|
||||
try {
|
||||
|
|
|
@ -711,34 +711,27 @@ public class PeertubeAPI {
|
|||
* Verifiy credential of the authenticated user *synchronously*
|
||||
* @return Account
|
||||
*/
|
||||
public Account verifyCredentials() {
|
||||
public Account verifyCredentials(String token, String instance) {
|
||||
account = new Account();
|
||||
InstanceNodeInfo nodeinfo = displayNodeInfo(instance);
|
||||
String social = null;
|
||||
if (nodeinfo != null) {
|
||||
social = nodeinfo.getName();
|
||||
}
|
||||
try {
|
||||
String response = new HttpsConnection(context).get(getAbsoluteUrl("/users/me"), 60, null, prefKeyOauthTokenT);
|
||||
String response = new HttpsConnection(context).get("https://"+instance+"/api/v1/users/me", 60, null, token);
|
||||
JSONObject accountObject = new JSONObject(response).getJSONObject("account");
|
||||
account = parseAccountResponsePeertube(accountObject);
|
||||
if (social != null) {
|
||||
account.setSocial(social.toUpperCase());
|
||||
}
|
||||
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
|
||||
e.printStackTrace();
|
||||
} catch (HttpsConnection.HttpsConnectionException e) {
|
||||
|
||||
if (e.getStatusCode() == 401 || e.getStatusCode() == 403) {
|
||||
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
Account targetedAccount = new AccountDAO(context, db).getAccountByToken(prefKeyOauthTokenT);
|
||||
Account targetedAccount = new AccountDAO(context, db).getAccountByToken(token);
|
||||
if (targetedAccount != null) {
|
||||
HashMap<String, String> values = refreshToken(targetedAccount.getClient_id(), targetedAccount.getClient_secret(), targetedAccount.getRefresh_token());
|
||||
if (values.containsKey("access_token") && values.get("access_token") != null) {
|
||||
targetedAccount.setToken(values.get("access_token"));
|
||||
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
||||
String tokenShared = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
||||
//This account is currently logged in, the token is updated
|
||||
if (prefKeyOauthTokenT != null && prefKeyOauthTokenT.equals(token)) {
|
||||
if (tokenShared != null && token.compareTo(tokenShared) == 0) {
|
||||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, targetedAccount.getToken());
|
||||
editor.apply();
|
||||
|
@ -750,12 +743,9 @@ public class PeertubeAPI {
|
|||
|
||||
String response;
|
||||
try {
|
||||
response = new HttpsConnection(context).get(getAbsoluteUrl("/users/me"), 60, null, targetedAccount.getToken());
|
||||
response = new HttpsConnection(context).get("https://"+instance+"/api/v1/users/me", 60, null, targetedAccount.getToken());
|
||||
JSONObject accountObject = new JSONObject(response).getJSONObject("account");
|
||||
account = parseAccountResponsePeertube(accountObject);
|
||||
if (social != null) {
|
||||
account.setSocial(social.toUpperCase());
|
||||
}
|
||||
} catch (IOException | NoSuchAlgorithmException | KeyManagementException | JSONException e1) {
|
||||
e1.printStackTrace();
|
||||
} catch (HttpsConnection.HttpsConnectionException e1) {
|
||||
|
|
|
@ -7,11 +7,13 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Resources;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
|
@ -43,9 +45,12 @@ import java.util.Locale;
|
|||
import java.util.TimeZone;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import app.fedilab.fedilabtube.MainActivity;
|
||||
import app.fedilab.fedilabtube.R;
|
||||
import app.fedilab.fedilabtube.WebviewActivity;
|
||||
import app.fedilab.fedilabtube.client.entities.Account;
|
||||
import app.fedilab.fedilabtube.sqlite.AccountDAO;
|
||||
import app.fedilab.fedilabtube.sqlite.Sqlite;
|
||||
import app.fedilab.fedilabtube.webview.CustomWebview;
|
||||
import app.fedilab.fedilabtube.webview.ProxyHelper;
|
||||
import es.dmoral.toasty.Toasty;
|
||||
|
@ -550,6 +555,36 @@ public class Helper {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Log out the authenticated user by removing its token
|
||||
*
|
||||
* @param activity Activity
|
||||
*/
|
||||
public static void logoutCurrentUser(Activity activity, Account account) {
|
||||
SharedPreferences sharedpreferences = activity.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
|
||||
SQLiteDatabase db = Sqlite.getInstance(activity.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
if (account != null) {
|
||||
new AccountDAO(activity, db).removeUser(account);
|
||||
}
|
||||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
||||
editor.putString(Helper.CLIENT_ID, null);
|
||||
editor.putString(Helper.CLIENT_SECRET, null);
|
||||
editor.putString(Helper.PREF_KEY_ID, null);
|
||||
editor.putBoolean(Helper.PREF_IS_MODERATOR, false);
|
||||
editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, false);
|
||||
editor.putString(Helper.PREF_INSTANCE, null);
|
||||
editor.putString(Helper.ID, null);
|
||||
editor.apply();
|
||||
Intent mainActivity = new Intent(activity, MainActivity.class);
|
||||
mainActivity.putExtra(Helper.INTENT_ACTION, Helper.ADD_USER_INTENT);
|
||||
activity.startActivity(mainActivity);
|
||||
activity.finish();
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static int getAttColor(Context context, int attColor) {
|
||||
TypedValue typedValue = new TypedValue();
|
||||
context.getTheme().resolveAttribute(attColor, typedValue, true);
|
||||
|
|
|
@ -4,12 +4,16 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<LinearLayout
|
||||
<RelativeLayout
|
||||
android:id="@+id/drawer_layout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
>
|
||||
<LinearLayout
|
||||
android:id="@+id/not_connected"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/step_instance"
|
||||
android:layout_width="match_parent"
|
||||
|
@ -122,7 +126,93 @@
|
|||
app:layout_constraintTop_toBottomOf="@id/login_passwd_container" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/connected"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingStart="50dp"
|
||||
android:paddingEnd="50dp">
|
||||
|
||||
<TextView
|
||||
android:layout_marginTop="20dp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
android:layout_width="match_parent"
|
||||
android:textSize="18sp"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/instance"/>
|
||||
<androidx.cardview.widget.CardView
|
||||
android:layout_marginTop="20dp"
|
||||
android:id="@+id/cardview_profile"
|
||||
android:src="@drawable/ic_launcher_foreground"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/instance"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
|
||||
android:padding="10dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<ImageView
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="60dp"
|
||||
android:id="@+id/profile_picture"
|
||||
android:contentDescription="@string/profile_picture" />
|
||||
<TextView
|
||||
android:layout_marginStart="20dp"
|
||||
app:layout_constraintStart_toEndOf="@+id/profile_picture"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
android:id="@+id/displayname"
|
||||
android:textSize="18sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
<TextView
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_marginTop="30dp"
|
||||
app:layout_constraintStart_toEndOf="@+id/profile_picture"
|
||||
app:layout_constraintTop_toTopOf="@+id/displayname"
|
||||
android:id="@+id/username"
|
||||
android:textSize="14sp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"/>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</androidx.cardview.widget.CardView>
|
||||
|
||||
<Button
|
||||
android:id="@+id/logout_button"
|
||||
style="@style/Base.Widget.AppCompat.Button.Colored"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
android:layout_marginTop="30dp"
|
||||
android:drawablePadding="10dp"
|
||||
android:gravity="center"
|
||||
android:paddingStart="15dp"
|
||||
android:paddingTop="12dp"
|
||||
android:paddingEnd="20dp"
|
||||
android:paddingBottom="12dp"
|
||||
android:singleLine="true"
|
||||
android:text="@string/action_logout"
|
||||
android:textAllCaps="false"
|
||||
android:textSize="20sp"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/cardview_profile" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</LinearLayout>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
</ScrollView>
|
|
@ -141,5 +141,7 @@
|
|||
<string name="agreement_check">J\'accepte les %1$s et les %2$s</string>
|
||||
<string name="email_error_domain">Les adresses mails %1$s ne sont pas autorisées !</string>
|
||||
<string name="create_an_account">Créer un compte</string>
|
||||
<string name="action_logout">Déconnexion</string>
|
||||
<string name="logout_account_confirmation">Voulez-vous vraiment déconnecter le compte @%1$s ?</string>
|
||||
|
||||
</resources>
|
Loading…
Reference in New Issue