omniauth 用のカスタム プロバイダーを作成する方法をネット全体で検索しました..そして、部分的に成功しました..
私は宝石を作成しましたが、他のプロバイダーが行うように、収集したデータをセッションコントローラーに返す方法を理解できないという部分を除いて、完全に機能しました..
auth gem のコードは次のとおりです。
require 'multi_json'
require 'digest/md5'
require 'rest-client'
module OmniAuth
module Strategies
class Providername < OmniAuth::Strategies::OAuth
attr_accessor :app_id, :api_key, :auth
def initialize(app, app_id = nil, api_key = nil, options = {})
super(app, :providername)
@app_id = app_id
@api_key = api_key
end
protected
def request_phase
redirect "http://valid_url"
end
def callback_phase
if request.params['code'] && request.params['status'] == 'ok'
response = RestClient.get("http://valid_url2/?code=#{request.params['auth_code']}")
auth = MultiJson.decode(response.to_s)
unless auth['error']
@auth_data = auth
if @auth_data
@return_data = OmniAuth::Utils.deep_merge(super, {
'uid' => @auth_data['uid'],
'nickname' => @auth_data['nick'],
'user_info' => {
'first_name' => @auth_data['name'],
'last_name' => @auth_data['surname'],
'location' => @auth_data['place'],
},
'credentials' => {
'apikey' => @auth_data['apikey']
},
'extra' => {'user_hash' => @auth_data}
})
end
end
else
fail!(:invalid_request)
end
rescue Exception => e
fail!(:invalid_response, e)
end
end
end
end
そしてここで私は私のイニシャライザでそれを呼び出します:
Rails.application.config.middleware.use OmniAuth::Builder do
provider "providername", Settings.providers.providername.app_id, Settings.providers.providername.app_secret
end
このコードでは、これまでのところすべて正常に動作し、プロバイダーが呼び出され、プロバイダーから情報を取得し、情報を含むハッシュ (@auth_data) を作成しましたが、それを返すにはどうすればよいですか