Guest User

Untitled

a guest
Jun 6th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. private static final String TAG = "EmailPassword";
  2.  
  3. private TextView mStatusTextView;
  4. private TextView mDetailTextView;
  5. private EditText mEmailField;
  6. private EditText mPasswordField;
  7.  
  8. // [START declare_auth]
  9. private FirebaseAuth mAuth;
  10. // [END declare_auth]
  11.  
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_emailpassword);
  16.  
  17. // Views
  18. mStatusTextView = findViewById(R.id.status);
  19. mDetailTextView = findViewById(R.id.detail);
  20. mEmailField = findViewById(R.id.field_email);
  21. mPasswordField = findViewById(R.id.field_password);
  22.  
  23. // Buttons
  24. findViewById(R.id.email_sign_in_button).setOnClickListener(this);
  25. findViewById(R.id.email_create_account_button).setOnClickListener(this);
  26. findViewById(R.id.sign_out_button).setOnClickListener(this);
  27. findViewById(R.id.verify_email_button).setOnClickListener(this);
  28.  
  29. // [START initialize_auth]
  30. mAuth = FirebaseAuth.getInstance();
  31. // [END initialize_auth]
  32. }
  33.  
  34. // [START on_start_check_user]
  35. @Override
  36. public void onStart() {
  37. super.onStart();
  38. // Check if user is signed in (non-null) and update UI accordingly.
  39. FirebaseUser currentUser = mAuth.getCurrentUser();
  40. updateUI(currentUser);
  41. }
  42. // [END on_start_check_user]
  43.  
  44. private void createAccount(String email, String password) {
  45. Log.d(TAG, "createAccount:" + email);
  46. if (!validateForm()) {
  47. return;
  48. }
  49.  
  50. showProgressDialog();
  51.  
  52. // [START create_user_with_email]
  53. mAuth.createUserWithEmailAndPassword(email, password)
  54. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  55. @Override
  56. public void onComplete(@NonNull Task<AuthResult> task) {
  57. if (task.isSuccessful()) {
  58. // Sign in success, update UI with the signed-in user's information
  59. Log.d(TAG, "createUserWithEmail:success");
  60. FirebaseUser user = mAuth.getCurrentUser();
  61. updateUI(user);
  62. } else {
  63. // If sign in fails, display a message to the user.
  64. Log.w(TAG, "createUserWithEmail:failure", task.getException());
  65. Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
  66. Toast.LENGTH_SHORT).show();
  67. updateUI(null);
  68. }
  69.  
  70. // [START_EXCLUDE]
  71. hideProgressDialog();
  72. // [END_EXCLUDE]
  73. }
  74. });
  75. // [END create_user_with_email]
  76. }
  77.  
  78. private void signIn(String email, String password) {
  79. Log.d(TAG, "signIn:" + email);
  80. if (!validateForm()) {
  81. return;
  82. }
  83.  
  84. showProgressDialog();
  85.  
  86. // [START sign_in_with_email]
  87. mAuth.signInWithEmailAndPassword(email, password)
  88. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  89. @Override
  90. public void onComplete(@NonNull Task<AuthResult> task) {
  91. if (task.isSuccessful()) {
  92. // Sign in success, update UI with the signed-in user's information
  93. Log.d(TAG, "signInWithEmail:success");
  94. FirebaseUser user = mAuth.getCurrentUser();
  95. updateUI(user);
  96. } else {
  97. // If sign in fails, display a message to the user.
  98. Log.w(TAG, "signInWithEmail:failure", task.getException());
  99. Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
  100. Toast.LENGTH_SHORT).show();
  101. updateUI(null);
  102. }
  103.  
  104. // [START_EXCLUDE]
  105. if (!task.isSuccessful()) {
  106. mStatusTextView.setText(R.string.auth_failed);
  107. }
  108. hideProgressDialog();
  109. // [END_EXCLUDE]
  110. }
  111. });
  112. // [END sign_in_with_email]
  113. }
  114.  
  115. private void signOut() {
  116. mAuth.signOut();
  117. updateUI(null);
  118. }
  119.  
  120. private void sendEmailVerification() {
  121. // Disable button
  122. findViewById(R.id.verify_email_button).setEnabled(false);
  123.  
  124. // Send verification email
  125. // [START send_email_verification]
  126. final FirebaseUser user = mAuth.getCurrentUser();
  127. user.sendEmailVerification()
  128. .addOnCompleteListener(this, new OnCompleteListener<Void>() {
  129. @Override
  130. public void onComplete(@NonNull Task<Void> task) {
  131. // [START_EXCLUDE]
  132. // Re-enable button
  133. findViewById(R.id.verify_email_button).setEnabled(true);
  134.  
  135. if (task.isSuccessful()) {
  136. Toast.makeText(EmailPasswordActivity.this,
  137. "Verification email sent to " + user.getEmail(),
  138. Toast.LENGTH_SHORT).show();
  139. } else {
  140. Log.e(TAG, "sendEmailVerification", task.getException());
  141. Toast.makeText(EmailPasswordActivity.this,
  142. "Failed to send verification email.",
  143. Toast.LENGTH_SHORT).show();
  144. }
  145. // [END_EXCLUDE]
  146. }
  147. });
  148. // [END send_email_verification]
  149. }
  150.  
  151. private boolean validateForm() {
  152. boolean valid = true;
  153.  
  154. String email = mEmailField.getText().toString();
  155. if (TextUtils.isEmpty(email)) {
  156. mEmailField.setError("Required.");
  157. valid = false;
  158. } else {
  159. mEmailField.setError(null);
  160. }
  161.  
  162. String password = mPasswordField.getText().toString();
  163. if (TextUtils.isEmpty(password)) {
  164. mPasswordField.setError("Required.");
  165. valid = false;
  166. } else {
  167. mPasswordField.setError(null);
  168. }
  169.  
  170. return valid;
  171. }
  172.  
  173. private void updateUI(FirebaseUser user) {
  174. hideProgressDialog();
  175. if (user != null) {
  176. mStatusTextView.setText(getString(R.string.emailpassword_status_fmt,
  177. user.getEmail(), user.isEmailVerified()));
  178. mDetailTextView.setText(getString(R.string.firebase_status_fmt, user.getUid()));
  179.  
  180. findViewById(R.id.email_password_buttons).setVisibility(View.GONE);
  181. findViewById(R.id.email_password_fields).setVisibility(View.GONE);
  182. findViewById(R.id.signed_in_buttons).setVisibility(View.VISIBLE);
  183.  
  184. findViewById(R.id.verify_email_button).setEnabled(!user.isEmailVerified());
  185. } else {
  186. mStatusTextView.setText(R.string.signed_out);
  187. mDetailTextView.setText(null);
  188.  
  189. findViewById(R.id.email_password_buttons).setVisibility(View.VISIBLE);
  190. findViewById(R.id.email_password_fields).setVisibility(View.VISIBLE);
  191. findViewById(R.id.signed_in_buttons).setVisibility(View.GONE);
  192. }
  193. }
  194.  
  195. @Override
  196. public void onClick(View v) {
  197. int i = v.getId();
  198. if (i == R.id.email_create_account_button) {
  199. createAccount(mEmailField.getText().toString(), mPasswordField.getText().toString());
  200. } else if (i == R.id.email_sign_in_button) {
  201. signIn(mEmailField.getText().toString(), mPasswordField.getText().toString());
  202. } else if (i == R.id.sign_out_button) {
  203. signOut();
  204. } else if (i == R.id.verify_email_button) {
  205. sendEmailVerification();
  206. }
  207.  
  208. }
Add Comment
Please, Sign In to add comment