Logout

Prev Next

Adding a Logout Button

Use Built-In Logout Button

Built-In Logout Button is available only on Android

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

Add a button titled "Logout" somewhere in your UI

When tapped, the button calls:

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

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 {
     // The listener to be called after the logout process is completed
     //remove the fragment from the container (requires the container id)
     supportFragmentManager.findFragmentById(R.id.tickets_sdk_view)?.let {
          supportFragmentManager.beginTransaction().remove(it).commit()
     }

     // Start LoginIntent
     TicketsSDKSingleton.getLoginIntent(this@MainActivity)
          ?.let { loginLauncher.launch(it) }
}
@objc func logoutButtonPressed(_ sender: Any) {
    TMAuthentication.shared.logout(backend: nil, completion: { _ in
      // handled by notifications below
    })
}

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
}

Set the button's initial state in your viewWillAppear method by calling:

public override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    updateLogoutButton()
}

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

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.

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

// 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.
```swift
// 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`:
```swift
let tmVC = TMTicketsViewController()
// do not show built-in logout button
tmVC.addLogoutButton = false

self.present(tmVC, animated: true)