Menu

API Mode Integration

@rootauth/core is the framework-free RootAuth SDK. It handles configuration, encryption, authentication requests, MFA, third-party login, session operations, and language switching, but does not render UI.

Preparation

Create an application in the RootAuth Console and copy the app_id (format: app_******).

Please add the following configuration line to the .npmrc file in the project root directory:

bash Copy
@rootauth:registry=http://gitlab.standard-software.co/api/v4/projects/12421/packages/npm/

1. Installation

@rootauth/core is the UI-free base SDK for RootAuth authentication APIs, request configuration, sessions, and shared utilities. This section shows installation with npm.

bash Copy
npm install @rootauth/core

2. Create a client

The client stores shared options such as app_id, API URL, OIDC parameters, and locale. This section covers client creation, required options, and the complete configuration table.

ts Copy
import { RootAuthClient, type RootAuthClientConfig } from '@rootauth/core'

const config: RootAuthClientConfig = {
  app_id: 'your-app-id',
  api_base_url: 'https://rootauth-api.rootauth.com',
  redirect_uri: window.location.origin,
  scope: ['openid'],
  cloudflare_container: '#rootauth-turnstile',
})

const rootAuth = new RootAuthClient(config)

// Optional: preload and cache app config and the device fingerprint.
await rootAuth.install()

An install() preload failure does not block application startup. If it is omitted or the preload fails, login() and register() fetch the required data before submitting.

2.1 Configuration

Option Type Required Default Description
app_id string Yes None Application ID created in the RootAuth console
api_base_url string No https://rootauth-api.rootauth.com API base URL
redirect_uri string No window.location.origin OIDC callback URL after login or registration
scope string[] No ['openid'] Requested user-field scope
state string No undefined Custom state returned unchanged in the callback
grant_type string No authorization_code OIDC authorization mode
locale string No zh-CN Supports zh-CN and en-US
enable_google_one_tap boolean No false Allows googleOneTapLogin() to start Google One Tap
cloudflare_container string Yes with Cloudflare undefined Dedicated container CSS selector used by every Turnstile mode

3. Calling convention

Authentication methods return promises and selected flows also accept an onSuccess callback. Supplying the callback transfers success handling to the application, while failures throw normalized error objects.

Authentication methods return a Promise and accept an optional onSuccess callback:

ts Copy
interface RootAuthFlowCallbacks<T> {
  onSuccess?: (response: T) => void
}

When onSuccess is supplied, login and registration do not perform the SDK default redirect or automatic login; the application handles the result.

The following interfaces support this callback convention: sendCode(), login(), verifyLoginMfa(), register(), resetPassword(), and updateProfile().

ts Copy
const result = await rootAuth.login({
  type: 'EMAIL_PASS',
  email: 'user@example.com',
  password: 'password',
})
ts Copy
rootAuth.login(
  {
    type: 'EMAIL_PASS',
    email: 'user@example.com',
    password: 'password',
  },
  {
    onSuccess(result) {
      console.log(result)
    },
  }
)

Successful responses and errors may carry an SDK message:

ts Copy
interface RootAuthMessage {
  type: string
  msg: string
}

interface RootAuthMessageCarrier {
  rootauth_message?: RootAuthMessage
}

type is one of success, warning or error, msg is the final localized copy returned by the backend for the current locale.

Core renders no UI. The application must display messages from rootauth_message or error objects.


4. Login: login()

login() supports email, phone, username, verification-code, WhatsApp, and invitation login flows. This section lists every type, its authentication method, required fields, and examples.

4.1 Complete type list

type Login method Required account fields Credential field
EMAIL_PASS Email and password email password
EMAIL_CODE Email and verification code email code
PHONE_PASS Phone and password phone_code, phone_num password
PHONE_CODE Phone and verification code phone_code, phone_num code
USER_NAME_PASS Username and password user_name password
WHATSAPP_CODE WhatsApp verification code phone_code, phone_num code

4.2 Parameters

Parameter Type Default Description
type Login type above None Required login method
email string None Required for email types
phone_code string PHONE_PASS, PHONE_CODE, WHATSAPP_CODE Calling code without +, for example 86
phone_num string PHONE_PASS, PHONE_CODE, WHATSAPP_CODE Phone number without the calling code
user_name string None Required for username login
password string Password types Plaintext input; encrypted with the application RSA public key
code string | number Verification-code types Six-digit verification code
is_invite number EMAIL_PASS, EMAIL_CODE Whether this is an invitation registration login
redirect_uri string Client configuration Overrides the configured redirect URI
scope string[] Client configuration Overrides configured scopes
state string Client configuration Overrides configured OIDC state
grant_type string Client configuration Overrides configured grant type

The caller must pass the correct type; the SDK does not infer the login method from supplied fields. When MFA follows login, loginAccountType is derived from this explicit login type. A TOTP account name is then selected from the matching account field: email for email login, phone for phone login, and username for username login.

ts Copy
await rootAuth.login({
  type: 'PHONE_CODE',
  phone_code: '86',
  phone_num: '13800000000',
  code: '123456',
})

