创建和添加自定义用户和权限提供程序
¥Creating and adding a custom Users & Permissions provider
Strapi 为 用户和权限功能 提供了 内置提供商 列表。你还可以按照本指南创建自己的提供商。
¥Strapi provides a list of built-in providers for the Users & Permissions feature. You can also create your own provider following this guide.
Prerequisites
你已阅读 用户和权限提供商文档 并了解登录流程。
¥You have read the Users & Permissions providers documentation and understood the login flow.
创建自定义提供程序
¥Creating a custom provider
你可以使用 register
生命周期函数 在 Strapi 应用的 src/index.js|ts
文件中创建自己的自定义提供程序。使用以下根据你的需要调整的代码示例:
¥You can use the register
lifecycle function to create your own custom provider in the src/index.js|ts
file of your Strapi application. Use the following code example adjusted to your needs:
/src/index.js
module.exports = {
register({ strapi }) {
strapi
.plugin("users-permissions")
.service("providers-registry")
.add("example-provider-name", {
icon: "",
enabled: true,
grantConfig: {
key: "",
secret: "",
callback: `${strapi.config.server.url}/auth/example-provider-name/callback`,
scope: ["email"],
authorize_url: "https://awesome.com/authorize",
access_url: "https://awesome.com/token",
oauth: 2,
},
async authCallback({ accessToken, providers, purest }) {
// use whatever you want here to get the user info
return {
username: "test",
email: "test",
};
},
});
},
};