Sha256: aaa39f6f88be713b7672b0b701e9d17df70f8a8e2b2906371f99919ca6dac65c

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

require 'httparty'

module Capcoauth
  class CallbackController < Capcoauth::ApplicationController
    def show
      # Abort if code not found
      return redirect_to root_url, alert: 'Authorization was canceled' unless params[:code].present?

      response = HTTParty.post('https://capcoauth.capco.com/oauth/token', {
        body: {
          client_id: Capcoauth.configuration.client_id,
          client_secret: Capcoauth.configuration.client_secret,
          code: params[:code],
          grant_type: 'authorization_code',
          redirect_uri: oauth_callback_url
        }
      })

      error_message = 'There was an error logging you in'

      if response.code == 200 and !response.parsed_response['access_token'].blank?
        @access_token = OAuth::AccessToken.new(response.parsed_response['access_token']).verify

        if @access_token
          session[:capcoauth_access_token] = @access_token.token
          session[:capcoauth_user_id] = @access_token.user_id
          redirect_to session[:previous_url].blank? ? root_url : session.delete(:previous_url)
        else
          redirect_to root_url, alert: error_message
        end
      else
        redirect_to root_url, alert: error_message
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
capcoauth-0.1.4 app/controllers/capcoauth/callback_controller.rb
capcoauth-0.1.3 app/controllers/capcoauth/callback_controller.rb
capcoauth-0.1.2 app/controllers/capcoauth/callback_controller.rb
capcoauth-0.1.1 app/controllers/capcoauth/callback_controller.rb