5. Send a code: sendCode()

sendCode() sends codes for login, registration, password reset, and MFA flows. This section describes email, SMS, WhatsApp, and other type values with their required parameters.

If the error message Too many incorrect verification code attempts. Please request a new code. appears when requesting a verification code, go to Console > General Security > CAPTCHA Rules > CAPTCHA Attempt Count to check whether the configured limit meets your requirements.

5.1 Complete type list

type Verification method Required fields
EMAIL Email code email, email_template_type
PHONE_TEXT SMS code phone_code, phone_num, phone_text_template_type
WHATSAPP WhatsApp code phone_code, phone_num

5.2 email_template_type and phone_text_template_type values

Template value Purpose
register_code Registration code
login_code Login code
reset_pass_code Password-reset code
login_mfa_code MFA login code; also requires mfa_token

5.3 Parameters

Parameter Type Required condition Description
type string Always EMAIL, PHONE_TEXT, or WHATSAPP
email string type = EMAIL Email address
email_template_type string EMAIL register_code, login_code, reset_pass_code, or login_mfa_code
phone_code string PHONE_TEXT, WHATSAPP Calling code without +, for example 86
phone_num string type = PHONE_TEXT/WHATSAPP Full phone number without the calling code
phone_text_template_type string PHONE_TEXT register_code, login_code, reset_pass_code, or login_mfa_code
mfa_token string Template is login_mfa_code MFA token returned by the login endpoint
ticket string CAPTCHA is enabled and supplied by the app Tencent CAPTCHA ticket; the SDK flow overwrites it when applicable
ticket_rand_str string CAPTCHA is enabled and supplied by the app Random string paired with the Tencent CAPTCHA ticket
redirect_uri string Optional Overrides the configured redirect URI
scope string[] Optional Overrides the configured scopes
state string Optional Overrides the configured state
grant_type string Optional Overrides the configured grant type
device_id string Optional Device fingerprint; the SDK may generate one when omitted

email_template_type is used only with EMAIL, and phone_text_template_type only with PHONE_TEXT. WHATSAPP does not use a template parameter.

ts Copy
await rootAuth.sendCode({
  type: 'EMAIL',
  email: 'user@example.com',
  email_template_type: 'register_code',
})
ts Copy
await rootAuth.sendCode({
  type: 'PHONE_TEXT',
  phone_code: '86',
  phone_num: '13800000000',
  phone_text_template_type: 'login_code',
})

When CAPTCHA is enabled, the SDK runs Tencent CAPTCHA automatically. The business UI controls delivery frequency and the 60-second countdown.


6. Registration: register()

register() supports email, phone, username, and WhatsApp registration. This section lists registration type values, required fields, and complete code-registration calls.

6.1 Complete type list

type Registration method Required fields
EMAIL_CODE_PASS Email code and password email, code, password
PHONE_CODE_PASS Phone code and password phone_code, phone_num, code, password
USER_NAME_PASS Username and password user_name, password
WHATSAPP_CODE WhatsApp verification-code registration phone_code, phone_num, code

6.2 Parameters

Parameter Type Required condition Description
type string Always Registration type listed above
email string EMAIL_PASS, EMAIL_CODE Email address
phone_code string PHONE_CODE_PASS, PHONE_CODE, WHATSAPP_CODE Calling code without +
phone_num string PHONE_CODE_PASS, PHONE_CODE, WHATSAPP_CODE Phone number without calling code
user_name string USER_NAME_PASS Username
code string | number EMAIL_CODE, PHONE_CODE, WHATSAPP_CODE Verification code
password string EMAIL_PASS, PHONE_CODE_PASS, USER_NAME_PASS Plaintext; encrypted by the SDK
redirect_uri string Optional Overrides the client redirect URI
scope string[] Optional Overrides the authorization scopes
state string Optional Overrides the state value
grant_type string Optional Overrides the grant type
device_id string Optional Device fingerprint
ts Copy
await rootAuth.register({
  type: 'EMAIL_CODE_PASS',
  email: 'user@example.com',
  code: '123456',
  password: 'password',
})
ts Copy
await rootAuth.register({
  type: 'USER_NAME_PASS',
  user_name: 'rootauth-user',
  password: 'password',
})

Passwords are encrypted with the application's RSA public key. Without onSuccess, the SDK may redirect based on the backend response or automatically log in when there is no redirect_url.


7. Reset password: resetPassword()

Password reset supports submitting a verification code directly or validating it first and then using the returned verify_token.

7.1 Verify code: verifyResetPasswordCode()

Parameter Type Required condition Description
type string Always EMAIL_CODE or PHONE_CODE
email string EMAIL_CODE Email address
phone_code string PHONE_CODE Calling code without +
phone_num string PHONE_CODE Phone number without the calling code
code number Always Verification code
ts Copy
const verification = await rootAuth.verifyResetPasswordCode({
  type: 'EMAIL_CODE',
  email: 'user@example.com',
  code: 123456,
})

The response data contains verify_token.

7.2 Reset password: resetPassword()

At least one of code and verify_token is required.

