我需要为Rails中的应用实施自定义策略, 软件版本: Rails 5.0.7.2 红宝石2.3.2p217(2016-11-15修订版56796)[x86_64-linux] Bundler版本1.16.6
这些是我添加的文件:
# config/initializers/omniauth.rb
module OmniAuth
module Strategies
# tell OmniAuth to load our strategy
autoload :Platform, 'lib/strategies/platform.rb'
end
end
Rails.application.config.middleware.use OmniAuth::Builder do
# pass the 2 parameters to the constructor
provider :platform, "Client Id", "Client Secret",
client_options: {
site: "https://my.auth.provider",
user_info_url: "/connect/userinfo"
}
end
现在,在我的lib / strategies /目录中创建了我的策略文件:
# lib/strategies/platform.rb
require 'omniauth-oauth2'
module OmniAuth
module Strategies
class Platform < OmniAuth::Strategies::OAuth2
option :name, :platform
option :client_options, {
:site => "https://my.auth.provider",
:authorize_url => "/connect/authorize",
:user_info_url => "/connect/userinfo"
}
uid { raw_info["id"] }
info do
{
:email => raw_info["email"]
# and anything else you want to return to your API consumers
}
end
def raw_info
@raw_info ||= access_token.get('/connect/userinfo/me.json').parsed
end
def callback_url
full_host + script_name + callback_path
end
end
end
end
然后我在config / routes.rb中注册了回调URL:
#OAuth2 Generic
get "/auth/:provider/callback" => "sessions#create"
现在,我正在尝试配置的该应用程序已经为Google,Twitter和Facebook设计并启用了omniauth,但是由于某些原因,我的URL从未得到注册。我想念什么?我该怎么办? 我想拥有
user_platform_omniauth_authorize_path and
user_platform_omniauth_callback_path
答案 0 :(得分:1)
在模型中添加以下内容:
class User < ApplicationRecord
devise :omniauthable, omniauth_providers: %i[platform]
end