Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
  2. Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
  3.  
  4. AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
  5. firebaseAuth.signInWithCredential(credential)
  6. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  7. @Override
  8. public void onComplete(@NonNull Task<AuthResult> task) {
  9. if (task.isSuccessful()) {
  10. try {
  11. sleep(1000);
  12. } catch (InterruptedException e) {
  13. e.printStackTrace();
  14. }
  15. // Sign in success, update UI with the signed-in user's information
  16. Log.d(TAG, "signInWithCredential:success");
  17. FirebaseUser user = firebaseAuth.getCurrentUser();
  18. databaseReference = FirebaseDatabase.getInstance().getReference().child("users").child(user.getUid());
  19. databaseReference.child("fullName").setValue(user.getDisplayName());
  20. databaseReference.child("email").setValue(user.getEmail());
  21. databaseReference.child("profile").setValue(user.getPhotoUrl() != null ? user.getPhotoUrl().toString() : null);
  22. databaseReference.addValueEventListener(new ValueEventListener() {
  23. @Override
  24. public void onDataChange(DataSnapshot dataSnapshot) {
  25. Profile profile = dataSnapshot.getValue(Profile.class);
  26. if (dataSnapshot.exists()) {
  27. if (profile.getUserType() != null) {
  28. Log.e("key", dataSnapshot.getKey());
  29. SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(SignInActivity.this);
  30. SharedPreferences.Editor editor = sharedPreferences.edit();
  31. if (profile != null) {
  32. editor.putString(Preferences.EMAIL, profile.getEmail());
  33. editor.putString(Preferences.NAME, profile.getFullName());
  34. editor.putString(Preferences.USER_TYPE, profile.getUserType());
  35. }
  36. editor.putString(Preferences.USERID, getUid());
  37. editor.apply();
  38.  
  39. Intent loginIntent = new Intent(SignInActivity.this, MainMenuActivity.class);
  40. try {
  41. sleep(1000);
  42. } catch (InterruptedException e) {
  43. e.printStackTrace();
  44. }
  45. startActivity(loginIntent);
  46. finish();
  47. } else {
  48. Intent contactIntent = new Intent(SignInActivity.this, ContactDetailsActivity.class);
  49. contactIntent.putExtra("userEmailAddress", profile.getEmail());
  50. startActivity(contactIntent);
  51. finish();
  52. }
  53. } else {
  54. Toast.makeText(SignInActivity.this, "Snapshot not yet saved", Toast.LENGTH_SHORT).show();
  55. }
  56. Log.e("key", dataSnapshot.getKey());
  57. }
  58.  
  59. @Override
  60. public void onCancelled(DatabaseError databaseError) {
  61.  
  62. }
  63. });
  64. //updateUI(user);
  65. } else {
  66. // If sign in fails, display a message to the user.
  67. Log.w(TAG, "signInWithCredential:failure", task.getException());
  68. Toast.makeText(SignInActivity.this, "Authentication failed.",
  69. Toast.LENGTH_SHORT).show();
  70. //updateUI(null);
  71. }
  72.  
  73. }
  74. });
  75. }
  76.  
  77. // Google Sign In Integration - End
  78.  
  79.  
  80. private void signInUser() {
  81. String email = emailLoginTextInputEditText.getText().toString();
  82. String password = passwordLoginEditText.getText().toString();
  83.  
  84. if (!validateEmail(email)) {
  85. return;
  86. }
  87. if (!validateSetPass(password)) {
  88. return;
  89. }
  90. showProgressDialog("Signing in...");
  91. firebaseAuth.signInWithEmailAndPassword(email, password)
  92. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  93. @Override
  94. public void onComplete(@NonNull Task<AuthResult> task) {
  95. if (!task.isSuccessful()) {
  96. hideProgressDialog();
  97. Toast.makeText(SignInActivity.this, task.getException().getMessage(),
  98. Toast.LENGTH_SHORT).show();
  99.  
  100. } else if (task.isSuccessful()) {
  101. hideProgressDialog();
  102. DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("users").child(getUid());
  103. databaseReference.addValueEventListener(new ValueEventListener() {
  104. @Override
  105. public void onDataChange(DataSnapshot dataSnapshot) {
  106. Profile profile = dataSnapshot.getValue(Profile.class);
  107. Log.e("key", dataSnapshot.getKey());
  108. SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(SignInActivity.this);
  109. SharedPreferences.Editor editor = sharedPreferences.edit();
  110. if (profile != null) {
  111. editor.putString(Preferences.EMAIL, profile.getEmail());
  112. editor.putString(Preferences.NAME, profile.getFullName());
  113. editor.putString(Preferences.USER_TYPE, profile.getUserType());
  114. }
  115. editor.putString(Preferences.USERID, getUid());
  116. editor.apply();
  117. }
  118.  
  119. @Override
  120. public void onCancelled(DatabaseError databaseError) {
  121.  
  122. }
  123. });
  124. Intent loginIntent = new Intent(SignInActivity.this, MainMenuActivity.class);
  125. loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  126. try {
  127. sleep(1000);
  128. } catch (InterruptedException e) {
  129. e.printStackTrace();
  130. }
  131. startActivity(loginIntent);
  132. finish();
  133. }
  134. }
  135. });
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement