Reputation: 579
I am currently trying to setup user login and registration in my app. The first thing I am doing though is having my MainActivity
check to see if a user is logged in and if they are, check if the session is valid. I am not sure if I am getting the ParseSession
correctly, and if I am, I am not sure what to do at this point. (The docs are not very clear at showing how to accomplish this). Here is my code:
public class MainActivity extends AppCompatActivity {
ParseUser currentUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId(getResources().getString(R.string.parse_app_id))
.clientKey(getResources().getString(R.string.parse_client_key))
.server(getResources().getString(R.string.parse_server_url))
.build()
);
if (isFirstLaunch()) {
Toast.makeText(this, "This is the first launch", Toast.LENGTH_LONG).show();
}
if (!isUserLoggedIn()) {
Toast.makeText(this, "User is not logged in", Toast.LENGTH_LONG).show();
}
}
private boolean isFirstLaunch() {
return false;
}
private boolean isUserLoggedIn() {
currentUser = ParseUser.getCurrentUser();
if (currentUser == null) {
return false;
} else {
ParseSession.getCurrentSessionInBackground(new GetCallback<ParseSession>() {
@Override
public void done(ParseSession object, ParseException e) {
/*
* if session is valid
* return true
* if session is invalid
* run ParseUser.logOut()
* return false
*/
}
});
}
}
}
Upvotes: 5
Views: 2340
Reputation: 3798
Session
are automatically managed by ParseSession
these objects are stored on Parse in the Session class, and you can view them on the Parse.com Data Browser
More information on Session
:Sessions represent an instance of a user logged into a device. Sessions are automatically created when users log in or sign up. They are automatically deleted when users log out.
There is one distinct Session object for each user-installation pair; if a user issues a login request from a device they’re already logged into, that user’s previous Session object for that Installation is automatically deleted.
Reference of Site for more detail on Session class.
How to check if logged in user session is valid in Android Parse SDK?
As mentioned above if user is valid (you can check it by ParseUser.getCurrentUser()!= null
) and authenticated using isAuthenticated()
method of ParseUser
class. Session must be valid by default (Automatically managed by ParseSession
). And the way you checked for Session is correct but there is no need to check for valid session if user is valid and authenticated.
Hope this is Helpful.
Upvotes: 4