The Userfront Core JavaScript library is intended for use in frontend applications.
It can be used for the following:
Authentication: useful for building your own custom forms and auth flows.
User: read or update information about a logged in user.
Tokens: read the user's access or ID token.
A standalone library with all of the JS methods on this page.
Prebuilt authentication components for use with React, Vue, and HTML Web Components. The toolkit library also exports all of the JS methods on this page.
Install @userfront/core
via npm or yarn.
The core library must always be initialized with a workspace or tenant ID using the init() method.
Initializes the Userfront core library with your workspace or tenant ID.
Calls the supplied callback whenever Userfront.init()
is called.
If addInitCallback
is called more than once, callbacks are called in the order they were added (first added = first called).
Once Userfront.init()
is called, the callbacks are reset and are not executed on subsequent calls to Userfront.init()
The function to call.
A JSON object with the tenantId
is supplied to the callback.
Requests to send emails or SMS messages to users are rate limited to mitigate spam, malicious activity, and accidental misuse. Requests that exceed the rate limit will fail with an error message indicating the request was rate limited.
There are two kinds of rate limit: duplicate request prevention and spam prevention.
Duplicate request prevention:
Userfront will not send email or SMS messages to any user more than once in any 2 second interval. If a request was already fulfilled in the last 2 seconds, additional attempts will fail with an error message indicating the request was rate limited.
Spam prevention:
Userfront will not send an excessive number of emails or SMS messages to a user. When these spam prevention rate limits are exceeded, the user will be marked as a suspected target of spam or malicious activity, and Userfront will send no further emails or SMS messages for the following 24 hours, and fail with an error message indicating the request was rate limited for any further requests to send messages to the email address or phone number.
For newly registered users and existing users whose email address or phone number is not verified, the limits are:
When sending to an existing user's confirmed email address or phone number, the limits are:
To opt any particular user account out of rate limiting - for example, user accounts used in development and automated testing - please contact support with the user IDs to be opted out.
To match live mode behavior, rate limits are enforced in test mode, even though Userfront does not send emails or SMS messages in test mode.
Registers a new user with one of the available methods.
The method for registering a new user.
password
- Sign up with email +
password
passwordless
- Sign up with email
only
verificationCode
- Sign up with verificationCode
only
apple
- Single Sign-On (SSO)
with Apple
azure
- Single Sign-On (SSO)
with Azure
facebook
- Single Sign-On
(SSO) with Facebook
github
- Single Sign-On
(SSO) with GitHub
google
- Single Sign-On
(SSO) with Google
linkedin
- Single Sign-On
(SSO) with LinkedIn
okta
- Single Sign-On (SSO)
with Okta
The user's email address.
The user's username.
The user's full name.
The user's phone number.
The user's custom data object.
The user's password.
Either "email"
or "sms"
to specify the channel the verification code was sent to.
Set a path to redirect to, or false
to prevent redirection.
Set a custom method to handle the JWT access token set upon a successful login response. See handleTokens()
Set a custom method to handle redirection upon a successful login response. See handleRedirect()
Set a custom method to handle an "MFA required" response. See handleMfaRequired()
Set a custom method to handle a "PKCE required" response. See handlePkceRequired()
password
methodSubmits an email and password to create a user.
Upon success, receives the JWT access token and adds it to the browser's cookies, then redirects the browser to the After-signup path.
passwordless
methodCreates a user and sends them an email with a link to log in. This link works with the Login via link method.
If a user with the given email address already exists, sends them an email to log in.
verificationCode
methodCreates a user and sends them a verification code to the channel specified to log in, either "email"
or "sms"
.
Sends the verification code and the channel, in order to receive a JWT access token, then adds the JWT access token to the browser's cookies and redirects the browser to the After-login path.
If a user with the given email address already exists, sends them a verification code to log in.
Initiates the sign-on flow for a given SSO provider.
apple
azure
facebook
github
google
linkedin
okta
See Single Sign-On for details about SSO.
Initiates a login for a user with one of the available methods.
The method for logging in.
password
- Log in with email + password
passwordless
- Log in with email only
link
- Log in with link
totp
- Log in with TOTP
verificationCode
- Log in with a verification code
apple
- Single Sign-On (SSO)
with Apple
azure
- Single Sign-On (SSO)
with Azure
facebook
- Single Sign-On
(SSO) with Facebook
github
- Single Sign-On (SSO)
with GitHub
google
- Single Sign-On (SSO)
with Google
linkedin
- Single Sign-On
(SSO) with LinkedIn
okta
- Single Sign-On (SSO)
with Okta
saml
- SAMLThe user's email address.
The user's username.
The user's email or username.
The user's phone number.
The user's ID.
The user's UUID.
The user's password.
The token=
URL parameter sent in a login link.
The uuid=
URL parameter sent in a login link.
The TOTP code sent to the user. Required if there is no backupCode
.
A backup code a user saved during TOTP setup. Required if there is no totpCode
.
The verification code sent to the user.
Either "email"
or "sms"
to specify the channel the verification code was sent to.
Set a path to redirect to, or false
to prevent redirection.
Set a custom method to handle the JWT access token set upon a successful login response. See handleTokens()
Set a custom method to handle redirection upon a successful login response. See handleRedirect()
Set a custom method to handle an "MFA required" response. See handleMfaRequired()
Set a custom method to handle a "PKCE required" response. See handlePkceRequired()
password
methodSends a username or email along with a password in order to receive a JWT access token, then adds the JWT access token to the browser's cookies and redirects the browser to the After-login path.
passwordless
methodSends the user an email with a link to log in. This link works with the Login via link method.
If no user exists with the given email, this method creates a new user and sends them a login link.
See also: signup via passwordless method.
link
methodThis method is used to read the URL query parameters token
and uuid
that are sent with login link emails, and uses these parameters to log in a user.
Sends the token and uuid in order to receive a JWT access token, then adds the JWT access token to the browser's cookies and redirects the browser to the After-login path.
totp
methodThis method is used to log in a user with a Time-based One-Time Password (TOTP) code.
Sends the TOTP or backup code in order to receive a JWT access token, then adds the JWT access token to the browser's cookies and redirects the browser to the After-login path.
verificationCode
methodThis method is used to log in a user with a code sent to their email or phone number.
Sends the verification code and the channel, either "email"
or "sms"
, in order to receive a JWT access token, then adds the JWT access token to the browser's cookies and redirects the browser to the After-login path.
Initiates the sign-on flow for a given SSO provider.
apple
azure
facebook
github
google
linkedin
okta
See Single Sign-On for details about SSO.
saml
methodCompletes the sign-on flow for a SAML service provider.
Obtains a SAML token and redirects the browser to the Userfront API SAML login endpoint where the login response will be sent to the service provider who initiated the SAML login request.
When a user clicks a link to log in to a service provider, the service provider sends a SAML login request to the Userfront API which will then redirect the browser to your After-logout path where this method should be called.
When using the saml
method, there is no difference between signing the user in and signing the user up.
Both cases are handled by the service provider during the sign-on flow:
logout()
Logs a user out by invalidating their session, removes auth tokens from the browser, and then redirects the browser to the After-logout path.
Completes the SAML logout process.
Obtains a SAML token and redirects the browser to the SAML logout endpoint where the logout response will be sent to the service provider who initiated the SAML logout request.
When a user wants to log out of a service provider, the service provider sends a SAML logout request to the Userfront API which will then redirect the browser to your After-logout path where this method should be called.
Upon successful logout, the user will be logged out of the service provider yet remain logged in to your tenant's application.
Checks if the user is logged in and, if so, redirects the browser to the After-login path.
Alias of updatePassword()
.
Updates a user's password with one of the following methods:
token
and uuid
); orIf the user does not have a password yet, then their password is created.
The new password to set for the user.
The user's existing password. If the user does not have a password yet, can be omitted.
The token=
URL parameter sent in a password reset link.
The uuid=
URL parameter sent in a password reset link.
Optionally specify the link
or jwt
method. When not defined,
updatePassword
checks for the reset link credentials (token
and uuid
)
first, followed by the user's JWT access token.
Manually set the path to redirect to, or false
to prevent redirection.
link
methodUses the reset link credentials (token
and uuid
) to reset the user's password, then logs the user in by adding their JWT access token to the browser's cookies, and finally redirects the browser to the After-login path.
jwt
methodUpdates a user's password while they are logged in.
If the user has a password already, the existingPassword
field must be correct.
If the user does not have a password yet (e.g. if they signed up with SSO), the existingPassword
field is ignored, and the password
field is set directly.
Sends an email containing a login link. This link directs the user to the After-logout path, where the login form should be located.
The user in question must exist already.
This endpoint is rate limited. Unverified users will only be emailed once per day. Verified users can only be emailed once every ten seconds.
Sends an email containing a password reset link. This link directs the user to the Password reset path.
The password reset link contains the token
and uuid
credentials, which can be used with the updatePassword method.
The user in question must exist already.
Custom handlers give you fine-grained control during signup or login.
When registered, these handlers are called in the following situations:
handleTokens(): upon successful signup or login.
handleRedirect(): upon successful signup or login.
handleMfaRequired(): upon "MFA required" response.
handlePkceRequired(): upon "PKCE required" response.
Calls the provided callback method upon a successful signup or login response.
The URL or path to redirect to, determined by one of the following in order of precedence:
A value provided in the initial method call:
Userfront.login({redirect})
A value provided in the URL querystring:
yoursite.com/login?redirect=/path
redirectTo
value returned in the responseThe full response object, which contains the mode, default redirection path, tokens, and more.
Calls the provided callback method upon an "MFA required" response.
Calls the provided callback method upon a "PKCE required" response.
The authorization code to include when submitting the PKCE request.
The URL or path to redirect to, determined by one of the following in order of precedence:
A value provided in the initial method call:
Userfront.login({redirect})
A value provided in the URL querystring:
yoursite.com/login?redirect=/path
redirectTo
value returned in the responseThe full response object, which contains the mode, authorization code, default redirection path, and more.
Returns information about the currently logged in user.
Userfront.user is intended for frontend use only, to help you display information about the user.
"live"
or "test"
mode.
The tenant that the user record is in.
The user's numerical ID.
The user's UUID.
The user's email address.
The user's phone number.
The user's full name.
The user's image URL.
The user's custom data object.
The user's username.
Whether the user has confirmed their email.
Whether the user has confirmed their phoneNumber.
Whether the user has required MFA for their own account.
When the user record was created.
When the user record was last updated.
[Deprecated] Whether the user has confirmed their email. Use isEmailConfirmed instead.
Helper method to determine if the logged in user has a given role in the authorization
object of their JWT access token.
Returns true
if the role is present, or false
if not present.
user.hasRole() should only be used to show or hide public elements like buttons or badges.
Sensitive information should always rely on server-side checks.
The tenant to check against. Defaults to the tenantId from
Userfront.init(tenantId)
Returns the JWT access token.
Your frontend application can send the access token to your server in order to authenticate a user and provide information about their access levels. For more information, see Concepts › JWT Access Tokens.
Returns the name of the cookie that holds the JWT access token.
The ID token is not intended for authentication or access control.
It is used client-side as a verifiable copy of the user's data. Typically it is easier to reference the user object instead.
Returns the JWT ID token.
Returns the name of the cookie that holds the JWT ID token.
Refreshes the existing token set. This will update the tokens to contain the latest user information and will extend the JWT access token's expiration.