Advertisement
Guest User

FireBase Auth

a guest
Mar 7th, 2019
431
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3.     private final static String TAG = "MainActivity";
  4.     private FirebaseAuth mAuth;
  5.  
  6.     private TextView text;
  7.     private EditText login;
  8.     private EditText pass;
  9.     private Button save;
  10.  
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.  
  16.         text = findViewById(R.id.text);
  17.         login = findViewById(R.id.login);
  18.         pass = findViewById(R.id.pass);
  19.         save = findViewById(R.id.save);
  20.  
  21.         save.setOnClickListener(new View.OnClickListener() {
  22.             @Override
  23.             public void onClick(View v) {
  24.                 performLoginOrAccountCreation(login.getText().toString(), pass.getText().toString());
  25.             }
  26.         });
  27.  
  28.         FirebaseApp.initializeApp(this);
  29.         mAuth = FirebaseAuth.getInstance();
  30.  
  31.     }
  32.  
  33.     @Override
  34.     protected void onStart() {
  35.         super.onStart();
  36.         FirebaseUser user = mAuth.getCurrentUser();
  37.         updateUI(user);
  38.     }
  39.  
  40.     private void performLoginOrAccountCreation(final String email, final String password){
  41.         mAuth.fetchSignInMethodsForEmail(email).addOnCompleteListener(
  42.                 this, new OnCompleteListener<SignInMethodQueryResult>() {
  43.                     @Override
  44.                     public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
  45.                         if (task.isSuccessful()) {
  46.  
  47.                             SignInMethodQueryResult result = task.getResult();
  48.                             if(result != null && result.getSignInMethods() != null && result.getSignInMethods().size() > 0){
  49.                                 signIn(email, password);
  50.                             }else{ createAccount(email, password); }
  51.  
  52.                         } else {
  53.                             Log.w(TAG, "User check failed", task.getException());
  54.                             Toast.makeText(MainActivity.this, "There is a problem, please try again later.", Toast.LENGTH_SHORT).show();
  55.                         }
  56.                     }
  57.                 });
  58.     }
  59.  
  60.     private void createAccount(String email, String password) {
  61.         mAuth.createUserWithEmailAndPassword(email, password)
  62.                 .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  63.                     @Override
  64.                     public void onComplete(@NonNull Task<AuthResult> task) {
  65.  
  66.                         FirebaseUser user = null;
  67.                         if (task.isSuccessful()) { user = mAuth.getCurrentUser(); }
  68.                         else {
  69.                             Log.w(TAG, "createUserWithEmail:failure", task.getException());
  70.                             Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
  71.                         }
  72.                         updateUI(user);
  73.  
  74.                     }
  75.                 });
  76.     }
  77.  
  78.     private void signIn(String email, String password) {
  79.         mAuth.signInWithEmailAndPassword(email, password)
  80.                 .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  81.                     @Override
  82.                     public void onComplete(@NonNull Task<AuthResult> task) {
  83.  
  84.                         FirebaseUser user = null;
  85.                         if (task.isSuccessful()) { user = mAuth.getCurrentUser(); }
  86.                         else {
  87.                             Log.w(TAG, "signInWithEmailAndPassword:failure", task.getException());
  88.                             Toast.makeText(MainActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show();
  89.                         }
  90.                         updateUI(user);
  91.  
  92.                     }
  93.                 });
  94.     }
  95.  
  96.     private void updateUI(FirebaseUser user) {
  97.         if(user == null) {
  98.             text.setText("NO USER");
  99.         }else{
  100.             text.setText(user.getEmail() + " authed");
  101.         }
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement