Advertisement
Guest User

Untitled

a guest
Mar 15th, 2020
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.23 KB | None | 0 0
  1. package com.example.foodtracker;
  2.  
  3. import android.content.Context;
  4. import android.os.Handler;
  5. import android.util.Log;
  6.  
  7. import androidx.annotation.NonNull;
  8.  
  9. import com.google.android.gms.tasks.OnCompleteListener;
  10. import com.google.android.gms.tasks.OnFailureListener;
  11. import com.google.android.gms.tasks.OnSuccessListener;
  12. import com.google.android.gms.tasks.Task;
  13. import com.google.firebase.auth.AuthResult;
  14. import com.google.firebase.auth.FirebaseAuth;
  15. import com.google.firebase.auth.FirebaseUser;
  16. import com.google.firebase.firestore.DocumentReference;
  17. import com.google.firebase.firestore.FirebaseFirestore;
  18.  
  19. import java.util.HashMap;
  20. import java.util.Map;
  21.  
  22. public class AuthentificationManager {
  23.     private static final String TAG = "AuthentificationManager";
  24.  
  25.  
  26.     private FirebaseAuth firebaseAuth;
  27.  
  28.  
  29.     private static AuthentificationManager singleInstance = null;
  30.  
  31.     private AuthentificationManager() {
  32.         firebaseAuth = FirebaseAuth.getInstance();
  33.     }
  34.  
  35.  
  36.  
  37.  
  38.     private void addUserToDatabase(String id, String email, String displayName, String favoriteFood) {
  39.         FirebaseFirestore database = FirebaseFirestore.getInstance();
  40.  
  41.         Map<String, String> user = new HashMap<>();
  42.         user.put("email", email);
  43.         user.put("displayName", displayName);
  44.         user.put("favoriteFood", favoriteFood);
  45.  
  46.         database.collection("users").document(id).set(user);
  47.  
  48.         /*database.collection("users")
  49.                 .add(user)
  50.                 .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
  51.                     @Override
  52.                     public void onSuccess(DocumentReference documentReference) {
  53.                         Log.d(TAG, "Document snapshot added with ID: " + documentReference.getId());
  54.                     }
  55.                 })
  56.                 .addOnFailureListener(new OnFailureListener() {
  57.                     @Override
  58.                     public void onFailure(@NonNull Exception e) {
  59.                         Log.w(TAG, "Error adding document", e);
  60.                     }
  61.                 }); */
  62.     }
  63.  
  64.  
  65.     public static AuthentificationManager getInstance() {
  66.         if (singleInstance == null) {
  67.             singleInstance = new AuthentificationManager();
  68.         }
  69.         return singleInstance;
  70.     }
  71.  
  72.  
  73.  
  74.     public void registerNewUser(Context context, final String email, final String displayName, String password, String confirmPassword,
  75.                                 final String favoriteFood) {
  76.         if (password.equals(confirmPassword)) {
  77.             firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(context.getMainExecutor(),
  78.                     new OnCompleteListener<AuthResult>() {
  79.                 @Override
  80.                 public void onComplete(@NonNull Task<AuthResult> task) {
  81.                     if (task.isSuccessful()) {
  82.                         Log.d(TAG, "createUserWithEmail : success");
  83.                     } else {
  84.                         Log.w(TAG, "createUserWithEmail : failure", task.getException());
  85.                     }
  86.                 }
  87.             });
  88.             if(firebaseAuth.getCurrentUser() != null) {
  89.                 addUserToDatabase(firebaseAuth.getCurrentUser().getUid(), email, displayName, favoriteFood);
  90.                 Log.d(TAG, "User != null");
  91.             }
  92.             else {
  93.                 Log.w(TAG, "User == null");
  94.             }
  95.         } else {
  96.             Log.w(TAG, "password trebuie sa coincida cu confirmPassword");
  97.         }
  98.     }
  99.  
  100.     public void signInUser(String email, String password) {
  101.         firebaseAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
  102.             @Override
  103.             public void onComplete(@NonNull Task<AuthResult> task) {
  104.                 if (task.isSuccessful()) {
  105.                     Log.d(TAG, "signInWithEmail : success");
  106.                 } else {
  107.                     Log.w(TAG, "signInWithEmail : failure", task.getException());
  108.                 }
  109.             }
  110.         });
  111.     }
  112.  
  113.     public void signOutUser() {
  114.         firebaseAuth.signOut();
  115.     }
  116.  
  117.     public FirebaseUser getCurrentUser() {
  118.         return firebaseAuth.getCurrentUser();
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement