Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. private FirebaseAuth mAuth;
  4.  
  5. String email = "gg@gmail.com";
  6. String password = "123456";
  7. String TAG = "LogTestLogin";
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. mAuth = FirebaseAuth.getInstance();
  14. createAccount();
  15. signIn();
  16. }
  17.  
  18. @Override
  19. public void onStart() {
  20. super.onStart();
  21. // Check if user is signed in (non-null) and update UI accordingly.
  22. FirebaseUser currentUser = mAuth.getCurrentUser();
  23. updateUI(currentUser);
  24. }
  25.  
  26. private void createAccount() {
  27. mAuth.createUserWithEmailAndPassword(email, password)
  28. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  29. @Override
  30. public void onComplete(@NonNull Task<AuthResult> task) {
  31. if (task.isSuccessful()) {
  32. // Sign in success, update UI with the signed-in user's information
  33. Log.d(TAG, "createUserWithEmail:success");
  34. FirebaseUser user = mAuth.getCurrentUser();
  35. updateUI(user);
  36. } else {
  37. // If sign in fails, display a message to the user.
  38. Log.w(TAG, "createUserWithEmail:failure", task.getException());
  39. Toast.makeText(MainActivity.this, "Authentication failed.",
  40. Toast.LENGTH_SHORT).show();
  41. updateUI(null);
  42. }
  43.  
  44. // ...
  45. }
  46. });
  47. }
  48.  
  49. private void signIn() {
  50. mAuth.signInWithEmailAndPassword(email, password)
  51. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  52. @Override
  53. public void onComplete(@NonNull Task<AuthResult> task) {
  54. if (task.isSuccessful()) {
  55. // Sign in success, update UI with the signed-in user's information
  56. Log.d(TAG, "signInWithEmail:success");
  57. FirebaseUser user = mAuth.getCurrentUser();
  58. updateUI(user);
  59. } else {
  60. // If sign in fails, display a message to the user.
  61. Log.w(TAG, "signInWithEmail:failure", task.getException());
  62. Toast.makeText(MainActivity.this, "Authentication failed.",
  63. Toast.LENGTH_SHORT).show();
  64. updateUI(null);
  65. }
  66.  
  67. // ...
  68. }
  69. });
  70. }
  71.  
  72. private void updateUI(FirebaseUser user) {
  73. // FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
  74. if (user != null) {
  75. // Name, email address, and profile photo Url
  76. String name = user.getDisplayName();
  77. String email = user.getEmail();
  78. Uri photoUrl = user.getPhotoUrl();
  79.  
  80. // Check if user's email is verified
  81. boolean emailVerified = user.isEmailVerified();
  82.  
  83. // The user's ID, unique to the Firebase project. Do NOT use this value to
  84. // authenticate with your backend server, if you have one. Use
  85. // FirebaseUser.getIdToken() instead.
  86. String uid = user.getUid();
  87. String str = "name: " + name +
  88. "\nemail: " + email +
  89. "\nuid: " + uid +
  90. "\nphotoUrl: " + photoUrl;
  91. Toast.makeText(MainActivity.this, str,
  92. Toast.LENGTH_SHORT).show();
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement