auth0 用户存储与自己的用户存储——它们如何协同工作?

auth0 user store vs. own user store - how do they work together?

我正在熟悉 auth0 并将其设置在仅 rails 6 api 的应用程序上。我计划围绕不同的资源等建立一个数据库。这些资源之一是用户资源。这将用于身份验证、授权和一些用户数据、followers/following 关系等。 Auth0 提供他们自己的用户存储,声称这是性能最高的,并且可能是最容易设置的,因为他们为您做所有事情,存储加密密码等。我很想利用这一点,但我想知道这个 auth0 用户存储的可定制性如何。它与我自己的数据库一起工作有多容易,它将存储其他资源,如 follower/following 关系等? 如果我想为用户添加更多自定义属性怎么办 table?

用户将成为我的应用程序的基本组成部分,并且会有以用户为中心的复杂计算和操作。 auth0 数据库 suitable 是为了这个吗?或者它更像是一个用于大多数静态网站的简单登录存储?有人有这方面的经验吗?

感谢任何建议!

auth0提供基于OAuth协议的认证机制。

该服务允许您将用户身份验证管理外包给您的应用程序。它基于 OAuth 协议,这与经典的 OAuth 集成(如 facebook 或 google 登录)并没有什么不同。

按照 this tutorial 实现所需内容的最简单方法是在数据库中创建 users table 并在身份验证回调期间导入信息。 request.env['omniauth.auth'] 散列包含有关从 auth0 登录的用户的所有信息,包括电子邮件等。该散列中有一个唯一 ID,您可以使用它来将 auth0 用户映射到您当前的数据库用户。如果用户不存在,只需创建它。

# auth hash structure from auth0:
#{
#  :provider => 'auth0',
#  :uid => 'auth0|USER_ID',
#  :info => {
#    :name => 'John Foo',
#    :email => 'johnfoo@example.org',
#    :nickname => 'john',
#    :image => 'https://example.org/john.jpg'
#  },
#  :credentials => {
#    :token => 'ACCESS_TOKEN',
#    :expires_at => 1485373937,
#    :expires => true,
#    :refresh_token => 'REFRESH_TOKEN',
#    :id_token => 'JWT_ID_TOKEN',
#    :token_type => 'bearer',
#  },
#  :extra => {
#    :raw_info => {
#      :email => 'johnfoo@example.org',
#      :email_verified => 'true',
#      :name => 'John Foo',
#      :picture => 'https://example.org/john.jpg',
#      :user_id => 'auth0|USER_ID',
#      :nickname => 'john',
#      :created_at => '2014-07-15T17:19:50.387Z'
#    }
#  }
#}

# app/controllers/auth0_controller.rb

class Auth0Controller < ApplicationController
  def callback
    # This stores all the user information that came from Auth0
    # and the IdP

    # request.env['omniauth.auth'] contains all user data
    auth_data = request.env['omniauth.auth']
    user = User.find_or_create_by(auth0_id: auth_data[:uid]) do |user|
      user.email = auth_data[:info][:email]
      # associate any other data from auth0 you need here
    end

    session[:userinfo] = request.env['omniauth.auth']

    # Redirect to the URL you want after successful auth
    redirect_to '/dashboard'
  end

  def failure
    # show a failure page or redirect to an error page
    @error_msg = request.params['message']
  end
end

由于用户信息存储在您的会话中,您可以使用此数据根据您存储的 uid 获取当前用户。这样您就可以在自己的 users table.

中获取所需的任何信息

希望对您有所帮助!