1
votes

So I am trying to implement Facebook Login on my Android app.

So far I think it is working. It logins. So, when I go back to my login page (still need to update this) the button I had saying "login with facebook", turns to "Log out".

I press it, but instead of logging out, it logs in again.

This is the code I have for handling Facebook Login

 var facebookLoginBtn = findViewById<Button>(R.id.loginFacebookBtn)


    facebookLoginBtn.setOnClickListener(View.OnClickListener {
        callBackManager = CallbackManager.Factory.create()
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"))
        LoginManager.getInstance().registerCallback(callBackManager,
                object : FacebookCallback<LoginResult> {
                    override fun onSuccess(loginResult: LoginResult) {
                        Log.d("LoginActivity", "Facebook Token: " + loginResult.accessToken.token)
                        startActivity(Intent(applicationContext, MainActivity::class.java))
                    }

                    override fun onCancel() {
                        Log.d("LoginActivity", "Facebook onCancel")
                    }

                    override fun onError(error: FacebookException?) {
                        Log.d("LoginActivity", "Facebook onError")
                    }
                })
    })




}

I tried this for the logout

 var btnLogout = findViewById<Button>(R.id.loginFacebookBtn)

    btnLogout.setOnClickListener(View.OnClickListener {
        if (AccessToken.getCurrentAccessToken() == null) {
            return@OnClickListener
        } else if (AccessToken.getCurrentAccessToken() != null) {
            GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions/",
                    null, HttpMethod.DELETE, GraphRequest.Callback {
                LoginManager.getInstance().logOut()
            }
            ).executeAsync()


        }
    })
1
You never log out in your code - rollstuhlfahrer
I used some logout code, but since the button is the same, it just doesn't do anything because the same button has there login and logout states - JaneDyta
can I show you the logout and see if its correct? - JaneDyta

1 Answers

0
votes

You are assigning 2 event handlers to 1 button. That may lead to both event handlers being executed. I haven't found a source for this, but at least it would fit your description of logging out and logging back in again.

Since you are using only one button to log in and out, why not use the same event handler and use some if statement to determine which way to go?

facebookLoginBtn.setOnClickListener(View.OnClickListener {
    // Check if logged in
    if (loggedIn) {
        facebookLogOut();
    }
    else {
        facebookLogIn();
    }
});

loggedIn may be replaced by (AccessToken.getCurrentAccessToken() == null) as you are already using this to check before the logout.