Authentication
  • 09 Apr 2024
  • 2 Minutes to read
  • Contributors
  • Dark
    Light

Authentication

  • Dark
    Light

Article summary

This is a quick list of the most common Authentication use cases.

Login

Android

AndroidManifest.xml code:

<activity
    android:name="com.ticketmaster.authenticationsdk.internal.modernaccounts.presentation.ModernAccountsLoginScreen"
    android:screenOrientation="portrait"
    android:exported="true"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <!-- YOUR SCHEME will be provided in your app settings, copy it to this location -->
        <data android:scheme="psdkscheme[client scheme name]" />
    </intent-filter>
</activity>

Example Kotlin code:


var tmAuthentication: TMAuthentication? = null

fun getClient() = TMAuthentication.Builder()
    .apiKey(consumer_key)
    .clientName(client_name) // Team name to be displayed
    .colors(TMAuthentication.ColorTheme(LightColorPalette, DarkColorPalette))
    .environment(TMXDeploymentEnvironment.Production) // Environment that the SDK will use. Default is Production
    .region(TMXDeploymentRegion.US) // Region that the SDK will use. Default is US
    .build(activity)
    
    private val loginLauncher = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) {
        when (it.resultCode) {
            // For a successful sign in
            Activity.RESULT_OK -> {
                val accessTokenData = it.data
                ?.getParcelableExtra(Constants.ACCESS_TOKEN_DATA)
                ?: AccessTokenData()
    }
        // If user cancelled the sign in or if there was an error while trying to log in.
        Activity.RESULT_CANCELED -> {
            //User cancelled the sign in process.
        }
    }
}

iOS

Example Swift code:

import TicketmasterAuthentication

// Tickets SDK handles login for you, so this call is optional
// call after `TMAuthentication.shared.configure(...)`
TMAuthentication.shared.login { authToken in
    print("Login Completed")
    print(" - AccessToken: \(authToken.accessToken.prefix(20))...")
    print(" - IdToken: \(authToken.idToken.prefix(20))...")
} aborted: { oldAuthToken, backend in
    print("Login Aborted by User")
} failure: { oldAuthToken, error, backend in
    print("Login Error: \(error.localizedDescription)")
}

Refresh Token

Android

Example Kotlin code:


suspend fun getAccessToken() {

    if (tmAuthentication == null) {
        tmAuthentication = getClient()
    }
    
    if (tmAuthentication!!.configuration == null) {
        // show an error
        return
    }
    
    val archticsAccessToken = tmAuthentication.getToken(AuthSource.ARCHTICS)
    val hostAccessToken = = tmAuthentication.getToken(AuthSource.HOST)
    
    if (archticsAccessToken.isNullOrEmpty() && hostAccessToken.isNullOrEmpty()) {
        val intent = tmAuthentication.getLoginIntent(activity)
        loginLauncher.launch(intent)
    }
}

iOS

Example Swift code:

import TicketmasterAuthentication

// Tickets SDK handles refresh for you, so this call is optional
// call after `TMAuthentication.shared.configure(...)`
TMAuthentication.shared.validToken { authToken in
    print("Token Refreshed (if needed)")
    print(" - AccessToken: \(authToken.accessToken.prefix(20))...")
    print(" - IdToken: \(authToken.idToken.prefix(20))...")
} aborted: { oldAuthToken, backend in
    print("Refresh Login Aborted by User")
} failure: { oldAuthToken, error, backend in
    print("Refresh Error: \(error.localizedDescription)")
}

Member Info

Android

Example Kotlin code:

suspend fun getUserDetails() {
    val archticsAccessToken = tmAuthentication.getToken(AuthSource.ARCHTICS)
    val hostAccessToken = tmAuthentication.getToken(AuthSource.HOST)

    if (archticsAccessToken.isNullOrEmpty() && hostAccessToken.isNullOrEmpty()) {

        val intent = tmAuthentication.getLoginIntent(activity)
        loginLauncher.launch(intent)
    } else {
        val userdetails = tmAuthentication.fetchUserDetails()
    }
}

iOS

Example Swift code:

import TicketmasterAuthentication

// Tickets SDK handles login for you, so this call is optional
// call after `TMAuthentication.shared.configure(...)`
TMAuthentication.shared.memberInfo { memberInfo in
    print("MemberInfo Completed")
    print(" - UserID: \(memberInfo.localID ?? "<nil>")")
    print(" - Email: \(memberInfo.email ?? "<nil>")")
} failure: { oldMemberInfo, error, backend in
    print("MemberInfo Error: \(error.localizedDescription)")
}

Logout

Android

Example Kotlin code:


//TicketsSDKClient and TMAuthentication already has it's own logout logic, so this call is optional
//unless you add your own logout button somewhere
suspend fun logout() {
    TicketsSDKSingleton.logout(context)
}

iOS

Example Swift code:

import TicketmasterAuthentication

// TMTicketsViewController has it's own logout button, so this call is optional
// unless you add your own logout button somewhere
// call after `TMAuthentication.shared.configure(...)`
TMAuthentication.shared.logout { backends in
    print("Logout Completed")
    print(" - Backends Count: \(backends?.count ?? 0)")
}

Was this article helpful?

What's Next