logologo
开始
手册
开发
插件
API
English
简体中文
开始
手册
开发
插件
API
English
简体中文
logologo

用户认证

概述
认证器管理

认证类型

密码
短信

OIDC

配置

示例

谷歌登录
微软登录

SAML

配置

示例

谷歌登录
LDAP
CAS
企业微信
钉钉
API 密钥

开发指南

扩展认证类型
API 参考

验证

概述

验证类型

短信
TOTP 认证器

开发指南

扩展验证类型
扩展验证场景
扩展短信服务商
API 参考
双因素身份认证 (2FA)
Previous Page扩展验证场景
Next PageAPI 参考

#扩展短信服务商

本文主要介绍如何通过插件的形式扩展 验证:短信 功能中的短信服务商。

#客户端

#注册配置表单

用户在配置短信验证器时,选择短信服务商类型后,会出现一个和该服务商类型关联的配置表单,这个配置表单需要开发者在客户端自行注册。

import { Plugin, SchemaComponent } from '@nocobase/client';
import PluginVerificationClient from '@nocobase/plugin-verification/client';
import React from 'react';

const CustomSMSProviderSettingsForm: React.FC = () => {
  return <SchemaComponent schema={{
    type: 'void',
    properties: {
      accessKeyId: {
        title: `{{t("Access Key ID", { ns: "${NAMESPACE}" })}}`,
        type: 'string',
        'x-decorator': 'FormItem',
        'x-component': 'TextAreaWithGlobalScope',
        required: true,
      },
      accessKeySecret: {
        title: `{{t("Access Key Secret", { ns: "${NAMESPACE}" })}}`,
        type: 'string',
        'x-decorator': 'FormItem',
        'x-component': 'TextAreaWithGlobalScope',
        'x-component-props': { password: true },
        required: true,
      },
    }
  }} />
}

class PluginCustomSMSProviderClient extends Plugin {
  async load() {
    const plugin = this.app.pm.get('verification') as PluginVerificationClient;
    plugin.smsOTPProviderManager.registerProvider('custom-sms-provider-name', {
      components: {
        AdminSettingsForm: CustomSMSProviderSettingsForm,
      },
    });
  }
}

#服务端

#实现发送接口

验证插件已经对创建一次性动态验证码 (OTP) 的流程实现了封装,开发者只需要实现与短信服务商交互的发送逻辑即可。

class CustomSMSProvider extends SMSProvider {
  constructor(options) {
    super(options);
    // options 为客户端的配置对象
    const options = this.options;
    // ...
  }

  async send(phoneNumber: string, data: { code: string }) {
    // ...
  }
}

#注册验证类型

发送接口实现好以后,需要进行注册。

import { Plugin } from '@nocobase/server';
import PluginVerificationServer from '@nocobase/plugin-verification';
import { tval } from '@nocobase/utils';

class PluginCustomSMSProviderServer extends Plugin {
  async load() {
    const plugin = this.app.pm.get('verification') as PluginVerificationServer;
    // name 需要和客户端对应
    plugin.smsOTPProviderManager.registerProvider('custom-sms-provider-name', {
      title: tval('Custom SMS provider', { ns: namespace }),
      provider: CustomSMSProvider,
    });
  }
}