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.
userbase.init({
appId: 'YOUR_APP_ID'
}).then((session) => {
// SDK initialized successfully
if (session.user) {
// there is a valid active session
console.log(session.user.username)
}
}).catch((e) => console.error(e))
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
Errors
- ParamsMustBeObject
- AppIdMissing
- AppIdAlreadySet
- AppIdMustBeString
- AppIdCannotBeBlank
- AppIdNotValid
- UpdateUserHandlerMustBeFunction
- SessionLengthMustBeNumber
- SessionLengthTooShort
- SessionLengthTooLong
- UserAlreadySignedIn
- ServiceUnavailable