Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
  2.  
  3. private Button buttonSignIn;
  4. private EditText editTextEmail;
  5. private EditText editTextPassword;
  6. private TextView textViewSignUp;
  7. private ProgressDialog progressDialog;
  8. private FirebaseAuth firebaseAuth;
  9. private CallbackManager mCallbackManager;
  10. private static final String TAG = "FACELOG";
  11.  
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.activity_login);
  16.  
  17. firebaseAuth = FirebaseAuth.getInstance();
  18.  
  19. if(firebaseAuth.getCurrentUser() != null) {
  20. //profile activity here (user already logged in)
  21. finish();
  22. startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
  23. }
  24.  
  25. progressDialog = new ProgressDialog(this);
  26. editTextEmail = (EditText) findViewById(R.id.editTextEmail);
  27. editTextPassword = (EditText) findViewById(R.id.editTextPassword);
  28. buttonSignIn = (Button) findViewById(R.id.buttonSignIn);
  29. textViewSignUp = (TextView) findViewById(R.id.textViewSignUp);
  30.  
  31. buttonSignIn.setOnClickListener(this);
  32. textViewSignUp.setOnClickListener(this);
  33.  
  34. // Initialize Facebook Login button
  35. mCallbackManager = CallbackManager.Factory.create();
  36. LoginButton loginButton = findViewById(R.id.login_button);
  37. loginButton.setReadPermissions("email", "public_profile");
  38. loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
  39. @Override
  40. public void onSuccess(LoginResult loginResult) {
  41. Log.d(TAG, "facebook:onSuccess:" + loginResult);
  42. handleFacebookAccessToken(loginResult.getAccessToken());
  43. }
  44.  
  45. @Override
  46. public void onCancel() {
  47. Log.d(TAG, "facebook:onCancel");
  48. }
  49.  
  50. @Override
  51. public void onError(FacebookException error) {
  52. Log.d(TAG, "facebook:onError", error);
  53. }
  54. });
  55. }
  56.  
  57. @Override
  58. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  59. super.onActivityResult(requestCode, resultCode, data);
  60.  
  61. // Pass the activity result back to the Facebook SDK
  62. mCallbackManager.onActivityResult(requestCode, resultCode, data);
  63. }
  64.  
  65. private void userLogin() {
  66. String email = editTextEmail.getText().toString().trim();
  67. String password = editTextPassword.getText().toString().trim();
  68.  
  69. if(TextUtils.isEmpty(email)) { //email is empty
  70. Toast.makeText(this, "Please enter email", Toast.LENGTH_SHORT).show();
  71. return;
  72. }
  73. if(TextUtils.isEmpty(password)) { //password is empty
  74. Toast.makeText(this, "Please enter password", Toast.LENGTH_SHORT).show();
  75. }
  76.  
  77. //if validation is okay
  78. //we will first show a progress bar
  79. progressDialog.setMessage("Logging In...");
  80. progressDialog.show();
  81.  
  82. firebaseAuth.signInWithEmailAndPassword(email, password)
  83. .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  84. @Override
  85. public void onComplete(@NonNull Task<AuthResult> task) {
  86. progressDialog.dismiss();
  87. if(task.isSuccessful()) {
  88. //start profile activity
  89. finish();
  90. startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
  91. }
  92. }
  93. });
  94. }
  95.  
  96. @Override
  97. public void onClick(View view) {
  98. if(view == buttonSignIn) { userLogin(); }
  99. if(view == textViewSignUp) {
  100. finish();
  101. startActivity(new Intent(this, SignupActivity.class));
  102. }
  103. }
  104.  
  105. @Override
  106. public void onStart() {
  107. super.onStart();
  108. // Check if user is signed in (non-null) and update UI accordingly.
  109. FirebaseUser currentUser = firebaseAuth.getCurrentUser();
  110.  
  111. if(currentUser != null) {
  112.  
  113. updateUI();
  114. }
  115. }
  116.  
  117. private void handleFacebookAccessToken(AccessToken token) {
  118. Log.d(TAG, "handleFacebookAccessToken:" + token);
  119.  
  120. AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
  121. firebaseAuth.signInWithCredential(credential);
  122. /*.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  123. @Override
  124. public void onComplete(@NonNull Task<AuthResult> task) {
  125. if (task.isSuccessful()) {
  126. // Sign in success, update UI with the signed-in user's information
  127. Log.d(TAG, "signInWithCredential:success");
  128. FirebaseUser user = firebaseAuth.getCurrentUser();
  129. updateUI();
  130. } else {
  131. // If sign in fails, display a message to the user.
  132. Log.w(TAG, "signInWithCredential:failure", task.getException());
  133. Toast.makeText(LoginActivity.this, "Authentication failed.",
  134. Toast.LENGTH_SHORT).show();
  135. //updateUI();
  136. }
  137.  
  138. // ...
  139. }
  140. });*/
  141. updateUI();
  142. }
  143.  
  144. private void updateUI() {
  145. Toast.makeText(LoginActivity.this, "You logged in with FB.",
  146. Toast.LENGTH_LONG).show();
  147. finish();
  148. startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement