Advertisement
Guest User

Untitled

a guest
May 14th, 2019
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. package com.example.firebaselogin;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.annotation.NonNull;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.text.TextUtils;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.ProgressBar;
  12. import android.widget.Toast;
  13.  
  14. import com.google.android.gms.tasks.OnCompleteListener;
  15. import com.google.android.gms.tasks.Task;
  16. import com.google.firebase.auth.AuthResult;
  17. import com.google.firebase.auth.FirebaseAuth;
  18. import com.google.firebase.database.FirebaseDatabase;
  19.  
  20. public class SignUpActivity extends AppCompatActivity {
  21.  
  22. private EditText inputEmail, inputPassword, inputFname, inputLname, inputPhone;
  23. private Button btnSignIn, btnSignUp, btnResetPassword;
  24. private ProgressBar progressBar;
  25. private FirebaseAuth auth;
  26.  
  27. @Override
  28. protected void onCreate(Bundle savedInstanceState) {
  29. super.onCreate(savedInstanceState);
  30. setContentView(R.layout.activity_signup);
  31.  
  32. //Get Firebase auth instance
  33. auth = FirebaseAuth.getInstance();
  34.  
  35. btnSignIn = (Button) findViewById(R.id.sign_in_button);
  36. btnSignUp = (Button) findViewById(R.id.sign_up_button);
  37. inputEmail = (EditText) findViewById(R.id.email);
  38. inputPassword = (EditText) findViewById(R.id.password);
  39. inputFname = (EditText) findViewById(R.id.fname);
  40. inputLname = (EditText) findViewById(R.id.lname);
  41. inputPhone = (EditText) findViewById(R.id.phone);
  42. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  43. btnResetPassword = (Button) findViewById(R.id.btn_reset_password);
  44.  
  45. btnResetPassword.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. startActivity(new Intent(SignUpActivity.this, PasswordResetActivity.class));
  49. }
  50. });
  51.  
  52. btnSignIn.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. finish();
  56. }
  57. });
  58.  
  59. btnSignUp.setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62.  
  63. final String email = inputEmail.getText().toString().trim();
  64. final String password = inputPassword.getText().toString().trim();
  65. final String phone = inputPhone.getText().toString().trim();
  66. final String fname = inputFname.getText().toString().trim();
  67. final String lname = inputLname.getText().toString().trim();
  68.  
  69. if (TextUtils.isEmpty(email)) {
  70. Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
  71. return;
  72. }
  73.  
  74. if (TextUtils.isEmpty(password)) {
  75. Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
  76. return;
  77. }
  78.  
  79. if (password.length() < 6) {
  80. Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();
  81. return;
  82. }
  83.  
  84. progressBar.setVisibility(View.VISIBLE);
  85. //create user
  86. auth.createUserWithEmailAndPassword(email, password)
  87. .addOnCompleteListener(SignUpActivity.this, new OnCompleteListener<AuthResult>() {
  88. @Override
  89. public void onComplete(@NonNull Task<AuthResult> task) {
  90. Toast.makeText(SignUpActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
  91. progressBar.setVisibility(View.GONE);
  92. // If sign in fails, display a message to the user. If sign in succeeds
  93. // the auth state listener will be notified and logic to handle the
  94. // signed in user can be handled in the listener.
  95. if (!task.isSuccessful()) {
  96. Toast.makeText(SignUpActivity.this, "Authentication failed." + task.getException(),
  97. Toast.LENGTH_SHORT).show();
  98. } else {
  99.  
  100. addUserTodatabase(email, password, phone, fname, lname);
  101. startActivity(new Intent(SignUpActivity.this, MainActivity.class));
  102. finish();
  103. }
  104. }
  105. });
  106.  
  107. }
  108. });
  109. }
  110.  
  111. private void addUserTodatabase(String email, String password, String phone, String fname, String lname){
  112.  
  113. User user=new User();
  114.  
  115. user.setEmail(email);
  116. user.setPassword(password);
  117. user.setMobile(phone);
  118. user.setFname(fname);
  119. user.setLname(lname);
  120.  
  121. FirebaseDatabase.getInstance().getReference("users").push().setValue(user);
  122. }
  123.  
  124.  
  125. @Override
  126. protected void onResume() {
  127. super.onResume();
  128. progressBar.setVisibility(View.GONE);
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement