Authing Guard 文档Web Guard
指南
在线体验
社区
博客
更新日志
关于
Github
  • 使用指南

    • 介绍
    • 安装
    • 快速开始
    • 初始配置
    • 类型定义
  • 基础

    • 创建一个 Authing 应用 (opens new window)
    • 了解 Guard 使用模式
    • 控制台配置与本地配置
  • API

    • 登录
    • 登出
    • 获取用户信息
    • 切换视图
    • 事件
    • 切换语言
    • 自定义样式
    • 刷新 Token
    • 操作登录注册协议
  • 高级功能

    • 使用 JS SDK
    • SSO 单点登录
    • 第三方身份源登录
    • MFA 多因素认证
    • 配置安全域
    • 私有化部署

¶ Guard 事件

使用 Guard 提供的 on 方法可以对 Guard 支持的事件进行监听。

// React 16 / 17
// 代码示例:https://github.com/Authing/Guard/tree/dev-v6/examples/guard-react/normal/src/pages/Embed.tsx
import { useGuard, User } from "@authing/guard-react";

// React 18
// 代码示例:https://github.com/Authing/Guard/tree/dev-v6/examples/guard-react18/normal/src/pages/Embed.tsx
// import { useGuard, User } from "@authing/guard-react18";

import React, { useEffect } from "react";

export default function Login() {
  const guard = useGuard();

  useEffect(() => {
    // 使用 start 方法挂载 Guard 组件到你指定的 DOM 节点,登录成功后返回 userInfo
    guard.start("#authing-guard-container").then((userInfo: User) => {
      console.log("userInfo: ", userInfo);
    });

    guard.on("login", (userInfo: User) => {
      // 自定义登录成功后的业务逻辑
      // 比如跳转到对应的某个页面等
      console.log("userInfo in login: ", userInfo);
    });
  }, []);

  return (
    <div>
      <div id="authing-guard-container"></div>
    </div>
  );
}
// 代码示例:https://github.com/Authing/Guard/tree/dev-v6/examples/guard-vue2/normal/src/views/Embed.vue
export default {
  mounted() {
    // 使用 start 方法挂载 Guard 组件到你指定的 DOM 节点,登录成功后返回 userInfo
    this.$guard.start("#authing-guard-container").then((userInfo) => {
      console.log(userInfo);
    });

    this.$guard.on("login", (userInfo) => {
      // 自定义登录成功后的业务逻辑
      // 比如跳转到对应的某个页面等
      console.log("userInfo in login: ", userInfo);
    });
  },
};
<script lang="ts" setup>
  // 代码示例:https://github.com/Authing/Guard/tree/dev-v6/examples/guard-vue3/normal/src/views/Embed.vue
  import { onMounted } from "vue";

  import { useGuard } from "@authing/guard-vue3";

  import type { User } from "@authing/guard-vue3";

  const guard = useGuard();

  onMounted(() => {
    // 使用 start 方法挂载 Guard 组件到你指定的 DOM 节点,登录成功后返回 userInfo
    guard.start("#authing-guard-container").then((userInfo: User) => {
      console.log("userInfo: ", userInfo);
    });

    guard.on("login", (userInfo: User) => {
      // 自定义登录成功后的业务逻辑
      // 比如跳转到对应的某个页面等
      console.log("userInfo in login: ", userInfo);
    });
  });
</script>
// 代码示例:https://github.com/Authing/Guard/tree/dev-v6/examples/guard-angular/normal/src/app/pages/embed/embed.component.ts
import { Component, ChangeDetectorRef } from "@angular/core";

import { GuardService, User } from "@authing/guard-angular";

@Component({
  selector: "embed-container",
  templateUrl: "./embed.component.html",
  styleUrls: ["./embed.component.css"],
})
export class EmbedComponent {
  constructor(private guard: GuardService) {}

