Bev*_*qua 0 authentication data-migration ruby-on-rails ruby-on-rails-5 auth0
我的项目目前用于has_secure_password处理用户(类似于这个railscast)。一切正常,但展望未来,我觉得切换到 Auth0 会带来好处,并希望在太多用户注册之前立即进行转换。
用户被保存到具有多个关联(签入/评论/等)的用户模型中。如何将 Auth0 用户链接到我的数据库中的用户记录。我想我可以在他们登录后通过电子邮件查找(因为它们将是唯一的)或 Auth0 用户 ID(并向用户表中添加一个新列)。似乎我可以简单地使用 Auth0 登录回调来登录用户(使用我当前的会话控制器),但是当在 OmniAuth 中创建用户时,如何在我的数据库上创建用户。是否也有回调?
在一个完美的世界中,我想做这样的事情:
我想要的可能吗?或者我应该坚持我当前的身份验证?
实现 Auth0 授权与任何其他 OAuth 提供程序(例如 Facebook、Twitter、Github 等)之间没有重大区别。Auth0 快速入门指南有点奇怪,因为它没有显示如何将凭据链接到应用程序中的用户模型。相反,他们只是将整个shebang推入会话中。
此处唯一需要对用户模型进行的真正更改是添加一个uid列,用于存储uid用户的 Auth0 。
OmniAuth 的 OAuth 流程基本上是这样工作的:
callbackorfailure方法。uid来自auth 散列在您的应用程序中查找用户或从auth 散列创建一个新用户。class Auth0Controller < ActionController::Base
# the signed up or logged in on auth0
# GET "/auth/auth0/callback"
def callback
auth_hash = request.env['omniauth.auth']
@user = User.find_or_intialize_by(uid: auth_hash[:uid])
if @user.new_record?
@user.name = auth_hash.name
@user.email = auth_hash.email # etc
@user.save!
end
reset_session
session[:user_id] = @user.id
redirect_to root_path, success: "Hello #{@user.name}"
end
# the user declined to give your app permissions
# GET "/auth/failure"
def failure
redirect_to root_path, alert: "Oh noes"
end
end
Run Code Online (Sandbox Code Playgroud)
看:
| 归档时间: |
|
| 查看次数: |
650 次 |
| 最近记录: |