From adc30fe90aeb9b67a51d79d7c4474fca4c403947 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20L=C3=A1szl=C3=B3?= Date: Sun, 5 May 2024 17:12:05 +0200 Subject: [PATCH] auth services --- .../java/com/dowerx/quack/MainActivity.java | 29 +++--- .../com/dowerx/quack/RegisterActivity.java | 39 ++++---- .../com/dowerx/quack/service/AuthService.java | 92 +++++++++++++++++++ .../com/dowerx/quack/service/PostService.java | 2 +- 4 files changed, 132 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/dowerx/quack/MainActivity.java b/app/src/main/java/com/dowerx/quack/MainActivity.java index 68d9a24..b3f4968 100644 --- a/app/src/main/java/com/dowerx/quack/MainActivity.java +++ b/app/src/main/java/com/dowerx/quack/MainActivity.java @@ -12,11 +12,14 @@ import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import com.dowerx.quack.service.AuthService; import com.google.firebase.auth.FirebaseAuth; +import java.util.concurrent.ExecutionException; + public class MainActivity extends AppCompatActivity { - private FirebaseAuth fbs; +// private FirebaseAuth fbs; @Override protected void onCreate(Bundle savedInstanceState) { @@ -29,7 +32,7 @@ public class MainActivity extends AppCompatActivity { return insets; }); - fbs = FirebaseAuth.getInstance(); +// fbs = FirebaseAuth.getInstance(); } public void login(View view) { @@ -39,16 +42,18 @@ public class MainActivity extends AppCompatActivity { if (email.isEmpty() || password.isEmpty()) return; - fbs.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, task -> { - if (task.isSuccessful()) { - startActivity(new Intent(this, SwitcherActivity.class)); - } else { - Toast toast = new Toast(this); - toast.setText("login failed"); - toast.setDuration(Toast.LENGTH_LONG); - toast.show(); - } - }); + AuthService.Login loginTask = new AuthService.Login(this); + loginTask.execute(email, password); +// fbs.signInWithEmailAndPassword(email, password).addOnCompleteListener(this, task -> { +// if (task.isSuccessful()) { +// startActivity(new Intent(this, SwitcherActivity.class)); +// } else { +// Toast toast = new Toast(this); +// toast.setText("login failed"); +// toast.setDuration(Toast.LENGTH_LONG); +// toast.show(); +// } +// }); } public void redirect_register(View view) { diff --git a/app/src/main/java/com/dowerx/quack/RegisterActivity.java b/app/src/main/java/com/dowerx/quack/RegisterActivity.java index 59785bc..123f3ac 100644 --- a/app/src/main/java/com/dowerx/quack/RegisterActivity.java +++ b/app/src/main/java/com/dowerx/quack/RegisterActivity.java @@ -12,11 +12,12 @@ import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; +import com.dowerx.quack.service.AuthService; import com.google.firebase.auth.FirebaseAuth; public class RegisterActivity extends AppCompatActivity { - private FirebaseAuth fbs; +// private FirebaseAuth fbs; @Override protected void onCreate(Bundle savedInstanceState) { @@ -29,15 +30,16 @@ public class RegisterActivity extends AppCompatActivity { return insets; }); - fbs = FirebaseAuth.getInstance(); +// fbs = FirebaseAuth.getInstance(); } public void register(View view) { String email = String.valueOf(((EditText)findViewById(R.id.email)).getText()); + String username = String.valueOf(((EditText)findViewById(R.id.username)).getText()); String password = String.valueOf(((EditText)findViewById(R.id.password)).getText()); String rePassword = String.valueOf(((EditText)findViewById(R.id.repeat_password)).getText()); - if (email.isEmpty() || password.isEmpty() || rePassword.isEmpty()) + if (email.isEmpty() || password.isEmpty() || rePassword.isEmpty() || username.isEmpty()) return; if (!password.equals(rePassword)) { @@ -48,20 +50,23 @@ public class RegisterActivity extends AppCompatActivity { return; } - fbs.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, task -> { - if (task.isSuccessful()) { - Toast toast = new Toast(this); - toast.setText("succesful register"); - toast.setDuration(Toast.LENGTH_LONG); - toast.show(); - startActivity(new Intent(this, MainActivity.class)); - } else { - Toast toast = new Toast(this); - toast.setText("login failed"); - toast.setDuration(Toast.LENGTH_LONG); - toast.show(); - } - }); + AuthService.Register task = new AuthService.Register(this); + task.execute(email, password, username); + +// fbs.createUserWithEmailAndPassword(email, password).addOnCompleteListener(this, task -> { +// if (task.isSuccessful()) { +// Toast toast = new Toast(this); +// toast.setText("succesful register"); +// toast.setDuration(Toast.LENGTH_LONG); +// toast.show(); +// startActivity(new Intent(this, MainActivity.class)); +// } else { +// Toast toast = new Toast(this); +// toast.setText("login failed"); +// toast.setDuration(Toast.LENGTH_LONG); +// toast.show(); +// } +// }); } public void redirect_login(View view) { diff --git a/app/src/main/java/com/dowerx/quack/service/AuthService.java b/app/src/main/java/com/dowerx/quack/service/AuthService.java index 8cbd26d..6d3c720 100644 --- a/app/src/main/java/com/dowerx/quack/service/AuthService.java +++ b/app/src/main/java/com/dowerx/quack/service/AuthService.java @@ -1,4 +1,96 @@ package com.dowerx.quack.service; +import android.app.Activity; +import android.content.Intent; +import android.os.AsyncTask; +import android.widget.Toast; + +import com.dowerx.quack.MainActivity; +import com.dowerx.quack.SwitcherActivity; +import com.google.android.gms.tasks.Tasks; +import com.google.firebase.auth.AuthResult; +import com.google.firebase.auth.FirebaseAuth; +import com.google.firebase.firestore.FirebaseFirestore; + +import java.util.HashMap; +import java.util.concurrent.ExecutionException; + public class AuthService { + // email, password + public static class Login extends AsyncTask { + Activity activity; + + public Login(Activity activity) { + this.activity = activity; + } + + @Override + protected Boolean doInBackground(String... loginDetails) { + FirebaseAuth fba = FirebaseAuth.getInstance(); + try { + Tasks.await(fba.signInWithEmailAndPassword(loginDetails[0], loginDetails[1])); + return true; + } catch (ExecutionException | InterruptedException e) { + return false; + } + } + + @Override + protected void onPostExecute(Boolean result) { + + if (result) { + activity.startActivity(new Intent(activity, SwitcherActivity.class)); + } else { + Toast toast = new Toast(activity.getBaseContext()); + toast.setText("login failed"); + toast.setDuration(Toast.LENGTH_LONG); + toast.show(); + } + } + } + + // email, password, username + public static class Register extends AsyncTask { + Activity activity; + + public Register(Activity activity) { + this.activity = activity; + } + + @Override + protected Boolean doInBackground(String... loginDetails) { + FirebaseAuth fba = FirebaseAuth.getInstance(); + try { + AuthResult result = Tasks.await(fba.createUserWithEmailAndPassword(loginDetails[0], loginDetails[1])); + + FirebaseFirestore ffs = FirebaseFirestore.getInstance(); + HashMap data = new HashMap<>(); + data.put("email", loginDetails[0]); + data.put("username", loginDetails[2]); + data.put("picture", "/assets/placeholder-profile-picture.png"); + + ffs.collection("user").document(loginDetails[2]).set(data); + + return true; + } catch (ExecutionException | InterruptedException e) { + return false; + } + } + + @Override + protected void onPostExecute(Boolean result) { + if (result) { + Toast toast = new Toast(activity.getBaseContext()); + toast.setText("succesful register"); + toast.setDuration(Toast.LENGTH_LONG); + toast.show(); + activity.startActivity(new Intent(activity, MainActivity.class)); + } else { + Toast toast = new Toast(activity.getBaseContext()); + toast.setText("register failed"); + toast.setDuration(Toast.LENGTH_LONG); + toast.show(); + } + } + } } diff --git a/app/src/main/java/com/dowerx/quack/service/PostService.java b/app/src/main/java/com/dowerx/quack/service/PostService.java index ded86c1..541e44d 100644 --- a/app/src/main/java/com/dowerx/quack/service/PostService.java +++ b/app/src/main/java/com/dowerx/quack/service/PostService.java @@ -39,7 +39,7 @@ public class PostService { FirebaseFirestore db = FirebaseFirestore.getInstance(); try { - for (QueryDocumentSnapshot post : Tasks.await(db.collection("/post").get())) { + for (QueryDocumentSnapshot post : Tasks.await(db.collection("/post").limit(10).get())) { DocumentSnapshot user = Tasks.await(post.getDocumentReference("user").get()); String username = user.get("username", String.class); String userid = user.get("id", String.class);