Registration + Login
This commit is contained in:
parent
0d9002776c
commit
71568a200f
@ -31,7 +31,9 @@ import core.notevault.ui.gallery.GalleryFragment;
|
||||
import core.notevault.ui.gallery.detail.ConcertSongSelector;
|
||||
import core.notevault.ui.gallery.editor.ConcertEditorDialog;
|
||||
import core.notevault.ui.home.HomeFragment;
|
||||
import core.notevault.ui.login.LoginCallBackImpl;
|
||||
import core.notevault.ui.login.LoginDialogFragment;
|
||||
import core.notevault.ui.login.RegisterCallback;
|
||||
import core.notevault.ui.metadatadialog.MetaDataDialog;
|
||||
import core.notevault.util.NoteSheetsUtil;
|
||||
|
||||
@ -129,7 +131,7 @@ public class MainActivity extends AppCompatActivity implements MetaDataDialog.On
|
||||
}
|
||||
|
||||
private void showLoginDialog() {
|
||||
LoginDialogFragment loginDialogFragment = new LoginDialogFragment();
|
||||
LoginDialogFragment loginDialogFragment = LoginDialogFragment.newInstance(new LoginCallBackImpl(this), new RegisterCallback(this));
|
||||
loginDialogFragment.show(getSupportFragmentManager(), "login");
|
||||
}
|
||||
|
||||
|
7
app/src/main/java/core/notevault/sync/APICallback.java
Normal file
7
app/src/main/java/core/notevault/sync/APICallback.java
Normal file
@ -0,0 +1,7 @@
|
||||
package core.notevault.sync;
|
||||
|
||||
public interface APICallback {
|
||||
|
||||
void onSuccess();
|
||||
void onError(String error);
|
||||
}
|
@ -24,7 +24,7 @@ public class ApiClient {
|
||||
.build();
|
||||
|
||||
retrofit = new Retrofit.Builder()
|
||||
.baseUrl("http://192.168.178.30:8080/")
|
||||
.baseUrl("http://192.168.178.30:8000/")
|
||||
.client(client)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
|
14
app/src/main/java/core/notevault/sync/StatusResponse.java
Normal file
14
app/src/main/java/core/notevault/sync/StatusResponse.java
Normal file
@ -0,0 +1,14 @@
|
||||
package core.notevault.sync;
|
||||
|
||||
public class StatusResponse {
|
||||
|
||||
private String status;
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
@ -1,8 +1,13 @@
|
||||
package core.notevault.sync.auth;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import core.notevault.sync.APICallback;
|
||||
import core.notevault.sync.ApiClient;
|
||||
import core.notevault.sync.StatusResponse;
|
||||
import core.notevault.sync.auth.apimodel.LoginRequest;
|
||||
import core.notevault.sync.auth.apimodel.LoginResponse;
|
||||
import core.notevault.sync.auth.apimodel.RegisterRequest;
|
||||
import core.notevault.sync.auth.apimodel.RegisterResponse;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
@ -16,8 +21,8 @@ public class AuthRepository {
|
||||
this.tokenManager = new TokenManager(context);
|
||||
}
|
||||
|
||||
public void performLogin(String username, String password, LoginCallback callback) {
|
||||
LoginRequest loginRequest = new LoginRequest(username, password);
|
||||
public void performLogin(String email, String password, APICallback callback) {
|
||||
LoginRequest loginRequest = new LoginRequest(email, password);
|
||||
|
||||
authService.login(loginRequest).enqueue(new Callback<LoginResponse>() {
|
||||
@Override
|
||||
@ -41,6 +46,26 @@ public class AuthRepository {
|
||||
});
|
||||
}
|
||||
|
||||
public void performRegistration(String email, String username, String password, APICallback callback) {
|
||||
RegisterRequest registerRequest = new RegisterRequest(username, password, email);
|
||||
authService.registration(registerRequest).enqueue(new Callback<StatusResponse>() {
|
||||
|
||||
@Override
|
||||
public void onResponse(Call<StatusResponse> call, Response<StatusResponse> response) {
|
||||
if(response.isSuccessful() && response.body() != null) {
|
||||
callback.onSuccess();
|
||||
} else {
|
||||
callback.onError("Registration fehlgeschlagen. Überprüfe Benutzername und Passwort.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<StatusResponse> call, Throwable throwable) {
|
||||
callback.onError("Netzwerkfehler: " + throwable.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void saveToken(String token) {
|
||||
tokenManager.saveToken(token);
|
||||
}
|
||||
|
@ -1,11 +1,20 @@
|
||||
package core.notevault.sync.auth;
|
||||
|
||||
import core.notevault.sync.StatusResponse;
|
||||
import core.notevault.sync.auth.apimodel.LoginRequest;
|
||||
import core.notevault.sync.auth.apimodel.LoginResponse;
|
||||
import core.notevault.sync.auth.apimodel.RegisterRequest;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.Headers;
|
||||
import retrofit2.http.POST;
|
||||
|
||||
public interface AuthService {
|
||||
|
||||
@POST("api/v1/auth/login")
|
||||
@POST("/login/")
|
||||
Call<LoginResponse> login(@Body LoginRequest loginRequest);
|
||||
|
||||
@POST("/register/")
|
||||
@Headers("Content-Type: application/json")
|
||||
Call<StatusResponse> registration(@Body RegisterRequest registerRequest);
|
||||
}
|
||||
|
@ -1,13 +0,0 @@
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package core.notevault.sync.auth.apimodel;
|
||||
|
||||
public class LoginRequest {
|
||||
private String email;
|
||||
private String password;
|
||||
|
||||
public LoginRequest(String email, String password) {
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package core.notevault.sync.auth;
|
||||
package core.notevault.sync.auth.apimodel;
|
||||
|
||||
public class LoginResponse {
|
||||
private String token;
|
@ -0,0 +1,14 @@
|
||||
package core.notevault.sync.auth.apimodel;
|
||||
|
||||
public class RegisterRequest {
|
||||
private String username;
|
||||
private String email;
|
||||
private String password;
|
||||
|
||||
public RegisterRequest(String username, String password, String email) {
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package core.notevault.sync.auth.apimodel;
|
||||
|
||||
public class RegisterResponse {
|
||||
|
||||
private String username;
|
||||
private String email;
|
||||
|
||||
public RegisterResponse(String username, String email) {
|
||||
this.username = username;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public RegisterResponse() {
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
}
|
@ -3,9 +3,10 @@ package core.notevault.ui.login;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
import core.notevault.sync.APICallback;
|
||||
import core.notevault.sync.auth.LoginCallback;
|
||||
|
||||
public class LoginCallBackImpl implements LoginCallback {
|
||||
public class LoginCallBackImpl implements APICallback {
|
||||
private Context context;
|
||||
|
||||
public LoginCallBackImpl(Context context) {
|
||||
|
@ -6,41 +6,44 @@ import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
import core.notevault.R;
|
||||
import core.notevault.sync.APICallback;
|
||||
import core.notevault.sync.auth.AuthRepository;
|
||||
import core.notevault.sync.auth.LoginCallback;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
|
||||
public class LoginDialogFragment extends DialogFragment {
|
||||
private LoginCallback loginCallback;
|
||||
private APICallback loginCallback;
|
||||
private APICallback registerCallback;
|
||||
|
||||
private boolean isLoginMode = true; // Default mode
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
if (context instanceof LoginCallback) {
|
||||
loginCallback = (LoginCallback) context;
|
||||
} else {
|
||||
throw new RuntimeException(context.toString() + " must implement LoginDialogListener");
|
||||
if (loginCallback == null || registerCallback == null) {
|
||||
throw new IllegalStateException("LoginDialogFragment must be created using newInstance()");
|
||||
}
|
||||
}
|
||||
|
||||
public static LoginDialogFragment newInstance(APICallback loginCallback, APICallback registerCallback) {
|
||||
LoginDialogFragment fragment = new LoginDialogFragment();
|
||||
fragment.loginCallback = loginCallback;
|
||||
fragment.registerCallback = registerCallback;
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@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())
|
||||
/*return new AlertDialog.Builder(requireContext())
|
||||
.setView(loginView)
|
||||
.setPositiveButton("Speichern", (dialog, which) -> {
|
||||
String username = usernameInput.getText().toString();
|
||||
@ -49,7 +52,48 @@ public class LoginDialogFragment extends DialogFragment {
|
||||
authRepository.performLogin(username, password, loginCallback);
|
||||
} )
|
||||
.setNegativeButton("Abbrechen", (dialog, which) -> {} )
|
||||
.create();
|
||||
.create();*/
|
||||
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
|
||||
LayoutInflater inflater = requireActivity().getLayoutInflater();
|
||||
View view = inflater.inflate(R.layout.fragment_login_dialog, null);
|
||||
|
||||
TextView textViewTitle = view.findViewById(R.id.textViewTitle);
|
||||
EditText editTextUsername = view.findViewById(R.id.editTextUsername);
|
||||
EditText editTextEmail = view.findViewById(R.id.editTextEmail);
|
||||
EditText editTextPassword = view.findViewById(R.id.editTextPassword);
|
||||
Button buttonAction = view.findViewById(R.id.buttonAction);
|
||||
TextView textViewSwitch = view.findViewById(R.id.textViewSwitch);
|
||||
|
||||
// Handle action button click
|
||||
buttonAction.setOnClickListener(v -> {
|
||||
if (isLoginMode) {
|
||||
// Handle login logic
|
||||
String email = editTextEmail.getText().toString();
|
||||
String password = editTextPassword.getText().toString();
|
||||
AuthRepository authRepository = new AuthRepository(this.getContext());
|
||||
authRepository.performLogin(email, password, loginCallback);
|
||||
} else {
|
||||
// Handle registration logic
|
||||
String email = editTextEmail.getText().toString();
|
||||
String password = editTextPassword.getText().toString();
|
||||
String username = editTextUsername.getText().toString();
|
||||
AuthRepository authRepository = new AuthRepository(this.getContext());
|
||||
authRepository.performRegistration(email, username, password, registerCallback);
|
||||
}
|
||||
});
|
||||
|
||||
// Toggle between Login and Registration
|
||||
textViewSwitch.setOnClickListener(v -> {
|
||||
isLoginMode = !isLoginMode;
|
||||
textViewTitle.setText(isLoginMode ? "Login" : "Register");
|
||||
editTextUsername.setVisibility(isLoginMode ? View.GONE : View.VISIBLE);
|
||||
buttonAction.setText(isLoginMode ? "Login" : "Register");
|
||||
textViewSwitch.setText(isLoginMode ? "Don't have an account? Register" : "Already have an account? Login");
|
||||
});
|
||||
|
||||
builder.setView(view);
|
||||
return builder.create();
|
||||
}
|
||||
|
||||
|
||||
|
@ -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.APICallback;
|
||||
|
||||
public class RegisterCallback implements APICallback {
|
||||
private Context context;
|
||||
|
||||
public RegisterCallback(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess() {
|
||||
Toast.makeText(context, "Registration 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();
|
||||
}
|
||||
}
|
6
app/src/main/res/drawable/dialog_background.xml
Normal file
6
app/src/main/res/drawable/dialog_background.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<solid android:color="@android:color/white" />
|
||||
<corners android:radius="16dp" />
|
||||
<padding android:left="16dp" android:top="16dp" android:right="16dp" android:bottom="16dp" />
|
||||
<elevation android:height="4dp" />
|
||||
</shape>
|
@ -1,21 +1,68 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- res/layout/dialog_login.xml -->
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
android:padding="16dp"
|
||||
android:background="@drawable/dialog_background"
|
||||
android:layout_gravity="center_horizontal">
|
||||
|
||||
<!-- Title -->
|
||||
<TextView
|
||||
android:id="@+id/textViewTitle"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Login"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
android:gravity="center"
|
||||
android:paddingBottom="16dp" />
|
||||
|
||||
<!-- Username Field (Visible only in Registration mode) -->
|
||||
<EditText
|
||||
android:id="@+id/editTextUsername"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Username" />
|
||||
android:hint="Username"
|
||||
android:visibility="gone" />
|
||||
|
||||
<!-- Email Field -->
|
||||
<EditText
|
||||
android:id="@+id/editTextEmail"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Email"
|
||||
android:inputType="textEmailAddress" />
|
||||
|
||||
<!-- Password Field -->
|
||||
<EditText
|
||||
android:id="@+id/editTextPassword"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Password"
|
||||
android:inputType="textPassword" />
|
||||
|
||||
<!-- Action Button -->
|
||||
<Button
|
||||
android:id="@+id/buttonAction"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Login"
|
||||
android:backgroundTint="?attr/colorPrimary"
|
||||
android:textColor="@android:color/white"
|
||||
android:layout_marginTop="16dp" />
|
||||
|
||||
<!-- Toggle Login/Registration -->
|
||||
<TextView
|
||||
android:id="@+id/textViewSwitch"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Don't have an account? Register"
|
||||
android:gravity="center"
|
||||
android:textColor="?attr/colorPrimary"
|
||||
android:layout_marginTop="8dp"
|
||||
android:padding="8dp"
|
||||
android:clickable="true"
|
||||
android:focusable="true" />
|
||||
|
||||
</LinearLayout>
|
||||
|
Loading…
Reference in New Issue
Block a user