Guest User

Untitled

a guest
Dec 10th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. int RC_SIGN_IN = 1;
  2. private FirebaseAuth mAuth = FirebaseAuth.getInstance();
  3. GoogleSignInClient mGoogleSignInClient;
  4. GoogleSignInOptions gso;
  5.  
  6. gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  7. .requestIdToken(getString(R.string.default_web_client_id))
  8. .requestEmail()
  9. .build();
  10.  
  11. mGoogleSignInClient = GoogleSignIn.getClient(MainActivity.this, gso);
  12. Intent signInIntent = mGoogleSignInClient.getSignInIntent();
  13. startActivityForResult(signInIntent, RC_SIGN_IN);
  14.  
  15. @Override
  16. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  17. if (requestCode == RC_SIGN_IN) {
  18. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
  19. try {
  20. GoogleSignInAccount account = task.getResult(ApiException.class);
  21. firebaseAuthWithGoogle(account);
  22. } catch (ApiException e) {
  23. Log.d("demo", "Google sign in failed: " + e.getMessage());
  24. }
  25. }
  26. }
  27.  
  28. private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
  29. AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
  30. mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener < AuthResult > () {
  31. @Override
  32. public void onComplete(@NonNull Task < AuthResult > task) {
  33. if (task.isSuccessful()) {
  34. FirebaseUser user = mAuth.getCurrentUser();
  35. Intent intent = new Intent(MainActivity.this, TripsActivity.class);
  36. startActivity(intent);
  37. finish();
  38. } else {
  39. Log.d("demo", "Google sign in failed");
  40. }
  41. }
  42. });
  43. }
  44.  
  45. //Logout
  46.  
  47. FirebaseAuth.getInstance().signOut();
  48. gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  49. .requestIdToken(getString(R.string.default_web_client_id))
  50. .requestEmail()
  51. .build();
  52.  
  53. mGoogleSignInClient = GoogleSignIn.getClient(getActivity(), gso);
  54. mGoogleSignInClient.signOut()
  55. .addOnCompleteListener(getActivity(), new OnCompleteListener < Void > () {
  56. @Override
  57. public void onComplete(@NonNull Task < Void > task) {
  58. Intent intent = new Intent(getActivity(), MainActivity.class);
  59. startActivity(intent);
  60. getActivity().finish();
  61. }
  62. });
  63. }
Add Comment
Please, Sign In to add comment