Sha256: d69af76fcb8e67abd67fbb79dddd68b3a979dd28e0bdae9b0ee361997118227e

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Ibrain::Mutations
  class SignUpMutation < AuthMutation
    field :is_verified, Boolean, null: true
    field :result, Boolean, null: true

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

    def resolve(args)
      # TODO: define logic inside repository
      return graphql_returning(false, false) 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 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
    end

    private

    def load_resource
      repo.create
    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
      OpenStruct.new(
        result: current_user.present?,
        is_verified: false
      )
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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