根据用户邮箱自动选择Auth0 DB Connection

Automatically choose Auth0 DB Connection according to user's email address

我已经阅读了已发布的 multi-tenancy guide,我相信我的应用程序所需的解决方案是为我注册的每个组织创建一个单独的数据库连接。

我的问题是,由于我要将 connection 参数设置为每个客户端的不同名称,我希望通用登录能够根据用户的电子邮件自动确定数据库连接名称地址。因此,我不想让用户手动提供某种提示,我应该根据哪个数据库连接对他们进行身份验证,我想以某种方式自动确定这一点。

有什么办法吗?

我假设您使用的是托管登录页面。根据客户端确定 connection 的最简单方法是在重定向到 /authorize 端点时传递连接参数。因此,Lock 将使用 URL 中传递的连接参数作为连接来验证用户。例如:

 https://[tenant]/authorize?
 client_id=K8B5DJdStcZtUzbhaxAOzCrXNbo2kmXG&
 response_type=token%20id_token&
 redirect_uri=http://application_url&
 scope=openid%20profile%20email%20&
 connection=connection_name&state=123&nonce=345

auth0.js和auth0-spa-js都可以用来传递额外的参数(连接)。

如果您使用 Hosted Login Page+ Lock,第二种方法是使用 connectionResolver 选项。

connectionResolver {功能}: 使用时,提供一个扩展点,可以根据用户名信息选择使用哪个连接。具有用户名、上下文和回调作为参数。回调需要一个对象,如:{type: 'database', name: 'connection name'}.

var options = {
  connectionResolver: function (username, context, cb) {
    var domain = username.includes('@') && username.split('@')[1];
    if (domain) {
      // If the username is test@auth0.com, the connection used will be the `auth0.com` connection.
      // Make sure you have a database connection with the name `auth0.com`.
      cb({ type: 'database', name: domain });
    } else {
      // Use the default approach to figure it out the connection
      cb(null);
    }
  }
}

您可以利用 context 对象来识别客户端 (context.clientID) 并选择连接,而不是用户名。