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 (me), refreshing a session (refreshSession), and logging out a user (logout).

Client 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

import DescopeKit
import AuthenticationServices
 
do {
    Descope.setup(projectId: "__ProjectID__")
    print("Successfully initialized Descope")
} catch {
    print("Failed to initialize Descope")
    print(error)
}

Me

Retrieves information about the currently authenticated user. This method is 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.

This function returns the following details:

  • 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 wether the user has TOTP login associated with it.
  • saml: Boolean wether the user has SAML login associated with it.
  • oauth: Boolean wether 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()

React Native SDK Methods (useDescope Hook)

The useDescope hook exposes the underlying Descope SDK instance, which can be used to call any of the SDK methods directly.

List of a few methods available:

  • refresh
  • logout
  • logoutAll
  • me
  • oauth
  • otp

For a full list, check out the definitions here.

App.tsx
import { useDescope, useSession } from '@descope/react-native-sdk'
 
const App = () => {
  const descope = useDescope()
  const { manageSession } = useSession()
 
  const verifyOTP = async () => {
    const resp = await descope.otp.email.verify('andy@example.com', '123456')
    manageSession(resp.data)
  }
 
  return (
    <>
      <p>Hello!</p>
      <button onClick={verifyOTP}>Verify OTP</button>
    </>
  )
}
Was this helpful?

On this page