How to create custom omniauth provider (how to return data)

Posted by user2803917 on Stack Overflow See other posts from Stack Overflow or by user2803917
Published on 2013-10-30T09:50:45Z Indexed on 2013/10/30 9:53 UTC
Read the original article Hit count: 281

Filed under:
|
|
|

I searched all around the net, how to create a custom provider for omniauth.. and i succedded partly..

I created a gem, and it worked perfectly, except the part, that i cant understand how to return the gathered data to sessions controller, like other providers do..

here is the code in 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

and here i call it in my initializers:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider "providername", Settings.providers.providername.app_id, Settings.providers.providername.app_secret
end

in this code, everything works fine so far, the provider gets called, i get the info from provider, i create a hash (@auth_data) with info, but how do i return it

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby