Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private LoginButton btnLogin;
- private CallbackManager callbackManager;
- private FirebaseAuth firebaseAuth;
- private FirebaseAuth.AuthStateListener firebaseAuthListener;
- private UserRegistration userRegistration = new UserRegistration();
- private ProgressBar progressBar;
- private EditText etName, etLastname, etEmail, etUsername, etPassword;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_option_registration);
- etName = findViewById(R.id.name);
- etLastname = findViewById(R.id.last_name);
- etEmail = findViewById(R.id.email);
- etUsername = findViewById(R.id.username);
- etPassword = findViewById(R.id.password);
- callbackManager = CallbackManager.Factory.create();
- btnLogin = findViewById(R.id.login_button);
- progressBar = findViewById(R.id.progressBar);
- btnLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
- @Override
- public void onSuccess(LoginResult loginResult) {
- AccessToken.getCurrentAccessToken().getPermissions();
- handleFacebookAccessToken(loginResult.getAccessToken());
- //loginResult.getAccessToken().getPermissions();
- }
- @Override
- public void onCancel() {
- Toast.makeText(getApplicationContext(), R.string.cancel_login, Toast.LENGTH_SHORT).show();
- }
- @Override
- public void onError(FacebookException error) {
- Toast.makeText(getApplicationContext(), R.string.error_login, Toast.LENGTH_SHORT).show();
- }
- });
- firebaseAuth = FirebaseAuth.getInstance();
- firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- FirebaseUser user = firebaseAuth.getCurrentUser();
- if(user != null){
- goFacebookRegisterActivity();
- }
- }
- };
- }
- private void handleFacebookAccessToken(AccessToken accessToken) {
- AuthCredential credential = FacebookAuthProvider.getCredential(accessToken.getToken());
- firebaseAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if(!task.isSuccessful()){
- Toast.makeText(getApplicationContext(), R.string.firebase_error_login, Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- private void goFacebookRegisterActivity() {
- Intent intent = new Intent(this, FacebookRegisterActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intent);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data){
- super.onActivityResult(requestCode, resultCode, data);
- callbackManager.onActivityResult(requestCode, resultCode, data);
- }
- @Override
- protected void onStart() {
- super.onStart();
- firebaseAuth.addAuthStateListener(firebaseAuthListener);
- }
- @Override
- protected void onStop() {
- super.onStop();
- firebaseAuth.removeAuthStateListener(firebaseAuthListener);
- }
Add Comment
Please, Sign In to add comment