Authentication Forwarding

The StreamLayer SDK provides a rich set of features. Some features require user authentication, including Poll, Trivia & Predication Ads, In-Play Game, Betting ,Watch Party, Chat, and Who’s Watching functionality. The SDK supports multiple authentication methods, allowing you to select the approach that best fits your app experience:

  • Bypass Authentication
  • Phone Authentication
  • Anonymous Authentication

Bypass Authentication

If your host app already uses a token-based authentication system, you can pass that authentication token to the StreamLayer SDK. This approach prevents users from having to authenticate separately in the SDK, enabling automatic authentication.

To enable bypass authentication, call the StreamLayer.authorizationBypass() suspend function during your app’s authentication process.

anyCoroutineScope.launch{
    runCatching{
      StreamLayer.authorizationBypass("schema", "token")
    }.onFailure{
      // check error
    }
}

The StreamLayer SDK requests user data from the backend. If the request is successful, the client creates a user entry in the SDK’s database based on the received data.

Note:
The StreamLayer.authorizationBypass() method throws aRuntimeExceptionif the operation fails. Make sure to always catch this exception in your implementation.

Phone Authentication

The SDK includes a default authentication method that collects the user’s phone number. This method is partially abstracted from the host app.

Use the provided StreamLayerAuthActivity` class to launch the authentication flow when needed. You can start the flow with the following method:

StreamLayerAuthActivity.open(context, closeWhenAuthorized = true)

The closeWhenAuthorized` parameter determines whether the authentication flow should open if the user is already authorized. For usage examples, see the demo application here.

Anonymous Authentication

If the host app does not implement authentication and you do not want to use phone authentication, you can use anonymous authentication. In this mode, the SDK assigns a userID without collecting a phone number.

To enable anonymous authentication, call the StreamLayer.useAnonymousAuth() suspend function:

anyCoroutineScope.launch{
    runCatching{
      StreamLayer.useAnonymousAuth()
    }.onFailure{
      // check error
    }
}

Note:
The StreamLayer.useAnonymousAuth() method throws aRuntimeException if the operation fails. Be sure to always catch this exception in your implementation.

Managing Auth state

To remove the current user and clear the user cache, call the StreamLayer.logout() suspend function:

anyCoroutineScope.launch{
    StreamLayer.logout()
}

To check the current authentication state, use StreamLayer.isUserAuthorized() or subscribe to authentication state updates withStreamLayer.userIsAuthorizedState():

// get current auth state - is user authorized or not
StreamLayer.isUserAuthorized()

// subscribe to auth state updates
StreamLayer.userIsAuthorizedState()

If the host app does not perform authentication for the StreamLayer SDK and a user requests an authentication-required feature, the SDK displays its internalAuth Requiredscreen.

To handle these requests in the host app, implement the SLRAuthRequestHandler interface.

// set auth handler
StreamLayer.setAuthHandler(object : SLRAuthRequestHandler {
    override fun onAuthRequired(context: Context) {
        // sdk requested authentication - use phone authentication for example
        StreamLayerAuthActivity.open(context, closeWhenAuthorized = true)
     }
})