Skip to main content

Enable Server Side User Authentication

Server-side user authentication in the LikeMinds SDK ensures secure and reliable user verification by handling authentication on the backend. It protects sensitive user data, prevents unauthorized access, and enables seamless session management. This approach enhances security while providing a smooth integration experience for developers.

Prerequisites

For each tech stacks below, LikeMinds Chat SDK should be integrate using the Getting Started Guide.

Steps to Enable Server-Side User Authentication

Step 1: Create an API on your Server to start a User Session with LikeMinds

Create a new API on your backend server that will handle user authentication with LikeMinds. This API should call the LikeMinds Initiate API to create a new user session. The Initiate API will return an accessToken and refreshToken that will be used to authenticate subsequent requests to the LikeMinds SDK.

Step 2: Create a function on Frontend to get tokens

Create a function to get accessToken and refreshToken from your backend using Initiate API.

suspend fun getTokens():Pair<String,String>{
// your implementation to fetch LikeMinds Authentication Tokens

return Pair(accessToken, refreshToken)
}

Step 3: Create an instance of LMChatCoreCallbacks

While setting up LikeMinds Chat SDK in onCreate() method of the Application class, extend LMChatCoreCallback and pass the instance of the same in LMChatCore.setup()

val application = this // instance of the application
val lmChatTheme = LMChatTheme.COMMUNITY_CHAT // chat theme choosen
val lmChatAppearanceRequest = null // instance of the appearance
val domain = "ENTER YOUR DOMAIN NAME" // domain of the app
val enablePushNotifications = false // enable or disable push notifications
val deviceId = null // device id of the user

val lmChatCoreCallback = object : LMChatCoreCallback {
override fun onAccessTokenExpiredAndRefreshed(
accessToken: String,
refreshToken: String
) {
Log.d("Example","accessToken: $accessToken, refreshToken: $refreshToken")
}

override fun onRefreshTokenExpired(): Pair<String?, String?> {
return runBlocking {
getTokens()
}
}
}

LMChatCore.setup(
application = application,
theme = lmChatTheme,
lmChatCoreCallback = lmChatCoreCallback,
lmChatAppearanceRequest = lmChatAppearanceRequest,
domain = domain,
enablePushNotifications = enablePushNotifications,
deviceId = deviceId
)
info

LMChatCoreCallback has two callbacks:

  1. onAccessTokenExpiredAndRefreshed(): This callback is triggered when the provided accessToken expires and is refreshed internally using the refreshToken.
  2. onRefreshTokenExpired() This callback is triggered when the provided refreshToken expires. In this case, you need to provide a new accessToken and refreshToken from your backend server using our initiate API.

Step 4: Send the received token to LikeMinds SDK

note

Skip this step if you are using AI Chatbot Theme. Navigate to Chatbot Section to know more.

Upon receiving the accessToken and refreshToken from your backend server, call LMChatCore.showChat() function with these tokens.

val context = this // instance of context
val accessToken = "RECEIVED ACCESS TOKEN"
val refreshToken = "RECEIVED REFRESH TOKEN"

val successCallback = { response : UserResponse? ->
//user session initiated successfully, write your logic here
Unit
} // callback triggered when the initiate user call is successful

val failureCallback = { errorMessage ->
Log.e("Example", errorMessage)
Unit
} // callback triggered when the initiate user call fails

LMChatCore.showChat(
context = context,
accessToken = accessToken,
refreshToken = refreshToken,
success = successCallback,
error = failureCallback
)

AI Chatbot Theme

While starting the user session, pass the accessToken and refreshToken from getTokens() function as mentioned in Step 2 to the LMChatAIBotButton component, instead of apiKey, userName, uuid.

val accessToken = "RECEIVED ACCESS TOKEN"
val refreshToken = "RECEIVED REFRESH TOKEN"

findViewById<LMChatAIBotButton>(R.id.btn_ai_chatbot).setChatAIButtonProps(
LMChatAIBotButtonProps.Builder()
.accessToken(accessToken)
.refreshToken(refreshToken)
.build()
)

Conclusion

After completing these steps, your application will be configured with server-side user authentication for the LikeMinds SDK. This setup provides:

  • Secure user verification through backend authentication
  • Protected sensitive user data
  • Seamless session management
  • Automatic token refresh handling

Make sure to test the authentication flow thoroughly in your development environment before deploying to production.