Authentication
1. Initialization of the component
HomeControlSDKComponent is the main entry point for every HomeControl functionality, in order to get a configured instance initialize it like in the following snippet:
| //For non content provider integration initialize like this
val homeControlComponent =
DaggerHomeControlSDKComponent.builder().homeControlContextModule(
HomeControlContextModule(appContext)
).homeControlApiModule(HomeControlApiModule(usesContentProvider = false,
contentProviderUrl = null)).build()
|
2. Authorize device by code
The authorization of a device is done using the Interactor presented in the code below. If the authorization is successful, the response will be a DeviceCodeStatus object containing the user code, user code link, and authentication status.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | val authorizeDeviceByCode: Interactor<Unit, Outcome> =
homeControlComponent.getAuthorizeByCodeInteractor()
//example of use in a ViewModelScope
viewModelScope.launch {
authorizeDeviceByCode(Unit)
.collect {
when (val outcome = it) {
is Success -> {
//handle outcome.value of type DeviceCodeStatus
}
is Failure -> {
//handle errors: outcome.error
}
}
}
}
|
Info
ViewModelScope is a CoroutineScope, bound to the lifetime of a ViewModel. A coroutine launched in viewModelScope will be cancelled when the ViewModel is cleared.
3. Check if user is authenticated
To check if a user is authenticated or not you can use an IsUserAuthenticatedInteractor. If no errors occur and the user's authentication can be checked, the value of the outcome will be true or false.
1
2
3
4
5
6
7
8
9
10
11
12
13 | val isUserAuthenticatedInteractor: Interactor<Unit, Outcome> =
homeControlComponent.getIsUserAuthenticatedInteractor()
viewModelScope.launch {
when (isUserAuthenticatedInteractor(Unit)) {
is Success -> {
//handle outcome.value of type Boolean
}
is Failure -> {
//handle errors: outcome.error
}
}
}
|
4. Revoke Authorization
In order to unlink the user account, you should revoke authorization using the following Interactor:
1
2
3
4
5
6
7
8
9
10
11
12
13 | val revokeAuthorizationInteractor: Interactor<Unit, Outcome> =
homeControlComponent.getRevokeAuthorizationInteractor()
viewModelScope.launch {
when (revokeAuthorizationInteractor(Unit)) {
is Success -> {
//...
}
is Failure -> {
//AuthorizationSet response is failure
}
}
}
|