Guest User

Untitled

a guest
Nov 17th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. public class LoginActivity extends AppCompatActivity {
  2.  
  3. //a constant for detecting the login intent result
  4. private static final int RC_SIGN_IN = 1;
  5.  
  6. //Tag for the logs optional
  7. private static final String TAG = "AndroidMads";
  8.  
  9. //creating a GoogleSignInClient object
  10. GoogleSignInClient mGoogleSignInClient;
  11.  
  12. //And also a Firebase Auth object
  13. FirebaseAuth mAuth;
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_login);
  19.  
  20. //first we initialized the Firebase Auth object
  21. mAuth = FirebaseAuth.getInstance();
  22.  
  23. //Then we need a GoogleSignInOptions object
  24. //And we need to build it as below
  25. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  26. .requestIdToken(getString(R.string.default_web_client_id))
  27. .requestEmail()
  28. .build();
  29.  
  30. //Then we will get the GoogleSignInClient object from GoogleSignIn class
  31. mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
  32.  
  33. //Now we will attach a click listener to the sign_in_button
  34. //and inside onClick() method we are calling the signIn() method that will open
  35. //google sign in intent
  36. findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
  37. @Override
  38. public void onClick(View view) {
  39. //getting the google signin intent
  40. Intent signInIntent = mGoogleSignInClient.getSignInIntent();
  41. //starting the activity for result
  42. startActivityForResult(signInIntent, RC_SIGN_IN);
  43. }
  44. });
  45. }
  46.  
  47. @Override
  48. protected void onStart() {
  49. super.onStart();
  50. //if the user is already signed in
  51. //we will close this activity
  52. //and take the user to profile activity
  53. if (mAuth.getCurrentUser() != null) {
  54. finish();
  55. startActivity(new Intent(this, ActivitySplash.class));
  56. }
  57. }
  58.  
  59. @Override
  60. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  61. super.onActivityResult(requestCode, resultCode, data);
  62.  
  63. //if the requestCode is the Google Sign In code that we defined at starting
  64. if (requestCode == RC_SIGN_IN) {
  65.  
  66. //Getting the GoogleSignIn Task
  67. Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
  68. try {
  69. //Google Sign In was successful, authenticate with Firebase
  70. GoogleSignInAccount account = task.getResult(ApiException.class);
  71. //authenticating with firebase
  72. firebaseAuthWithGoogle(account);
  73. } catch (ApiException e) {
  74. Log.v("API Exception", e.toString());
  75. Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
  76. }
  77. }
  78. }
  79.  
  80. private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
  81. Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
  82.  
  83. //getting the auth credential
  84. AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
  85.  
  86. //Now using firebase we are signing in the user here
  87. mAuth.signInWithCredential(credential)
  88. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  89. @Override
  90. public void onComplete(@NonNull Task<AuthResult> task) {
  91. if (task.isSuccessful()) {
  92. if (mAuth.getCurrentUser() != null) {
  93. startActivity(new Intent(LoginActivity.this, ActivitySplash.class));
  94. finish();
  95. }
  96. } else {
  97. // If sign in fails, display a message to the user.
  98. Log.w(TAG, "signInWithCredential:failure", task.getException());
  99. Toast.makeText(LoginActivity.this, "Authentication failed.",
  100. Toast.LENGTH_SHORT).show();
  101.  
  102. }
  103. }
  104. });
  105. }}
Add Comment
Please, Sign In to add comment