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
*/
}
});
}
}
}
According to this: To check if the user is signed-in, call isConnected(). if (mGoogleApiClient != null && mGoogleApiClient.
Parse is an open-source Android SDK and back-end solution that enables developers to build mobile apps with shared data quickly and without writing any back-end code or custom APIs. Parse is a Node.
Go to Android Studio. Change Android to Project (top left corner). Expand the folders and find "libs". Copy the "parse" folder and paste it in the "libs".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With