Authentication

Authenticate the patient with the Nabla SDK

Once a Patient is authenticated in your app, you need to authenticate them on Nabla SDK so that they can securely access their conversations.

The authenticate(userId:provider:) method requires a SessionTokenProvider that you must implement.

It will be called every time the Nabla SDK needs to authenticate the user, and you are responsible for providing an accessToken and a refreshToken from your backend. See server side documentation for more details.

The userId is used to identify the user and will be passed to your SessionTokenProvider when provideTokens(userId:completion:) is called.

📘

Note that the SessionTokenProvider will be called only when the SDK needs to identify the user, it won't be instantaneously called after calling authenticate.

import NablaCore

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
      NablaClient.initialize(
        modules: [
          NablaMessagingModule()
        ],
        configuration: .init(apiKey: "YOUR_API_KEY")
      )

      NablaClient.shared.authenticate(userId: myUserId, provider: self)

      // ...

      return true
    }
}

extension AppDelegate: SessionTokenProvider {
    func provideTokens(forUserId userId: String, completion: (AuthTokens?) -> Void) {
        // Use your own backend to get the tokens for your user
        myService.getAuthTokens() { tokens in
            completion(tokens)
        }
    }
}

The example above makes AppDelegate implement SessionTokenProvider. For production application, we recommend that SessionTokenProvider is implemented by another class, usually responsible for the authentication in the other parts of your app.

⚠️

Make sure you call authenticate before calling any other method of the SDK, otherwise a NablaError.authenticationError will be returned as Result.