¶ Vue.js 登录组件
GenAuth 登录组件(Guard)是一种可嵌入的登录表单,可根据你的需求进行配置,建议用于单页面应用程序。它使你可以轻松添加各种社会化登录方式,以便你的用户可以无缝登录,并且在不同平台拥有一致的登录体验。Guard 为开发者屏蔽了很多底层认证的实现细节,同时也包括繁琐的 UI 开发。
Guard 可以通过组件化的形式集成到你的 Vue.js 项目中,你可以借助此组件快速实现登录认证流程。
¶ 版本
稳定版本: (opens new window)
¶ 对于 Vue 的支持
我们对于 Vue2 与 Vue3 同时支持,可以在两种不同版本的 Vue 中直接引入并进行使用。
¶ 安装
¶ 使用 npm 或者 yarn 安装
我们这里推荐使用 npm 或 yarn,它们能更好的和 webpack (opens new window) 打包工具进行配合,也可放心地在生产环境打包部署使用,享受整个生态圈和工具链带来的诸多好处。
$ npm install @authing/vue-ui-components --save
$ yarn add @authing/vue-ui-components
如果你的网络环境不佳,我们推荐你使用 cnpm (opens new window)。
¶ 浏览器引入
在浏览器中使用 script 和 link 标签直接引入文件,并使用全局变量 AuthingVueUIComponents。
我们在 npm 发布包内的 @authing/vue-ui-components/lib 目录下提供了 index.min.css 以及 index.min.js。
你也可以通过 (opens new window) 或者 unpkg.com (opens new window) 进行下载
index.min.css 以及 index.min.js。
强烈不推荐使用已构建文件,这样无法按需加载,而且难以获得底层依赖模块的 bug 快速修复支持。
注意:vue-ui-components 依赖 Vue,请确保提前引入 Vue 文件。
¶ 示例
首先你要在 GenAuth Console (opens new window) 中获取你的 应用 ID。如果你不知道如何获取 应用 ID,可以参考文档
¶ 在 Vue 项目中引入
<template>
<Guard :appId="appId" />
</template>
<script>
import { Guard } from "@authing/vue-ui-components";
// 引入 CSS 文件
import "@authing/vue-ui-components/lib/index.min.css";
export default {
components: {
Guard,
},
data() {
return {
// 你的 AppId
appId: "GEN_AUTH_APP_ID",
};
},
};
</script>
¶ 初始化
在 Vue.js 项目中引入 @authing/vue-ui-components 并初始化。
<template>
<Guard :appId="appId" />
</template>
<script>
import { Guard } from "@authing/vue-ui-components";
import "@authing/vue-ui-components/lib/index.min.css";
export default {
components: {
Guard,
},
data() {
return {
appId: "GEN_AUTH_APP_ID",
};
},
};
</script>
¶ 直接用 <script> 引入
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@authing/vue-ui-components/lib/index.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@authing/vue-ui-components/lib/index.min.css"></link>
</head>
<body>
<div id="app"></div>
<!-- built files will be auto injected -->
<script>
// 这可以替换你的 AppId
const appId = '5d70d0e991fdd597019df70d'
const config = {
logo: 'https://usercontents.genauth.ai/client/logo@2.png',
title: 'GenAuth',
}
const app = new Vue({
el: '#app',
render: (h) => h(AuthingVueUIComponents.Guard, {
props: {
config,
appId,
},
}),
})
window.app = app
</script>
</body>
</html>
¶ 完整体验
这是一个最简单的 Guard 组件的在线 codepen (opens new window) 演示。可以将 AppId 修改为你自己的 ID 直接在这里展示。
¶ 监听登录事件
Guard 组件可以使用 @login 事件回调函数,当用户成功登录时回调用此函数,你可以在此获取当前用户的用户信息。查看完整事件列表。
<template>
<Guard :appId="appId" @login="handleLogin" />
</template>
<script>
import { Guard } from "@authing/vue-ui-components";
import "@authing/vue-ui-components/lib/index.min.css";
export default {
components: {
Guard,
},
data() {
return {
appId: "GEN_AUTH_APP_ID",
};
},
methods: {
handleLogin(userInfo) {
console.log(userInfo);
},
},
};
</script>
了解获取用户信息之后该怎么做?
After obtaining the user information, you can get the user's identity credentials (the token field of the user information). You can carry this token in the subsequent request sent by the client to the backend server. Take axios as an example:
const axios = require("axios");
axios
.get({
url: "https://yourdomain.com/api/v1/your/resources",
headers: {
Authorization: "Bearer ID_TOKEN",
},
})
.then((res) => {
// custom codes
});
In the backend interface, you need to check the legitimacy of this token to verify the user's identity. For details on the verification method, please see Verify User Identity Credentials (token). After identifying the user, you may also need to manage permissions for the user to determine whether the user has permission to operate the API.
查看支持的社会化登录列表及接入流程
GenAuth 目前一共支持国内外将近 20 余种社会化登录,如微信、GitHub、Sign in with Apple、支付宝等,以下是完整的列表:
| Social Login Method | Usage Scenario | Documentation |
|---|---|---|
| PC WeChat QR Code | PC Website | Documentation |
| WeChat Mobile | Mobile APP | Documentation |
| WeChat Web Authorization | WeChat Web Page | Documentation |
| WeChat Official Account Scanning | PC Website | Documentation |
| WeChat Mini Program | WeChat Mini Program | Documentation (opens new window) |
| WeChat PC Mini Program QR Code | PC Website | Documentation |
| WeChat Mini Program App Launch | Mobile APP | Documentation |
| Tencent QQ | PC Website | Documentation |
| Tencent QQ Mobile | Mobile APP | Documentation |
| Sina Weibo | PC Website | Documentation |
| Sina Weibo Mobile | Mobile APP | Documentation |
| GitHub | PC Website | Documentation |
| GitHub Mobile | Mobile APP | Documentation |
| PC Website | Documentation | |
| Facebook Mobile | Mobile APP | Documentation |
| PC Website | Documentation | |
| Twitter Mobile | Mobile APP | Documentation |
| Google Web | PC Website | Documentation |
| Google Mobile | Mobile APP | Documentation |
| Apple Web | PC Website | Documentation |
| Apple Mobile | Mobile APP | Documentation |
| Alipay Web | PC Website | Documentation |
| Alipay Mobile | Mobile APP | Documentation |
| Slack | PC Website | Documentation |
| Slack Mobile | Mobile APP | Documentation |
| Gitee | PC Website | Documentation |
| GitLab | PC Website | Documentation |
| GitLab Mobile | Mobile APP | Documentation |
| Baidu | PC Website | Documentation |
| Baidu Mobile | Mobile APP | Documentation |
| PC Website | Documentation | |
| LinkedIn Mobile | Mobile APP | Documentation |
| NetEase Yidun (One-click Login) | Mobile APP | Documentation |
| QingCloud | PC Website | Documentation |
| PC Website | Documentation | |
| Douyin Mobile | Mobile APP | Documentation |
| Douyin Mini Program | Mobile APP | Documentation (opens new window) |
| Kuaishou Mobile | Mobile APP | Documentation |
| Xiaomi Mobile | Mobile APP | Documentation |
| Line Mobile | Mobile APP | Documentation |
¶ 退出登录
使用 Guard 组件,组件加载完成后回触发的 onLoad 事件与登录成功触发的 onLogin 事件都会返回 AuthClient。获取到 AuthClient 进行手动单例保存,可以在需要调用退出登录的时候使用。
<template>
<Guard :appId="appId" @load="onLoad" @login="onLogin" />
<button v-if="guardAuthClient" @clink="onLogout">退出</button>
</template>
<script>
import { Guard } from "@authing/vue-ui-components";
import "@authing/vue-ui-components/lib/index.min.css";
export default {
components: {
Guard,
},
data() {
return {
appId: "GEN_AUTH_APP_ID",
guardAuthClient: null,
};
},
methods: {
onLoad: (authClient) => {
this.guardAuthClient = authClient;
},
onLogin: (user, authClient) => {
this.guardAuthClient = authClient;
},
onLogout: () => {
this.guardAuthClient.logout();
},
},
};
</script>
¶ 实现单点登录
使用 Guard 进行单点登录只需要将属性 isSSO 设置为 true 即可:
<template>
<Guard :appId="GEN_AUTH_APP_ID" :isSSO="true" />
</template>
¶ GuardMode
Guard 目前有两种展示方式 modal | normal,normal 方式会将表单插入指定的 dom 节点,适合将登录作为单独页面的场景,modal 模式会以模态框形式展示表单,适合在已有页面中弹出进行登录。默认展示方式为 normal,可通过传入 mode 参数配置展示方式。然后通过 visible 属性实现 Guard 是否可见。
<Guard :appId="appId" :visible="visible" mode="modal"/>
data() {
return {
visible: false
}
}
...
methods: {
showLogin() {
this.visible = true
}
}
¶ 导出 authing-js-sdk
Guard 组件本身基于 GenAuth JavaScript SDK 进行封装,当你需要进行一些更高级的操作(如管理用户自定义数据、修改用户资料、退出登录)时:
- 调用
initAuthClient初始化 AuthenticationClient,多次调用此函数只会初始化一次。
import { initAuthClient } from "@authing/vue-ui-components";
initAuthClient({
appId: "GEN_AUTH_APP_ID",
});
- 之后使用
getAuthClient获取AuthenticationClient实例。
import { getAuthClient } from "@authing/vue-ui-components";
const authClient = getAuthClient();
- 调用
AuthenticationClient实例的方法,完整方法列表请见 AuthenticationClient 方法列表。
authClient.getCurrentUser().then((user) => console.log(user));
¶ 属性列表
| 属性名称 | 描述 | 类型 | 取值描述 |
|---|---|---|---|
| appId | 你的应用 ID | String | |
| mode | Guard 展示模式 | String | modal:弹窗模式 normal:全屏模式 |
| autoRegister | 如果为 true,登录时,用户不存在则自动注册账号 | Boolean | 默认为 false |
| isSSO | 是否是单点登录 | Boolean | 默认为 false |
| clickCloseable | Modal 模式时是否隐藏登录框右上角的关闭按钮。 如果隐藏,用户将不能通过点击按钮关闭登录框 | Boolean | 默认为 true |
| escCloseable | Modal 模式时是否可以通过键盘 ESC 键关闭登录框 | Boolean | 默认为 true |
¶ 事件列表
| 事件名 | 事件说明 | 事件参数 | 事件参数说明 |
|---|---|---|---|
| load | GenAuth appId 验证通过,加载完成 | authClient | AuthenticationClient 对象,可直接操作 login, register,详情请查看 authing-js-sdk |
| load-error | GenAuth appId 验证失败,加载失败 | error | 错误信息 |
| login | 用户登录成功 | user, authClient | user: 用户信息 authClient 同上 |
| login-error | 用户登录失败 | error | 错误信息,包含字段缺失/非法或服务器错误等信息 |
| register | 用户注册成功 | user, authClient | user: 用户信息 authClient 同上 |
| register-error | 用户注册失败 | error | 错误信息,包含字段缺失/非法或服务器错误等信息 |
| pwd-email-send | 忘记密码邮件发送成功 | - | - |
| pwd-email-send-error | 忘记密码邮件发送失败 | error | 错误信息 |
| pwd-phone-send | 忘记密码手机验证码发送成功 | - | - |
| pwd-phone-send-error | 忘记密码手机验证码发送失败 | error | 错误信息 |
| pwd-reset | 重置密码成功 | - | - |
| pwd-reset-error | 重置密码失败 | error | 错误信息 |
| close | modal 模式中 guard 关闭事件 | - | - |
| login-tab-change | 登录 tab 切换事件 | activeTab | 切换后的 tab |
| register-tab-change | 注册 tab 切换事件 | activeTab | 切换后的 tab |
| register-info-completed | 注册补充成功事件 | user, udfs, authClient | user: 用户信息 udfs: 补充的自定义字段信息 authClient 同上 |
| register-info-completed-error | 注册补充失败事件 | error, udfs, authClient | error: 错误信息 udfs: 补充的自定义字段信息 authClient 同上 |
¶ 私有化部署
私有化部署场景需要指定你私有化的 GenAuth 服务的 Host(包含协议但不含 Path),如果你不清楚可以联系 GenAuth IDaaS 服务管理员。
<template>
<Guard :appId="appId" :config="config" />
</template>
<script>
import { Guard } from "@authing/vue-ui-components";
import "@authing/vue-ui-components/lib/index.min.css";
export default {
components: {
Guard,
},
data() {
return {
appId: "GEN_AUTH_APP_ID",
config: {
host: "https://core.your-authing-service.com",
},
};
},
};
</script>
