Sha256: 659e41a5879c67cc3dece3e50f6de1a4f890484edec9be380fd6f5266fe9a626

Contents?: true

Size: 1.66 KB

Versions: 7

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

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

    argument :id_token, String, description: 'Id Token from firebase', required: true
    argument :device_token, String, description: 'Device token for notificaiton', required: false

    def resolve(args)
      # TODO: define logic inside repository
      repo = ::AuthRepository.new(nil, normalize_params(args))
      user = repo.sign_in

      return OpenStruct.new({ user: nil, token: nil, result: false, is_verified: false }) if user.blank?

      sign_in(resource_name, user)
      @current_user = warden.authenticate!(auth_options)

      warden.set_user(current_user)
      current_user.jwt_token, jti = auth_headers(request, user)

      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

      OpenStruct.new(
        user: user_signed_in? ? current_user : nil,
        token: current_user.try(:jwt_token),
        result: user_signed_in?,
        is_verified: true
      )
    end

    private

    def normalize_params(args)
      ActionController::Parameters.new(args.as_json)
    rescue StandardError
      ActionController::Parameters.new({})
    end

    def auth_options
      { scope: resource_name }
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

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