Mobile SDK Auth Helpers

The Auth class is a crucial component of the Descope SDK, handling key user authentication operations. This class is designed to execute essential functions such as fetching user details, fetching and refreshing sessions, and logging out users.

Mobile SDK

Install SDK

// 1. Within XCode, go to File > Add Packages
// 2. Search for the URL of the git repo: https://github.com/descope/swift-sdk
// 3. Configure your desired dependency rule
// 4. Click Add Package

Import and initialize SDK

Parameters:

  • baseUrl: Custom domain that must be configured to manage token response in cookies. This makes sure every request to our service is through your custom domain, preventing accidental domain blockages.
import DescopeKit
import AuthenticationServices
 
do {
    Descope.setup(projectId: "__ProjectID__", baseUrl = "https://auth.app.example.com")
    print("Successfully initialized Descope")
} catch {
    print("Failed to initialize Descope")
    print(error)
}

Manage User

Retrieve information about the current authenticated user. These methods are used when you need to fetch or display user-related data in your application. It requires the refreshJwt as an argument which is the current refresh token of the user. For Swift, Kotlin, and Flutter, you will use the me method, and for React Native you will use the useDescope hook.

This function returns the following details:

  • email: Email address associated to the user.
  • name: Name associated to the user.
  • givenName: Given name associated to the user.
  • middleName: Middle name associated to the user.
  • familyName: Family name associated to the user.
  • phone: Phone number associated to the user.
  • loginIds: An array of loginIds associated to the user.
  • userId: The user's unique Descope generated userId.
  • verifiedEmail: Boolean whether the email address for the user has been verified.
  • verifiedPhone: Boolean whether the phone number for the user has been verified.
  • picture: The base64 encoded image if the user has an image associated to them.
  • roleNames: An array of roles associated to the user.
  • userTenants: An array of tenant names and IDs associated to the user.
  • createTime: The time that the user was created.
  • totp: Boolean whether the user has TOTP login associated with it.
  • saml: Boolean whether the user has SAML login associated with it.
  • oauth: Boolean whether the user has OAuth login associated with it.
guard let refreshJwt = Descope.sessionManager.session?.refreshJwt else { return }
try await Descope.auth.me(refreshJwt: refreshJwt)

Refresh Session

Refreshes the session of the currently authenticated user. This method is useful when the current user session is about to expire or has expired. By calling this method, you can ensure that the user remains authenticated. This function takes the current refreshJwt as an argument.

guard let refreshJwt = Descope.sessionManager.session?.refreshJwt else { return }
try await Descope.auth.refreshSession(refreshJwt: refreshJwt)

Logout

Logs out the currently authenticated user. This method invalidates the user's current JWT tokens and ends their session. This function is typically used when the user chooses to log out of your application. The function takes the current refreshJwt as an argument.

guard let refreshJwt = Descope.sessionManager.session?.refreshJwt else { return }
try await Descope.auth.logout(refreshJwt: refreshJwt)
Descope.sessionManager.clearSession()
Was this helpful?

On this page