Sha256: 6020cb52e7a8bfeac8dfdcfb0d6cba81bdbf84c7db6c3a7744cfd838e0ecfeba

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Ibrain::Mutations
  class SocialSignInMutation < AuthMutation
    field :user, Types::Objects::UserType, null: true
    field :token, String, null: true
    field :result, Boolean, null: true

    argument :attributes, Ibrain::Auth::Config.social_sign_in_input, required: true
    argument :device_token, String, description: 'Device token for notification', required: false

    def resolve(_args)
      return graphql_returning(false) if auth_resource.blank?

      auth_resource.skip_confirmation! unless auth_resource.try(:confirmed?)
      sign_in(resource_name, auth_resource)
      @current_user = warden.authenticate!(auth_options)

      warden.set_user(current_user)
      current_user.jwt_token, jti = auth_headers(request, auth_resource)
      current_user.jti = jti
      current_user.save!

      if args[:device_token].present?
        device_token = current_user.device_tokens.find_by(token: args[:device_token])

        current_user.device_tokens.create!({ token: args[:device_token] }) if device_token.blank?
      end

      context[:current_user] = current_user

      graphql_returning(
        user_signed_in?,
        user_signed_in? ? current_user : nil,
        current_user.try(:jwt_token)
      )
    end

    private

    def load_resource
      repo.sign_in
    end

    def repo
      ::AuthRepository.new(nil, normalize_parameters)
    end

    def normalize_parameters
      attribute_params
    rescue StandardError
      ActionController::Parameters.new({})
    end

    def auth_options
      { scope: resource_name }
    end

    def graphql_returning(result, user = nil, token = nil)
      OpenStruct.new(
        user: user,
        token: token,
        result: result
      )
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ibrain-auth-0.3.9 app/graphql/ibrain/mutations/social_sign_in_mutation.rb
ibrain-auth-0.3.8 app/graphql/ibrain/mutations/social_sign_in_mutation.rb