Authentication Forwarding

Configure user authentication for the StreamLayer Android SDK. Choose from bypass, phone, or anonymous authentication methods to enable interactive features like polls and chat.

Authentication Forwarding

Some StreamLayer features require user authentication, including poll units, trivia and prediction units, In-Play Games, betting, chat, and Who's Watching. The SDK supports multiple authentication methods so you can choose the approach that best fits your app experience:

  • Bypass Authentication — forward your existing auth token to the SDK
  • Phone Authentication — use the SDK's built-in phone number flow
  • Anonymous Authentication — assign a user ID without collecting a phone number

Bypass Authentication

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

To enable bypass authentication, call StreamLayer.authorizationBypass() 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 succeeds, the client creates a user entry in the SDK's database based on the received data.

Note: StreamLayer.authorizationBypass() throws a RuntimeException if the operation fails. Always catch this exception in your implementation.

Phone Authentication

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

Use the StreamLayerAuthActivity class to launch the authentication flow:

StreamLayerAuthActivity.open(context, closeWhenAuthorized = true)

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

Anonymous Authentication

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

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

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

Note: StreamLayer.useAnonymousAuth() throws a RuntimeException if the operation fails. Always catch this exception in your implementation.

Managing Auth State

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

anyCoroutineScope.launch{
    StreamLayer.logout()
}

To check the current authentication state, use StreamLayer.isUserAuthorized() or subscribe to authentication state updates with StreamLayer.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 internal Auth Required screen.

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)
     }
})

Related