How to check if logged in user session token is valid?

1,381 views
Skip to first unread message

cayet...@gmail.com

unread,
Feb 15, 2017, 5:39:07 PM2/15/17
to back{4}app
I started using back4app a couple of days ago and as I am going over the documentation, I am not able to understand how to use session tokens. I noticed that if a user logs in, it automatically creates a session. My question is, how do I check if the session is valid for the currently logged on user. I currently do not have a log in feature yet, but I am first creating the method that will check to see if a user is logged in. Here is what I currently have:

public class Home extends AppCompatActivity {

   
SharedPreferences mPreferences;


   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView
(R.layout.activity_home);

        mPreferences
= getSharedPreferences("com.example.example", MODE_PRIVATE);

       
Parse.initialize(new Parse.Configuration.Builder(this)
               
.applicationId("appid")
               
.clientKey("clientkey")
               
.server("https://parseapi.back4app.com/")
               
.build());

        checkFirstRun
();

       
if (!isUserLoggedIn()) {
            startActivity
(new Intent(this, Authentication.class));
            finish
();
       
} else {
            checkValidSession
();
       
}
   
}

   
private void checkFirstRun() {
       
if (mPreferences.getBoolean("isFirstRun", true)) {
            mPreferences
.edit().putBoolean("isFirstRun", false).apply();
            startActivity
(new Intent(this, Introduction.class));
            finish
();
       
}
   
}

   
private boolean isUserLoggedIn() {
       
ParseUser currentUser = ParseUser.getCurrentUser();
       
return currentUser != null;
   
}

   
private void checkValidSession() {

   
}
How can I check if the session is valid using the checkValidSession method?

casag...@back4app.com

unread,
Mar 16, 2017, 10:06:03 AM3/16/17
to back{4}app
Hello,

If you try calling the actual section using REST API, like you can check here: http://parseplatform.org/docs/rest/guide/#sessions (also changing the url to https://parseapi.back4app.com/sessions/me), you might get the actual session in order to check if it is valid or not. 

Also with a session toke you may check if it is valid by using this example:

curl -X GET 
-H "X-Parse-Application-Id: application-id-here" 
-H "X-Parse-REST-API-Key: rest-api-key-here" 
-H "X-Parse-Session-Token: session-token-here" 
https://parseapi.back4app.com/sessions/me

If you have any other questions, please contact us here, on the Online Chat or by emailing us on comm...@back4app.com.

Best!

Alexis L.

unread,
Mar 17, 2017, 3:16:20 AM3/17/17
to back{4}app
Just to mention that it is even more important to react to session invalidation, and detect such cases when a query/save object returns 'object not found for update....
Reply all
Reply to author
Forward
0 new messages