Ter*_*nce 5 ruby rubygems ruby-on-rails devise omniauth
我按照本指南尝试将Devise放入另一个可安装的宝石中: 如何:在可安装的引擎内使用设计.
除了omniauth部分,一切似乎都工作得很好.我想让omniauth-google-oauth2工作.我发现它是Devise中的一个已知问题,但除了没有提出的解决方案有效之外,我注意到该问题中提到的解决方案已经在Devise中实现了.
这是我到目前为止所做的:
my_engine/my_engine.gemspec
s.add_dependency 'omniauth'
s.add_dependency 'devise'
s.add_dependency 'omniauth-google-oauth2'
Run Code Online (Sandbox Code Playgroud)
my_engine/LIB/my_engine.rb
require 'omniauth-google-oauth2'
require 'devise'
Run Code Online (Sandbox Code Playgroud)
my_engine /配置/初始化/ devise.rb
config.omniauth :google_oauth2, ENV['GOOGLE_OAUTH2_API_KEY'], ENV['GOOGLE_OAUTH2_SECRET'], scope: 'email, profile'
Run Code Online (Sandbox Code Playgroud)
my_engine /配置/ routes.rb中
devise_for :users, controllers: {omniauth_callbacks: 'my_engine/omniauth_callbacks'}, class_name: "MyEngine::User", module: :devise
Run Code Online (Sandbox Code Playgroud)
my_engine /应用/控制器/ my_engine/omniauth_callbacks_controller.rb
class MyEngine::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
auth = request.env["omniauth.auth"]
user = MyEngine::User.from_omniauth(request.env["omniauth.auth"])
user.update_attributes(name: auth.info.name)
if user.persisted?
flash.notice = 'Signed in!'
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
alias_method :google_oauth2, :all
end
Run Code Online (Sandbox Code Playgroud)
当我点击"使用Google OAuth2登录"时,它会MyEngine::OmniauthCallbacksController#passthru直接转到"未找到.身份验证passthru".我一直在挖掘源代码,但我无法弄清楚Devise是如何让它通过提供者方法而不是#passthru.我在这里错过了什么吗?我正在运行rails 4.1,Ruby 2.0,设计3.2.4和omniauth-oauth2 1.2.0.
我也有这个问题。我工作的版本与您链接到的 github 问题页面上讨论的解决方案非常相似,但并不完全相同(我在下面的代码中稍微调整了我所做的事情,因为我还在:path我的 中指定了自定义devise_for)。另外,我目前使用的是 devise 3.1.2,但希望它与以下内容足够相似,可以为您工作。
在my_engine/config/routes.rb中:
devise_for :users, :class_name => 'MyEngine::User', :module => :devise, :controllers => { :omniauth_callbacks => "my_engine/omniauth_callbacks" }
devise_scope :user do
# Had to add routes for callbacks here because otherwise the routes get
# messed up -- prepending an extra "/my_engine" in one case.
providers = Regexp.union(Devise.omniauth_providers.map(&:to_s))
path_prefix = '/users/auth'
match "#{path_prefix}/:provider",
:constraints => { :provider => providers },
:to => "omniauth_callbacks#passthru",
:as => :user_omniauth_authorize,
:via => [:get, :post]
match "#{path_prefix}/:action/callback",
:constraints => { :action => providers },
:to => 'omniauth_callbacks',
:as => :user_omniauth_callback,
:via => [:get, :post]
end
Run Code Online (Sandbox Code Playgroud)
在my_engine/config/initializers/devise.rb中:
config.router_name = :my_engine
config.omniauth_path_prefix = "/my_engine/users/auth"
Run Code Online (Sandbox Code Playgroud)
其中一些可能是不必要的,但在花了一些时间使用调试器单步执行流程后,我让它像这样工作,并且从那以后没有时间回去并尝试使其更优雅/精简或其他任何东西。
| 归档时间: |
|
| 查看次数: |
2904 次 |
| 最近记录: |