Advertisement
tuttelikz

EnterActivity [Firebase]

Nov 28th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.73 KB | None | 0 0
  1. package com.soyalab.askaruly.lor;
  2.  
  3. import android.content.Intent;
  4. import android.support.annotation.NonNull;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.TextView;
  10. import android.widget.Toast;
  11.  
  12. import com.facebook.CallbackManager;
  13. import com.facebook.FacebookCallback;
  14. import com.facebook.FacebookException;
  15. import com.facebook.login.LoginResult;
  16. import com.facebook.login.widget.LoginButton;
  17. import com.google.android.gms.auth.api.Auth;
  18. import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
  19. import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
  20. import com.google.android.gms.auth.api.signin.GoogleSignInResult;
  21. import com.google.android.gms.common.ConnectionResult;
  22. import com.google.android.gms.common.SignInButton;
  23. import com.google.android.gms.common.api.GoogleApiClient;
  24. import com.google.android.gms.tasks.OnCompleteListener;
  25. import com.google.android.gms.tasks.Task;
  26. import com.google.firebase.auth.AuthCredential;
  27. import com.google.firebase.auth.AuthResult;
  28. import com.google.firebase.auth.FirebaseAuth;
  29. import com.google.firebase.auth.FirebaseUser;
  30. import com.google.firebase.auth.GoogleAuthProvider;
  31.  
  32. public class EnterActivity extends AppCompatActivity {
  33.  
  34. SignInButton googleButton;
  35. FirebaseAuth mAuth;
  36. LoginButton fbButton;
  37. TextView loginStatus;
  38. CallbackManager callbackManager;
  39.  
  40. private final static int RC_SIGN_IN = 2;
  41. GoogleApiClient mGoogleApiClient;
  42. FirebaseAuth.AuthStateListener mAuthListener;
  43.  
  44. @Override
  45. protected void onStart() {
  46. super.onStart();
  47.  
  48. mAuth.addAuthStateListener(mAuthListener);
  49. }
  50.  
  51. @Override
  52. protected void onCreate(Bundle savedInstanceState) {
  53. super.onCreate(savedInstanceState);
  54. setContentView(R.layout.activity_enter);
  55.  
  56. fbButton = (LoginButton)findViewById(R.id.fbBtn);
  57. googleButton = (SignInButton)findViewById(R.id.googleBtn);
  58. loginStatus = (TextView)findViewById(R.id.loginStatus);
  59.  
  60. callbackManager = CallbackManager.Factory.create();
  61. fbButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
  62. @Override
  63. public void onSuccess(LoginResult loginResult) {
  64. loginStatus.setText("Login Success\n" + loginResult.getAccessToken().getUserId());
  65. /*startActivity(new Intent(EnterActivity.this,MainActivity.class));*/
  66. startActivity(new Intent(EnterActivity.this,LoginActivity.class));
  67. }
  68.  
  69. @Override
  70. public void onCancel() {
  71. loginStatus.setText("Login cancelled");
  72. }
  73.  
  74. @Override
  75. public void onError(FacebookException error) {
  76.  
  77. }
  78. });
  79.  
  80. mAuth = FirebaseAuth.getInstance();
  81.  
  82. googleButton.setOnClickListener(new View.OnClickListener() {
  83. @Override
  84. public void onClick(View view) {
  85. signIn();
  86. }
  87. });
  88.  
  89. mAuthListener = new FirebaseAuth.AuthStateListener() {
  90. @Override
  91. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  92. if (firebaseAuth.getCurrentUser() != null) {
  93. /*startActivity(new Intent(EnterActivity.this,MainActivity.class));*/
  94. startActivity(new Intent(EnterActivity.this,LoginActivity.class));
  95. }
  96. }
  97. };
  98.  
  99. // Configure Google Sign In
  100. GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
  101. .requestIdToken(getString(R.string.default_web_client_id))
  102. .requestEmail()
  103. .build();
  104.  
  105. // Build a GoogleApiClient with access to the Google Sign-In API and the
  106. // options specified by gso.
  107. mGoogleApiClient = new GoogleApiClient.Builder(this)
  108. .enableAutoManage(this /* FragmentActivity */, new GoogleApiClient.OnConnectionFailedListener() {
  109. @Override
  110. public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
  111. Toast.makeText(EnterActivity.this, "Something went wrong",Toast.LENGTH_SHORT).show();
  112. }
  113. })
  114. .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
  115. .build();
  116. }
  117.  
  118. private void signIn() {
  119. Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
  120. startActivityForResult(signInIntent, RC_SIGN_IN);
  121. }
  122.  
  123. @Override
  124. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  125. super.onActivityResult(requestCode, resultCode, data);
  126.  
  127. // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
  128. if (requestCode == RC_SIGN_IN) {
  129. GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
  130. if (result.isSuccess()) {
  131. // Google Sign In was successful, authenticate with Firebase
  132. GoogleSignInAccount account = result.getSignInAccount();
  133. firebaseAuthWithGoogle(account);
  134. } else {
  135. // Google Sign In failed, update UI appropriately
  136. // ...
  137. Toast.makeText(EnterActivity.this, "Auth went wrong",Toast.LENGTH_SHORT).show();
  138. }
  139. }
  140. }
  141.  
  142. private void firebaseAuthWithGoogle(GoogleSignInAccount account) {
  143. AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
  144. mAuth.signInWithCredential(credential)
  145. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  146. @Override
  147. public void onComplete(@NonNull Task<AuthResult> task) {
  148. if (task.isSuccessful()) {
  149. // Sign in success, update UI with the signed-in user's information
  150. Log.d("TAG", "signInWithCredential:success");
  151. FirebaseUser user = mAuth.getCurrentUser();
  152. //updateUI(user);
  153. } else {
  154. // If sign in fails, display a message to the user.
  155. Log.w("TAG", "signInWithCredential:failure", task.getException());
  156. Toast.makeText(EnterActivity.this, "Authentication failed.",
  157. Toast.LENGTH_SHORT).show();
  158. //updateUI(null);
  159. }
  160.  
  161. // ...
  162. }
  163. });
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement