Facebook / Android – how to check if a user is registered, now just log in

I encountered a problem when using Android to log in / register with Facebook. The documents are not enough, and their use cases are strange (what they discuss is only the fragments there, which will attract people's attention to the topic)

I create a working login (Registration), but if I don't check whether the user has logged in, the application will try to reinsert them into dB, resulting in an error

Besides, I don't know what uihelper is, so I've completely removed it from my code

My question is how to check whether the user has registered through Facebook. Now just log in

Editor: what I want to say is that users do not log in in either case, but in one case, they have registered through FB, and I want to log in only to them. In the other case, they do not register through FB and its first method, so I want to register them in the database and log them in (I have completed the latter)

@Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.authButton) {
            OpenRequest openrequest = new OpenRequest(this).setPermissions(Arrays.asList("basic_info", "email", "user_likes", "user_status"));
            Session session = new Session(Login.this);
            session.openForRead(openrequest);
}

private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

private void onSessionStateChange(Session session, SessionState state, Exception exception) {

    if (state.isOpened()) {
         makeMeRequest(session);
    } else if (state.isClosed()) {

        System.out.println("closed"); //debugging
        Session sessionw = new Session(this);
        Session.setActiveSession(sessionw);
        sessionw.openForRead(new Session.OpenRequest(this).setCallback(callback).setPermissions(Arrays.asList("basic_info", "email", "user_likes", "user_status")));
    }
}


private void makeMeRequest(final Session session) {
    // Make an API call to get user data and define a 
    // new callback to handle the response.
    Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
        @Override
        public void onCompleted(GraphUser user, Response response) {
            // If the response is successful
            if (session == Session.getActiveSession()) {
                if (user != null) {
                    userFBId = user.getId();
                    userName = user.getName();
                    userEmail = (String) user.asMap().get("email");
                    userSex = (String) user.getProperty("gender");
                    userFacebookAvatar = "http://graph.facebook.com/"+user.getId()+"/picture?type=square";

                    new RegisterFacebookUser(Login.this, Login.this, userName, userEmail, userSex, userFacebookAvatar).execute(); //this is an async task that inserts the user in the db
                }
            }
            if (response.getError() != null) {
                System.out.println(response.getError().toString());
            }
        }
    });
    request.executeAsync();
} 


@Override 
public void OnFacebookUserRegisteredSuccess(int userId) {
    editor = sharedPrefs.edit();
    editor.putBoolean("userLoggedInState", true);
    editor.putBoolean("userHasRegisteredViaFacebook", true);
    editor.putInt("currentLoggedInUserId", userId);
    editor.putString("userFBId", userFBId);
    editor.commit();
    Intent signupSuccessHome = new Intent(this, Home.class);
    startActivity(signupSuccessHome);
    finish();

}

@Override
public void OnFacebookUserRegisteredFailure() {

    toastMaker.toast(net.asdqwe.activities.Login.this, Configurationz.ErrorMessages.FACEBOOK_LOGIN_FAILURE, Toast.LENGTH_LONG);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REAUTH_ACTIVITY_CODE) {
       Session.getActiveSession().onActivityResult(Login.this, requestCode, resultCode, data);
    } else {

        Session session = Session.getActiveSession();
        int sanitizedRequestCode = requestCode % 0x10000;
        session.onActivityResult(this, sanitizedRequestCode, resultCode, data);

    }
}

resolvent:

You should not rely on your preferences to determine whether users have previously registered, so you can only "log in". Facebook allows users to register applications, but users may cancel registration or delete your applications from their console in the future. Facebook will then reset its permission token sent to your applications, and any activities posted on behalf of users will be rejected

Therefore, to answer your question, please perform the process you have done for registration and login. If the user has registered, Facebook will pop up some permission UI flows, and the user does not have to do anything (because all permissions have been granted). You will get the session token in the application. Update this session token in your application and use it for all requests on behalf of the user in the future

Note: I think you are using the old version of Facebook SDK. You should upgrade to the latest version (3.6). They have fixed many registration / login problems. Your users will thank you very much

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>