Guest User

Untitled

a guest
Jun 3rd, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. private LoginButton btnLogin;
  2. private CallbackManager callbackManager;
  3. private FirebaseAuth firebaseAuth;
  4. private FirebaseAuth.AuthStateListener firebaseAuthListener;
  5. private UserRegistration userRegistration = new UserRegistration();
  6. private ProgressBar progressBar;
  7. private EditText etName, etLastname, etEmail, etUsername, etPassword;
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_option_registration);
  13. etName = findViewById(R.id.name);
  14. etLastname = findViewById(R.id.last_name);
  15. etEmail = findViewById(R.id.email);
  16. etUsername = findViewById(R.id.username);
  17. etPassword = findViewById(R.id.password);
  18.  
  19. callbackManager = CallbackManager.Factory.create();
  20. btnLogin = findViewById(R.id.login_button);
  21. progressBar = findViewById(R.id.progressBar);
  22. btnLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
  23. @Override
  24. public void onSuccess(LoginResult loginResult) {
  25. AccessToken.getCurrentAccessToken().getPermissions();
  26. handleFacebookAccessToken(loginResult.getAccessToken());
  27. //loginResult.getAccessToken().getPermissions();
  28.  
  29. }
  30.  
  31. @Override
  32. public void onCancel() {
  33. Toast.makeText(getApplicationContext(), R.string.cancel_login, Toast.LENGTH_SHORT).show();
  34. }
  35.  
  36. @Override
  37. public void onError(FacebookException error) {
  38. Toast.makeText(getApplicationContext(), R.string.error_login, Toast.LENGTH_SHORT).show();
  39. }
  40. });
  41.  
  42. firebaseAuth = FirebaseAuth.getInstance();
  43. firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
  44. @Override
  45. public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
  46. FirebaseUser user = firebaseAuth.getCurrentUser();
  47. if(user != null){
  48. goFacebookRegisterActivity();
  49. }
  50. }
  51. };
  52. }
  53.  
  54. private void handleFacebookAccessToken(AccessToken accessToken) {
  55. AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
  56. firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
  57. @Override
  58. public void onComplete(@NonNull Task<AuthResult> task) {
  59. if(!task.isSuccessful()){
  60. Toast.makeText(getApplicationContext(), R.string.firebase_error_login, Toast.LENGTH_SHORT).show();
  61. }
  62.  
  63. }
  64. });
  65. }
  66.  
  67. private void goFacebookRegisterActivity() {
  68. Intent intent = new Intent(this, FacebookRegisterActivity.class);
  69. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  70. startActivity(intent);
  71. }
  72.  
  73. @Override
  74. protected void onActivityResult(int requestCode, int resultCode, Intent data){
  75. super.onActivityResult(requestCode, resultCode, data);
  76. callbackManager.onActivityResult(requestCode, resultCode, data);
  77. }
  78.  
  79. @Override
  80. protected void onStart() {
  81. super.onStart();
  82. firebaseAuth.addAuthStateListener(firebaseAuthListener);
  83. }
  84.  
  85. @Override
  86. protected void onStop() {
  87. super.onStop();
  88. firebaseAuth.removeAuthStateListener(firebaseAuthListener);
  89. }
Add Comment
Please, Sign In to add comment