auth services

This commit is contained in:
Benedek László 2024-05-05 17:12:05 +02:00
parent 46c9b7a1f3
commit adc30fe90a
4 changed files with 132 additions and 30 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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<String, Void, Boolean> {
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<String, Void, Boolean> {
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<String, String> 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();
}
}
}
}

View File

@ -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);