Sha256: 955d95410ae658bf8d17bf11667c02d71a65b547800cb4b1c2f0e309669e0a44

Contents?: true

Size: 1.71 KB

Versions: 9

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Ibrain::Auth::Mutations
  class SignInMutation < BaseMutation
    field :user, Types::Objects::UserType, null: true
    field :token, String, null: true
    field :result, Boolean, null: true

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

    def resolve(_args)
      raise ActionController::InvalidAuthenticityToken, I18n.t('ibrain.errors.account.incorrect') if auth_resource.blank?

      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 params[:device_token].present?
        device_token = current_user.device_tokens.find_by(token: params[:device_token])
        current_user.device_tokens.create!({ token: params[: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

9 entries across 9 versions & 1 rubygems

Version Path
ibrain-auth-0.3.3 app/graphql/ibrain/auth/mutations/sign_in_mutation.rb
ibrain-auth-0.3.2 app/graphql/ibrain/auth/mutations/sign_in_mutation.rb
ibrain-auth-0.3.1 app/graphql/ibrain/auth/mutations/sign_in_mutation.rb
ibrain-auth-0.2.9 app/graphql/ibrain/auth/mutations/sign_in_mutation.rb
ibrain-auth-0.2.8 app/graphql/ibrain/auth/mutations/sign_in_mutation.rb
ibrain-auth-0.2.7 app/graphql/ibrain/auth/mutations/sign_in_mutation.rb
ibrain-auth-0.2.6 app/graphql/ibrain/auth/mutations/sign_in_mutation.rb
ibrain-auth-0.2.5 app/graphql/ibrain/auth/mutations/sign_in_mutation.rb
ibrain-auth-0.2.4 app/graphql/ibrain/auth/mutations/sign_in_mutation.rb