To acquire the single instance of the UserManager call sdk.getUserManager()
.
The logged status of the user can be observed from the getStatusLiveData()
method.
Possible values and their expected values are:
login
command was just executed and it’s being processed. On both cases the status should update shortly.Additionally NotLoggedIn
and LoggingOut
contains a user-friendly message
string and a Reason
enum.
When a LoggingOut
occurs, the message and reason will be copied over to the following NotLoggedIn
status.
The Reason
values are:
LoggingOut
and a new call to login()
should pass without issues on the next attempt.ClientApplicationRequestedLogout
: The client app called the logout()
method.The UserManager provides 3 different types of login for the user
val result : LiveData<SimpleResponse<Void>> = userManager.usernamePasswordLoginLiveData(username, password)
result.observe(this)
.onSuccess {
// login success
}
.onFail { exception ->
// login fail
}
To login with OAuth2 you have to do the login in an external Webview and add a deep-link back into the app.
The deep-link URI have to be configured on the backend.
The URL to call for the external url consists of the BASE_URL + /auth/ + OAUTH_ID
val uri = Uri.parse("${baseUrl}/auth/${oauthId}")
CustomTabsIntent.Builder()
.build()
.launchUrl(activity, uri)
We recommend the use CustomTabsIntent class of Android. All security conscious oauth2 providers blocks the usage of built-in WebView. To use the CustomTabsIntent
you have to add
implementation "com.android.support:customtabs:<latest>"
as a dependency into your build.gradle
file
After the login succeeded the WebView will deep-link back into your application which will receive the redirectedUrl
on the Intent
to login to the SDK.
val result : LiveData<SimpleResponse<Void>> = userManager.oAuth2LoginLiveData(intent.dataString)
The result of the login operation can be observed similarly to the username and password login
To login the user using server-to-server pre-shared authentication token your backend has to register a token for the user on the Smartspaces Server and provide it to the following method.
val result : LiveData<SimpleResponse<Void>> = userManager.externalTokenLoginLiveData(authToken)
The result of the login operation can be observed similarly to the username and password login
If the login failed the state of the UserManager will be UserManager.Status.NotAuthorized
and then UserManager.Status.NotLoggedIn
for all login variations. UserManager.Status.NotAuthorized
also has a message string with the explanation, why the authorization failed.
To logout a user simply call the logout()
method:
userManager.logout()
This method doesn’t return a LiveData<Response>
because its result can be directly observed on the UserManager status
You can only change the password, if you are using login via username and password
You can change the password of a logged in user, like in the following example:
val result : LiveData<SimpleResponse<Void>> = userManager.changePasswordLiveData(oldPassword, newPassword, newPasswordConfirm)
result.observe(this)
.onSuccess {
// change password success
}
.onFail { exception ->
// change password fail
}
To get all available user information (f.e. email, firstname, lastname, salutation) use the following code
val user : LiveData<User> = userManager.getUserLiveData().onSuccess()
user.observe(this, Observer { user ->
user?.let {
}
})