  ngOnInit() {
    // 使用 start 方法挂载 Guard 组件到你指定的 DOM 节点,登录成功后返回 userInfo
    this.guard.client
      .start("#authing-guard-container")
      .then((userInfo: User) => {
        console.log(userInfo);
      });

    this.guard.client.on("login", (userInfo: User) => {
      // 自定义登录成功后的业务逻辑
      // 比如跳转到对应的某个页面等
      console.log("userInfo in login: ", userInfo);
    });
  }
}
// 代码示例:https://github.com/Authing/Guard/tree/dev-v6/examples/guard/normal/embed.html
guard.on("login", (userInfo) => {
  // 自定义登录成功后的业务逻辑
  // 比如跳转到对应的某个页面等
  console.log(userInfo);
});

完整事件如下:

  • 如果配置了登录注册合并,将只会触发 login 事件,不会触发 register 事件。
  • 事件回调函数参数类型参考:类型定义
// 关于 AuthenticationClient 可参考:https://docs.authing.cn/v2/reference/sdk-for-node/authentication/

// Guard 初始化完成,开始渲染页面
guard.on('load', (authenticationClient: AuthenticationClient) => {})

// Guard 初始化失败
guard.on('load-error', (error: any) => {})

// 用户触发登录前(返回<boolean | Promise<boolean>>用于控制本次登录是否继续)。
guard.on('before-login', (
  loginParams: NomalLoginParams | VerifyCodeLoginParams | ScanLoginParams,
  authenticationClient: AuthenticationClient
) => {})

// 用户登录成功,可以在回调函数中自定义登录后的业务逻辑
guard.on('login', (
  user: User, 
  authenticationClient: AuthenticationClient
) => {})

// 用户登录失败
guard.on('login-error', (error: ILoginError) => {})

// 用户触发注册前(返回<boolean | Promise<boolean>>用于控制本次注册是否继续)。
guard.on('before-register', (
  registerParams: RegisterParams,
  authenticationClient: AuthenticationClient
) => {})

// 用户注册成功
guard.on('register', (
  user: User, 
  authenticationClient: AuthenticationClient
) => {})

// 用户注册失败
guard.on('register-error', (error: any) => {})

// 邮件发送成功
guard.on('email-send', (
  sence: IEmailScene, 
  authenticationClient: AuthenticationClient
) => {})

// 邮件发送失败
guard.on('email-send-error', (
  error: any, 
  sence: IEmailScene, 
  authenticationClient: AuthenticationClient
) => {})

// 短信验证码发送成功
guard.on('phone-send', (
  sence: ISceneType, 
  authenticationClient: AuthenticationClient
) => {})

// 短信验证码发送失败
guard.on('phone-send-error', (
  error: any,
  sence: ISceneType, 
  authenticationClient: AuthenticationClient
) => {})

// 重置密码成功
guard.on('pwd-reset', (authenticationClient: AuthenticationClient) => {})

// 重置密码失败
guard.on('pwd-reset-error', (
  error: any, 
  authenticationClient: AuthenticationClient
) => {})

// modal 模式中 guard 关闭
guard.on('close', () => {})

// 注册补全成功
guard.on('register-info-completed', (
  user: User, 
  udfs: Object, 
  authenticationClient: AuthenticationClient
) => {})

// 注册补全失败
guard.on('register-info-completed-error', (
  user: User, 
  udfs: Object, 
  authenticationClient: AuthenticationClient
) => {})

// 语言切换
guard.on('lang-change', (lang: Lang) => {})

// Guard 内部 Module 切换前事件(返回<boolean | Promise<boolean>>用于控制本次切换是否继续)
guard.on('before-change-module', (
  moduleType: IGuardModuleType, 
  initData: any
) => {})

// Guard 内部 Module 切换后事件
guard.on('after-change-module', (options: OnAfterChangeModuleOptions) => {})
上一篇: 切换视图 下一篇: 切换语言

用户身份管理

集成第三方登录
手机号闪验
通用登录表单组件
自定义认证流程

企业内部管理

单点登录
多因素认证
权限管理

开发者

开发文档
框架集成
博客
GitHub
社区用户中心

公司

400 888 2106
sales@authing.cn
北京市朝阳区北辰世纪中心 B 座 16 层(总)
成都市高新区天府五街 200 号 1 号楼 B 区 4 楼 406 室(分)

京ICP备19051205号

beian京公网安备 11010802035968号

© 北京蒸汽记忆科技有限公司