Sha256: 57ed9a09c713f19e2bd3569729c57d674be9be60b320c93b5f5d2a73140cc69d

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

require 'omniauth-oauth2'
require 'graphql/client'
require 'graphql/client/http'
require 'intercode_client'

module OmniAuth
  module Strategies
    class Intercode < OmniAuth::Strategies::OAuth2
      CurrentUserQuery = IntercodeClient::Client.parse <<~GRAPHQL
        query {
          currentUser {
            id
            first_name
            last_name
            email
          }
        }
      GRAPHQL

      # Give your strategy a name.
      option :name, 'intercode'

      # This is where you pass the options you would pass when
      # initializing your consumer from the OAuth gem.
      option :client_options, site: IntercodeClient.intercode_url

      uid do
        decoded_jwt['user']['id']
      end

      info do
        {
          first_name: raw_info.data.current_user.first_name,
          last_name: raw_info.data.current_user.last_name,
          email: raw_info.data.current_user.email
        }
      end

      extra do
        {
          'access_token' => access_token.token,
          'raw_info' => raw_info.to_h
        }
      end

      def raw_info
        @raw_info ||= IntercodeClient::Client.query(
          CurrentUserQuery,
          context: { headers: access_token.headers }
        )
      end

      def decoded_jwt
        @decoded_jwt ||= IntercodeClient.decode_jwt(access_token.token)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
intercode_client-0.1.1 lib/omniauth/strategies/intercode.rb