请输入
菜单

JavaScript SDK

RootAuth 提供 JavaScript SDK,帮助您在 Web 应用中快速集成登录、注册、授权等身份认证功能。SDK 支持两种接入模式,您可以根据项目需求灵活选择。

 

1.接入模式概览

模式 说明 适用场景
Hosted(托管模式) 使用 RootAuth 提供的完整登录/注册/重置密码页面(含 UI 和接口) 希望快速上线,对认证页面样式有基本要求
API(接口模式) 仅调用 RootAuth 的 API 接口,完全自定义 UI 需要与产品原有设计风格保持一致,或深度定制认证流程

两种模式的共同点

  • 都需要在工作台创建应用,获取 app_id

  • 都需要创建 RootAuthClient 实例

 

2. 准备工作

创建应用并获取 app_id

  1. 登录 RootAuth 工作台

  2. 创建或选择您的应用

  3. 在应用配置页面复制 APP ID(格式如 app_******

3. 安装 SDK

在项目根目录执行以下命令安装正式版本:

npm install --save rootauth-js

注意:请勿下载 beta 版本(测试版),仅使用正式版本。

 

4. 公共步骤

创建 RootAuthClient 实例

在项目的入口文件(如 main.js)中创建客户端实例,并根据需要注入到全局对象。

// main.js
import type { Router, RouteRecordRaw } from 'vue-router'
import { RootAuthClient, type RootAuthClientConfig } from 'rootauth-js'

const options: RootAuthClientConfig = {
    app_id: 'app_******', // 必填项,值为工作台创建应用生成的app_id
  }
  const client = new RootAuthClient(options)

完整配置项(RootAuthClientConfig)

 interface RootAuthClientConfig {
  app_id: string // 必填项,从工作台应用配置获取
  mode?: 'api' | 'hosted'  // 使用模式,默认为hosted,即使用RootAuth页面
  /** hosted模式下路由基础路径,如 '/auth' 则路由为 /auth/login、/auth/register 等;不传则从站点根路径 / 开始 */
  base_path?: string
  api_base_url?: string // 请求后端api接口的路径,默认为https://rootauth-api.rootauth.com, 如开发环境需要修改到开发环境的路径https://rootauth-api.rootauth.dev
  redirect_uri?: string // 登录成功后跳转的路径,默认为根路径location.origin
  state?: string // 自定义参数,原样在接口返回
  locale?: string // 传入当前语言,默认为zh-CN,当前支持zh-CN、en-US
  scope?: string[]  // scope映射关系,默认为['openid']
  grant_type?: string // 授权模式,默认为authorization_code
  enable_google_one_tap?: boolean // 是否开启Google Onetap登陆,默认为false
  /** hosted模式登录成功回调;返回 false 时不执行默认跳转 */
  onLoginSuccess?: LoginSuccessCallback;
  /** hosted模式注册成功回调;返回 false 时不执行默认跳转 */
  onRegisterSuccess?: RegisterSuccessCallback;
}

 

至此,公共部分已完成。接下来根据您选择的接入模式继续配置。

 

5. Hosted 模式(托管模式)

此模式为默认模式,会启用 RootAuth 自带的完整登录/注册/重置密码页面。

 

5.1 路由说明

Hosted 模式会占用以下三个路由,请避免在您的应用路由中重复定义:

  • /login

  • /register

  • /reset-password

如需自定义基础路径,可通过 base_path 配置项统一添加前缀(例如设为 /auth 后,路由变为 /auth/login 等)。

 

5.2 挂载路由

调用实例的 install 方法,传入您的 Vue Router 实例,SDK 会自动将认证路由合并到您的路由系统中。

// main.js
import type { Router, RouteRecordRaw } from 'vue-router'
import { RootAuthClient, type RootAuthClientConfig } from 'rootauth-js'

const options: RootAuthClientConfig = {
    app_id: 'app_******', // 必填项,值为工作台创建应用生成的app_id
  }
  const client = new RootAuthClient(options)

  client.install(router as Router)

 

5.3 动态切换语言

如果您在系统中需要切换语言,可以将 client 实例注入到全局,然后调用:

client?.setLocale('en-US')

 

5.4 Hosted 模式完整示例

// main.js
import type { Router, RouteRecordRaw } from 'vue-router'
import { RootAuthClient, type RootAuthClientConfig } from 'rootauth-js'

const options: RootAuthClientConfig = {
    app_id: 'app_******', // 必填项,值为工作台创建应用生成的app_id
  }
const client = new RootAuthClient(options)

  client.install(router as Router)

 

6. API 模式(接口模式)

此模式仅提供 API 调用,不包含任何 UI。您需要自己实现登录/注册等页面,并调用 SDK 提供的方法完成认证。

 

6.1 创建客户端实例

(必须指定 mode: 'api')

import { RootAuthClient, type RootAuthClientConfig } from 'rootauth-js'

const options: RootAuthClientConfig = {
    app_id: 'app_******', // 必填项,值为工作台创建应用生成的app_id
    mode: 'api' // api模式必填
  }
  const client = new RootAuthClient(options)

 

6.2 注入全局实例

(以 Nuxt 为例)

其他框架(如 Vue、React)可参考类似思路,将 client 挂载到全局或使用 Context/Provider。

 

6.2.1 创建插件文件

 plugins/rootauth.client.ts

// plugins/rootauth.client.ts
import { RootAuthClient, type RootAuthClientConfig } from 'rootauth-js'
export default defineNuxtPlugin(() => {
  const options: RootAuthClientConfig = {
    app_id: 'app_******', // 必填项,值为工作台创建应用生成的app_id
    mode: 'api' // api模式必填
  }
  const client = new RootAuthClient(options)

  return {
    provide: {
      rootauth: client,
    },
  }
})

 

6.2.2 注册插件 

nuxt.config.ts

// nuxt.config.ts
export default defineNuxtConfig({
  plugins: [
    '@/plugins/rootauth.client.ts',
  ]
})

 

6.2.3 在组件中获取实例

const { $rootauth } = useNuxtApp()
$rootauth?.xxx()

 

6.2.4 封装 Composable(可选,推荐)

创建 composables/useRootAuth.ts

// composables/useRootAuth.ts
import type { RootAuthClient } from 'rootauth-js'

/** RootAuth 客户端(由 `plugins/rootauth.client` 在配置了 `ROOTAUTH_APP_ID` 时注入) */
export function useRootAuth(): RootAuthClient | undefined {
  return useNuxtApp().$rootauth as RootAuthClient | undefined
}

此时rootauth对象的使用方式更简洁

const rootauth = useRootAuth()
rootauth?.xxx()

 

6.3 API 方法说明

登录、注册、发送验证码和重置密码均返回 Promise,第二个参数可传入自定义回调(onSuccess/onFailure)来覆盖 SDK 默认行为。

若只需补充额外逻辑(如关闭 loading),请直接使用 Promise(.then().catch()),不要传入第二个参数,SDK 会保留默认行为。

6.3.1 登录

const rootauth = useRootAuth()

interface LoginParams {
  type: string // 必填项,登陆方式:EMAIL_PASS|EMAIL_CODE,现在支持账号密码登陆EMAIL_PASS和邮箱验证码登陆EMAIL_CODE
  email: string // 必填项,用户邮箱
  password?: string // 当type为EMAIL_PASS必填,注意:仅需要传入明文密码,RootAuth进行加密处理
  code?: string // 当type为EMAIL_CODE必填
}
interface RootAuthFlowCallbacks<T = unknown> {
  onSuccess?: (data: T) => void
  onFailure?: (error: unknown) => void
}

//可使用的类型
rootauth?.login(
  params: LoginParams,
  callbacks?: RootAuthFlowCallbacks<
    AxiosResponse<ApiResponse<{ redirect_url: string }>>
  >,
)
.then((res) => {
  // 基于当前的RootAuth逻辑,补充自有逻辑
})
.catch((err) => {
  console.log('err', err)
})
.finally(() => {
  // 关闭loading等
})

带自定义回调示例:

const handleSubmit=()=>{
  // 用户自行处理前置校验
  loading.value = true
  rootauth?.login({
    type:'EMAIL_PASS',
    email:'123456@qq.com',
    password:'123456'
  },
  {
    onSuccess:()=>{
      // 用户自己的登录成功回调,包括提示等
    },
    onFailure:()=>{
      // 用户自己的登录失败回调,包括提示等
    }
  })
  .finally(() => {
    loading.value = false
  })
}

 

6.3.2 注册

interface RegisterParams {
  type?: string // 用户注册类型:EMAIL_CODE,默认为邮箱验证码注册EMAIL_CODE
  email: string // 必填项,用户邮箱
  code: string // 必填项,邮箱激活码
  password: string // 必填项,密码,注意:仅需要传入明文密码,RootAuth进行加密处理
}

const params: RegisterParams = {
  email: '123456@qq.com',
  password: '123456',
  code: '123456',
}
  
rootauth?.register(params)
.then((res) => {
  // 基于当前的RootAuth逻辑,补充自有逻辑
})
.catch((err) => {
  console.error(err)
})
.finally(() => {
})

 

6.3.3 发送验证码

interface SendCodeParams {
  type?: string // 发送验证码类型:EMAIL,默认为邮箱验证码EMAIL
  email: string // 必填项,用户邮箱
  email_template_type: string // 必填项,选择的模板类型:register_code|login_code|reset_pass_code,register_code:注册验证码,login_code:登录验证码,reset_pass_code:重置验证码
}

const params: SendCodeParams = {
  email: '123456@qq.com',
  email_template_type: 'register_code'
}
  
rootauth?.sendCode(params)
.then((res) => {
  // 基于当前的RootAuth逻辑,补充自有逻辑
})
.catch((err) => {
  console.error(err)
})
.finally(() => {
})

 

6.3.4 重置密码

interface ResetPasswordUParams {
  type?: string // 发验证码类型:EMAIL_CODE,默认为邮箱验证码EMAIL_CODE
  email: string
  password: string // 新密码,注意:仅需要传入明文密码,RootAuth进行加密处理
  password_confirmation: string // 确认密码,注意:仅需要传入明文密码,RootAuth进行加密处理
  code: string
}

const params: ResetPasswordUParams = {
  email: '123456@qq.com',
  password: '123456',
  password_confirmation: '123456',
  code: '111111'
}
  
rootauth?.resetPassword(params)
.then((res) => {
  // 基于当前的RootAuth逻辑,补充自有逻辑
})
.catch((err) => {
  console.error(err)
})
.finally(() => {
})

 

6.3.5 第三方登录(Google OAuth)

interface ThirdPartyLoginParams {
  auth_by: string // 第三方类型:google_oauth,google_oauth:谷歌登陆
}

const params: ThirdPartyLoginParams = {
  auth_by: 'google_oauth'
}
  
rootauth?.thirdPartyLogin(params)
.then((res) => {
  // 基于当前的RootAuth逻辑,补充自有逻辑
})
.catch((err) => {
  console.error(err)
})
.finally(() => {
})

 

6.3.6 Google One Tap 登录

需先在配置中开启 enable_google_one_tap: true

rootauth?.googleOneTapLogin()

 

 

上一个
开发集成
最近修改: 2026-04-15Powered by