Parameter Type Required condition Description
type string Always EMAIL_CODE or PHONE_CODE
email string EMAIL_CODE Email address
phone_code string PHONE_CODE Calling code without +
phone_num string PHONE_CODE Phone number without the calling code
password string Always New password
password_confirmation string Always Confirmation password
code string | number Required without verify_token Verification code
verify_token string Required without code Token returned by the code verification API
redirect_uri, scope, state, grant_type Global config types No Override the global config for this request
ts Copy
await rootAuth.resetPassword({
  type: 'EMAIL_CODE',
  email: 'user@example.com',
  verify_token: verification.data.data.verify_token,
  password: 'NewPassword123',
  password_confirmation: 'NewPassword123',
})

8. MFA: verifyLoginMfa()

verifyLoginMfa() handles email, phone, and TOTP verification as well as first-time binding. This section explains each mfa_type, code content, and additional fields.

mfa_type Verification method code content Additional fields
EMAIL Verify bound email Email code None
EMAIL_BIND Bind and verify email Email code email
PHONE_CODE Verify bound phone SMS code Usually the phone from the login response
PHONE_CODE_BIND Bind and verify phone SMS code phone_code, phone_num
TOTP Verify bound authenticator Authenticator code None
TOTP_BIND Bind an authenticator for the first time Authenticator code totp_bind_code when totp_bind_need_verify_code=true

8.1 Parameters

Parameter Type Required condition Description
mfa_token string Always MFA token returned by login
mfa_type string Always EMAIL, EMAIL_BIND, PHONE_CODE, PHONE_CODE_BIND, TOTP, or TOTP_BIND
code string | number Always Email, SMS, or TOTP code
email string EMAIL_BIND Email to bind
phone_code string PHONE_CODE_BIND Calling code without +
phone_num string PHONE_CODE_BIND Phone number to bind
totp_bind_code string TOTP_BIND with totp_bind_need_verify_code=true Code used to bind the authenticator
ts Copy
await rootAuth.verifyLoginMfa({
  mfa_type: 'TOTP',
  code: '123456',
})

mfa_methods may contain multiple options. For TOTP_BIND, the login response may also contain totp_secret for QR-code generation.


9. Third-party login

Third-party authentication covers Google OAuth, Telegram, Facebook, VK, and WeChat Official Account QR flows. This section distinguishes redirects, Google One Tap, and QR polling, and clarifies that WhatsApp is not third-party OAuth.

9.1 thirdPartyLogin()

Parameter Type Required condition Description
auth_by string Always google_oauth, telegram, facebook, or vkid
response_type string Optional OAuth response type
redirect_uri string Optional Overrides the client redirect URI
scope string[] Optional Overrides the authorization scopes
state string Optional Overrides the state value
grant_type string Optional Overrides the grant type
device_id string Optional Device fingerprint
auth_by Provider
google_oauth Google OAuth
telegram Telegram
facebook Facebook
vkid VK ID
ts Copy
await rootAuth.thirdPartyLogin({
  auth_by: 'google_oauth',
})

thirdPartyLogin() is only for Google OAuth, Telegram, Facebook, and VK. It obtains the authorization URL and redirects to the page configured by redirect_uri.

WhatsApp is not a third-party OAuth provider; both WhatsApp login and registration use login() or register() with type: 'WHATSAPP_CODE'.

9.2 Google One Tap

ts Copy
const rootAuth = new RootAuthClient({
  app_id: 'your-app-id',
  enable_google_one_tap: true,
})

await rootAuth.googleOneTapLogin()

Google One Tap must be enabled in application configuration and requires Google's browser script and a valid client ID.

9.3 WeChat Official Account QR code

WeChat Official Account does not use thirdPartyLogin(). Call wechatServiceQrLogin() to obtain QR authorization data, then query the scan result with getThirdPartyQrResult() and the returned state_key.

ts Copy
const qr = await rootAuth.wechatServiceQrLogin()

const result = await rootAuth.getThirdPartyQrResult({
  state_key: qr.data.data.state_key,
})

Display qr.data.data.qr_url or authorize_url and use expire_seconds as the polling deadline. getThirdPartyQrResult() performs one query; the application must schedule polling and stop on success, expiry, or view disposal.


10. Session, configuration, and language

This section summarizes current-user lookup, logout, application configuration, and locale APIs. These supporting methods share the same Core client as login and registration.

Method Purpose
getAppConfig() Fetch application login, registration, branding, agreement, and MFA settings
getToken() Read the current access token
isAuthenticated() Check whether the client has an access token
logout() End the current session
setAppId(appId) Change the application ID
setLocale(locale) Switch request language
ts Copy
await rootAuth.getAppConfig()
rootAuth.getToken()
rootAuth.isAuthenticated()
rootAuth.logout()
rootAuth.setAppId('another-app-id')
rootAuth.setLocale('en-US')

Applications that do not build every authentication screen can use Web Components, Vue Hosted, or React Hosted. This section links each integration and explains its level of UI ownership.

Previous
Frontend Integration
Next
Vue Mode Integration
Last modified: 2026-08-28Powered by