init needs to be the very first thing you call in your web app. It initializes the SDK with your App ID, and it also handles automatic logins when users return to your web app with an active session.
Parameters
- appId [string] - The App ID you received when you created your Userbase app.
- sessionLength [number | optional | Unit: hours | Min: 0.084 (5 mins) | Max: 8760 (1 yr) ] - If resuming a session, this is the number of hours the user's session will extend. Defaults to 24.
- allowServerSideEncryption [boolean | optional] - If your app is set to use the Server-side encryption mode in your Admin panel, this value must be set to true.
- updateUserHandler [function | optional] - A callback that gets invoked when a user is updated. See the Advanced section below for more details.
Result
-
session [object] - Contains information about the last used session.
-
user [object] - If set, this contains information about the logged in user.
- username [string] - The user's username.
- userId [string] - The user's unique identifier.
- authToken [string] - The user's authentication token that can be used to verify the user is signed in via the Verify Auth Token endpoint in the Userbase Admin API.
- creationDate [string] - The date the user was created.
- email [string] - The user's email.
- profile [object] - The user's profile.
- protectedProfile [object] - The user's protected profile set via Update User in the Admin API.
- paymentsMode [string] - The app's payments mode set in your Admin panel. Can be 'disabled', 'test', or 'prod'.
- subscriptionStatus [string] - The user's subscription status. Can be 'active', 'incomplete', 'incomplete_expired', 'past_due', 'canceled', or 'unpaid'. Each status is explained further here.
- subscriptionPlanId [string] - The subscription plan the user is subscribed to.
- trialExpirationDate [string] - The date the user's trial expires.
- cancelSubscriptionAt [string] - The date the user's subscription is set to be canceled.
- lastUsedUsername [string] - If set, this is the username of the last user that was logged in.
Advanced
- You can provide an updateUserHandler callback function to listen for when a user gets updated in real-time, either via updateUser in the SDK, or Update User in the Admin panel. The function can be used to keep your user's state in sync with the server, like this:
The updateUserHandler callback is invoked with the following:
-
user [object] - The newly updated user.
- username [string] - The user's username.
- userId [string] - The user's unique identifier.
- authToken [string] - The user's authentication token that can be used to verify the user is signed in via the Verify Auth Token endpoint in the Userbase Admin API.
- creationDate [string] - The date the user was created.
- email [string] - The user's email.
- profile [object] - The user's profile.
- protectedProfile [object] - The user's protected profile set via Update User in the Admin API.
- passwordChanged [boolean] - Indicates if a user's password was changed.
- paymentsMode [string] - The app's payments mode set in your Admin panel. Can be 'disabled', 'test', or 'prod'.
- subscriptionStatus [string] - The user's subscription status. Can be 'active', 'incomplete', 'incomplete_expired', 'past_due', 'canceled', or 'unpaid'. Each status is explained further here.
- subscriptionPlanId [string] - The subscription plan the user is subscribed to.
- trialExpirationDate [string] - The date the user's trial expires.
- cancelSubscriptionAt [string] - The date the user's subscription is set to be canceled.
Errors
- ParamsMustBeObject
- AppIdMissing
- AppIdAlreadySet
- AppIdMustBeString
- AppIdCannotBeBlank
- AppIdNotValid
- UpdateUserHandlerMustBeFunction
- SessionLengthMustBeNumber
- SessionLengthTooShort
- SessionLengthTooLong
- UserAlreadySignedIn
- ServiceUnavailable