From c49efba57d3f904d8b5fe471644408da835d6d00 Mon Sep 17 00:00:00 2001 From: sebastian Date: Sun, 3 Nov 2024 18:54:53 +0100 Subject: [PATCH] Implement Login Logic --- app/build.gradle.kts | 8 +++ app/src/main/AndroidManifest.xml | 2 + .../java/core/notevault/MainActivity.java | 21 ++++++++ .../java/core/notevault/sync/ApiClient.java | 19 +++++++ .../notevault/sync/auth/AuthRepository.java | 50 +++++++++++++++++++ .../core/notevault/sync/auth/AuthService.java | 11 ++++ .../notevault/sync/auth/LoginCallback.java | 7 +++ .../notevault/sync/auth/LoginRequest.java | 13 +++++ .../notevault/sync/auth/LoginResponse.java | 22 ++++++++ .../notevault/ui/login/LoginCallBackImpl.java | 25 ++++++++++ .../ui/login/LoginDialogFragment.java | 41 +++++++++++++++ app/src/main/res/drawable/login.xml | 9 ++++ .../main/res/layout/fragment_login_dialog.xml | 21 ++++++++ app/src/main/res/menu/main.xml | 15 +++--- 14 files changed, 257 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/core/notevault/sync/ApiClient.java create mode 100644 app/src/main/java/core/notevault/sync/auth/AuthRepository.java create mode 100644 app/src/main/java/core/notevault/sync/auth/AuthService.java create mode 100644 app/src/main/java/core/notevault/sync/auth/LoginCallback.java create mode 100644 app/src/main/java/core/notevault/sync/auth/LoginRequest.java create mode 100644 app/src/main/java/core/notevault/sync/auth/LoginResponse.java create mode 100644 app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java create mode 100644 app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java create mode 100644 app/src/main/res/drawable/login.xml create mode 100644 app/src/main/res/layout/fragment_login_dialog.xml diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 338d995..09ccecd 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -22,6 +22,10 @@ android { isMinifyEnabled = false proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") } + debug { + // Setzt den Manifest-Platzhalter nur für den Debug-Build auf true + manifestPlaceholders["usesCleartextTraffic"] = "true" + } } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 @@ -48,6 +52,10 @@ dependencies { testImplementation(libs.junit) androidTestImplementation(libs.ext.junit) androidTestImplementation(libs.espresso.core) + implementation("com.squareup.retrofit2:retrofit:2.9.0") + implementation("com.squareup.retrofit2:converter-gson:2.9.0") + implementation("com.squareup.okhttp3:logging-interceptor:4.9.1") + val room_version = "2.6.1" // Aktuelle Room-Version diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d970a5f..ddcd607 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + () { + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful() && response.body() != null) { + String token = response.body().getToken(); + saveToken(token); + + // Erfolgsrückmeldung an den Callback senden + callback.onSuccess(); + } else { + // Fehlermeldung an den Callback senden + callback.onError("Login fehlgeschlagen. Überprüfe Benutzername und Passwort."); + } + } + + @Override + public void onFailure(Call call, Throwable t) { + // Netzwerkfehler an den Callback senden + callback.onError("Netzwerkfehler: " + t.getMessage()); + } + }); + } + + private void saveToken(String token) { + SharedPreferences.Editor editor = preferences.edit(); + editor.putString("jwt_token", token); + editor.apply(); + } +} diff --git a/app/src/main/java/core/notevault/sync/auth/AuthService.java b/app/src/main/java/core/notevault/sync/auth/AuthService.java new file mode 100644 index 0000000..8790231 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/auth/AuthService.java @@ -0,0 +1,11 @@ +package core.notevault.sync.auth; + +import retrofit2.Call; +import retrofit2.http.Body; +import retrofit2.http.POST; + +public interface AuthService { + + @POST("api/v1/auth/login") + Call login(@Body LoginRequest loginRequest); +} diff --git a/app/src/main/java/core/notevault/sync/auth/LoginCallback.java b/app/src/main/java/core/notevault/sync/auth/LoginCallback.java new file mode 100644 index 0000000..565944f --- /dev/null +++ b/app/src/main/java/core/notevault/sync/auth/LoginCallback.java @@ -0,0 +1,7 @@ +package core.notevault.sync.auth; + +public interface LoginCallback { + + void onSuccess(); + void onError(String error); +} diff --git a/app/src/main/java/core/notevault/sync/auth/LoginRequest.java b/app/src/main/java/core/notevault/sync/auth/LoginRequest.java new file mode 100644 index 0000000..6465d72 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/auth/LoginRequest.java @@ -0,0 +1,13 @@ +package core.notevault.sync.auth; + +public class LoginRequest { + private String username; + private String password; + + public LoginRequest(String username, String password) { + this.username = username; + this.password = password; + } + + +} diff --git a/app/src/main/java/core/notevault/sync/auth/LoginResponse.java b/app/src/main/java/core/notevault/sync/auth/LoginResponse.java new file mode 100644 index 0000000..e9a1330 --- /dev/null +++ b/app/src/main/java/core/notevault/sync/auth/LoginResponse.java @@ -0,0 +1,22 @@ +package core.notevault.sync.auth; + +public class LoginResponse { + private String token; + private String username; + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } +} diff --git a/app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java b/app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java new file mode 100644 index 0000000..4c1c0e0 --- /dev/null +++ b/app/src/main/java/core/notevault/ui/login/LoginCallBackImpl.java @@ -0,0 +1,25 @@ +package core.notevault.ui.login; + +import android.content.Context; +import android.util.Log; +import android.widget.Toast; +import core.notevault.sync.auth.LoginCallback; + +public class LoginCallBackImpl implements LoginCallback { + private Context context; + + public LoginCallBackImpl(Context context) { + this.context = context; + } + + @Override + public void onSuccess() { + Toast.makeText(context, "Login successfull", Toast.LENGTH_SHORT).show(); + } + + @Override + public void onError(String error) { + Log.d("Login", error); + Toast.makeText(context, "Login not successfull: " + error, Toast.LENGTH_LONG).show(); + } +} diff --git a/app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java b/app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java new file mode 100644 index 0000000..07ed33d --- /dev/null +++ b/app/src/main/java/core/notevault/ui/login/LoginDialogFragment.java @@ -0,0 +1,41 @@ +package core.notevault.ui.login; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.EditText; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.DialogFragment; +import core.notevault.R; +import core.notevault.sync.auth.AuthRepository; + +public class LoginDialogFragment extends DialogFragment { + + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + LayoutInflater inflater = requireActivity().getLayoutInflater(); + View loginView = inflater.inflate(R.layout.fragment_login_dialog, null); // Ersetze 'your_dialog_layout' durch deinen Dateinamen + + EditText usernameInput = loginView.findViewById(R.id.editTextUsername); + EditText passwordInput = loginView.findViewById(R.id.editTextPassword); + + AuthRepository authRepository = new AuthRepository(this.getContext()); + + return new AlertDialog.Builder(requireContext()) + .setView(loginView) + .setPositiveButton("Speichern", (dialog, which) -> { + String username = usernameInput.getText().toString(); + String password = passwordInput.getText().toString(); + + authRepository.performLogin(username, password, new LoginCallBackImpl(this.getContext())); + } ) + .setNegativeButton("Abbrechen", (dialog, which) -> {} ) + .create(); + } + + +} diff --git a/app/src/main/res/drawable/login.xml b/app/src/main/res/drawable/login.xml new file mode 100644 index 0000000..2e5aa22 --- /dev/null +++ b/app/src/main/res/drawable/login.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/fragment_login_dialog.xml b/app/src/main/res/layout/fragment_login_dialog.xml new file mode 100644 index 0000000..cd95cea --- /dev/null +++ b/app/src/main/res/layout/fragment_login_dialog.xml @@ -0,0 +1,21 @@ + + + + + + + + diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml index 5960867..4c6e013 100644 --- a/app/src/main/res/menu/main.xml +++ b/app/src/main/res/menu/main.xml @@ -1,8 +1,9 @@ - - - \ No newline at end of file + + + +