5

In previous versions of the Facebook SDK we could check if a user is still signed in by using tokens. In the new 3.0 version I can't find another way other than invoking openSession() to check if the user is still logged in with SSO. But openSession() automatically invokes the login if the user is not saved and I don't want that. I only want to check SSO. How do I do this?

George Netu
  • 2,758
  • 4
  • 28
  • 49
YanDaik
  • 167
  • 1
  • 10
  • I need this too. Can't find it. WTF? – Andrea Baccega Oct 25 '12 at 15:24
  • 2
    Once you create a Session, you can check the state of the Session object by calling session.getState, and if the state is in the CREATED_TOKEN_LOADED state, that means there's saved and valid tokens, and you can call open() directly without any user interaction. – Ming Li Oct 25 '12 at 18:16
  • 4
    Alternatively, you can call Session.openActiveSession(Context context), which according to the javadocs, will only open the session if it does not require user interaction. – Ming Li Oct 25 '12 at 18:16
  • In what scenario would you use openActiveSessionFromCache – Madhur Ahuja Oct 22 '13 at 06:04

2 Answers2

8

I made a helper function that to check if the user is still logged in (or actually has an active session) for me in my app:

public static boolean isActive() {
    Session session = Session.getActiveSession();
    if (session == null) {
        return false;
    }
    return session.isOpened();
}
Daniel Backman
  • 5,121
  • 1
  • 32
  • 37
  • It doesn't work for me, I keep getting "{Session state:CLOSED, token:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}, appId:234242323423434}" in the session – Nativ Apr 24 '13 at 10:49
  • I can see some people have problems with this. First of all make sure that your keys are correct in the FB app. I recall having some issue with this. My current flow that works for me is 1. check isActive(); no? 2. call openSession() 3. if error & session!=null ->session.closeAndClearTokenInformation(); – Daniel Backman Apr 26 '13 at 15:02
  • In what scenario would you use openActiveSessionFromCache – Madhur Ahuja Oct 22 '13 at 06:05
  • I am using 3.23.0 version. Session.getActiveSession() always returning null when I am checking in splash screen though user is logged in previously. – Prashanth Debbadwar Oct 15 '15 at 09:01
3

Alternatively, you can call Session.openActiveSession(Context context), which according to the javadocs, will only open the session if it does not require user interaction. – Ming Li

YanDaik
  • 167
  • 1
  • 10