Logout
  • 15 Nov 2024
  • 2 Minutes to read
  • Contributors
  • Dark
    Light

Logout

  • Dark
    Light

Article summary

Android

By default, Android doesn't add a logout button to the view; instead, it gives you the freedom to create the logout button with your own style. However, TicketsSDK also has a feature that adds a built-in button to the Activity's Toolbar.

Use Built-In Logout Button

After the configuration of TicketsSDKClient, you can call the TicketsSDKSingleton.addLogoutMenuItemToEventsScreen method, to add a built-in Logout button into your Top Bar (Toolbar).

TicketsSDKSingleton.addLogoutMenuItemToEventsScreen {
    // The listener to be called after the logout process is completed
}

This logout button will call internally the Activity.finish() and the listener you previously defined.

Add Custom Logout Button

  1. Add a button titled "Logout" somewhere in your UI
  2. When tapped, the button calls:
TicketsSDKSingleton.logout(this@TicketsSdkHostActivity) {
    // The listener to be called after the logout process is completed
}
  1. When the logout listener has being called, you can redirect the user to another Activity or relaunch the login screen. If you stay in the same Activity, remove the EventFragment first from the FragmentManager and then relaunch the LoginIntent. As an example:
TicketsSDKSingleton.logout(this@TicketsSdkHostActivity) {
    // Remove the fragment from the container (it requires the container id)
    supportFragmentManager.findFragmentById(containerId)?.let {
        supportFragmentManager.beginTransaction().remove(it).commit()
    }
    
    // ResultLauncher object to start LoginIntent.
    resultLauncher.launch(TicketsSDKSingleton.getLoginIntent(this))
}

iOS

Logout on App Launch

If you want to force a logout on App Launch (maybe due to end of sport season), make sure you call logout AFTER configuration.

// 1. Launch your app
// 2. configure Auth SDK
TMAuthentication.shared.configure(apiKey: "12345") {
  // 3. if season has ended, logout users 
  TMAuthentication.shared.logout()
}

If you need to logout BEFORE configuration, use logoutAll.

// 1. Launch your app
// 2. if season has ended, logout users 
TMAuthentication.shared.logoutAll()

// 3. configure Auth SDK (sometime later)
TMAuthentication.shared.configure(apiKey: "12345") {
}

Use Built-In Logout Button

By default, the TMTicketsViewController tries to add a Logout button to it's own UINavigationController (if any).

Do not use Built-In Logout Button

This default behavior can be overriden by setting addLogoutButton to false:

let tmVC = TMTicketsViewController()
// do not show built-in logout button
tmVC.addLogoutButton = false

self.present(tmVC, animated: true)

Add Custom Logout Button

In the case you are using a TMTicketsView directly, or the TMTicketsViewController is embedded inside another ViewController, you will need to add your own Logout button somewhere in your UI:

  1. Add a button titled "Logout" somewhere in your UI
  2. When tapped, the button calls:
@objc func logoutButtonPressed(_ sender: Any) {
    TMAuthentication.shared.logout(backend: nil, completion: { _ in
      // handled by notifications below
    })
}
  1. Update the button state by quering the hasToken() method in the Authentication SDK. If the user has a token (even an expired one), then you should allow them to logout via your UI:
@objc func updateLogoutButton() {
    // if the user has ANY kind of token, allow logout
    let hasToken = TMAuthentication.shared.hasToken(backend: nil)
    // you could also hide the button here
    logoutButton.isEnabled = hasToken
    
    // note that you don't actually need to hide or disable the button at all,
    // since logout can be called over and over without any harmful side-effects
}
  1. Set the button's initial state in your viewWillAppear method by calling:
public override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    updateLogoutButton()
}
  1. Update the button at runtime by listening for the loggedIn and loggedOut notifications from TMAuthentication:
private func setupLoginNotifications() {
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(updateLogoutButton),
                                           name: TMAuthentication.AuthNotification.loggedIn,
                                           object: nil)

    NotificationCenter.default.addObserver(self,
                                           selector: #selector(updateLogoutButton),
                                           name: TMAuthentication.AuthNotification.loggedOut,
                                           object: nil)
}

Was this article helpful?