Client 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.
Before using the Auth Helper Functions, make sure to import and initialize the relevant SDK.
Manage Session
If project settings are configured to manage session token in cookies, Descope services will automatically set the session token in the DS cookie as a Secure and HttpOnly cookie. In this case, the session token will not be stored in the browser and will not be accessible to the client-side code using these session management functions.
The useSession
functions retrieve information about the current user session.
They return the following details:
Objects
sessionToken
: The JWT for the current session.
Booleans
isAuthenticated
: Boolean for the authentication state of the current user session.isSessionLoading
: Boolean for the loading state of the session. Can be used to display a "loading" message while the session is still loading.
Additional helper functions
getSessionToken
: The JWT for the current session.getRefreshToken
: The refresh JWT for the current session.
Limited to Non-Cookie Storage
The getRefreshMethod
function will not work if refresh tokens are stored in httpOnly
cookies, as they cannot be accessed from the frontend.
isSessionTokenExpired
: Boolean for checking if a given session token is expired.isRefreshTokenExpired
: Boolean for checking if a refresh token is expired.getJwtRoles
: Get current roles from an existing session token. Provide tenant id for specific tenant roles.getJwtPermissions
: Get current permissions from an existing session token. Provide tenant id for specific tenant permissions.getCurrentTenant
: Get current tenant id from an existing session token (from the dct claim).
Handling Authentication State Changes
Descope provides event listeners to track session and user state changes dynamically. These functions can be accessed through useDescope()
.
Event Functions
onSessionTokenChange(newSession, oldSession)
: Triggers when the session token changes.onIsAuthenticatedChange(isAuthenticated)
: Fires when authentication status changes.onUserChange(newUser, oldUser)
: Triggers when user data updates.
Manage User
The useUser
and me
functions return information about the currently authenticated user. These methods are used when you need to fetch or display user-related data in your application.
These functions returns the following details:
Objects
user
: object that contains the following user attributes: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.
Booleans
isUserLoading
: Boolean for whether the user object is currently loading.
Note
useDescope
, useSession
, and useUser
should be used inside AuthProvider
context, and will throw an exception if this requirement is not met
Refresh Session
In the case that the browser has a valid refresh token on storage/cookie,
the user should get a valid session token (i.e. user should be logged-in).
For this reason, it is common to call the refresh function after sdk initialization. Refresh returns a session token, so if autoRefresh
is set to true, the sdk will
automatically continue to refresh the token.
useDescope is a React hook that retrieves the Descope SDK for further operations related to authentication. This includes the refresh operation, as shown in the React and Next.js examples below.
Note
useSession
triggers a single request to the Descope backend to attempt to refresh the session. If you don't useSession on your app, the session will not be refreshed automatically. If your app does not require useSession, you can trigger the refresh manually by calling refresh from the useDescope hook:
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.
useDescope is a React hook that retrieves the Descope SDK for further operations related to authentication. This includes the logout operation, as shown in the React, Next.js, and Vue examples below.
Logout All
This will sign the user out of all the devices they are currently signed-in with. Successfully executing this endpoint will invalidate all user's refresh tokens. Response will include all user tokens and fields empty, so client will remove cookies as well.
useDescope is a React hook that retrieves the Descope SDK for further operations related to authentication. This includes the logoutAll operation, as shown in the React, Next.js, and Vue examples below.