If you want your users to be able to upload content or other operations you will need them to login with their Newzulu Platform account. The SDK comes with an handy login API to help you do it.
1. Authenticate user
To authenticate a user, they will need to input their email and password.
Here is how to pass them to the SDK:
getPlatformClient().setCredentials(NEWZULU_PLATFORM_EMAIL, NEWZULU_PLATFORM_PASSWORD, new SharedPreferencesSessionStore(this));
The
SharedPreferencesSessionStore
is a storage for the SDK that allows the user session to persist on the device across application launches, thus saving the cost to authenticate again. If you don't want the session to persist, you can pass anEphemeralSessionStore
.
Calling setCredentials
won't authenticate your user directly. If you want to make sure the credentials are valid, you then need to call the authenticate
method to validate the credentials with the server and create a new session:
getPlatformClient().authenticate(new Request.ResponseListener<Session>()
{
@Override
public void onResponse(@NonNull Session session)
{
// User is authenticated and now has a valid session. You can use APIs that require auth.
}
}, new Request.ErrorListener<PlatformException>()
{
@Override
public void onError(@NonNull PlatformException e)
{
// An error occurred during authentication, see #3 to handle it properly
}
});
If you're confident that the credentials are valid (e.g. the application has already authenticated with the same credentials in the past), you may skip the authentication process. It will be performed automatically as needed when you perform a request.
2. Access user information
If the login is successful you can access the user information via the Session
object.
You can access the session object by calling: getCurrentSession()
on your PlatformClient.
This session object contains a User
object that you can get using getUser()
method:
if( getPlatformClient().getCurrentSession() != null )
{
User user = getPlatformClient().getCurrentSession().getUser();
}
Note that
getCurrentSession
will return null if the user isn't properly authenticated.
The user object contains the following information:
getId()
: The unique ID of the user on the server.getUserName()
: The username (guaranteed to be not-null).getFirstName()
andgetLastName()
: The first and last name of the user (may be null).getEmail()
: The email address of the user (may be null).
3. Handle errors
If the login fails, you get a PlatformException
that contains information about what went wrong. You can call getType()
on the exception to get the type of error, here's the list of possible values:
NETWORK
: A network error occurred and the SDK unable to reach the remote service.WRONG_CREDENTIAL
: The provided email and/or password aren't correct.SERVICE_ERROR
: The remote service encountered an error, you should try again later.UNKNOWN
: An unknown internal error occurred.