Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ Rails:
Enabled: true

AllCops:
TargetRubyVersion: 2.3
TargetRubyVersion: 2.2
DisplayCopNames: true
Exclude:
- bin/**/*
- db/**/*
- vendor/**/*
- tmp/**/*

Rails/HttpPositionalArguments:
Enabled: false

Rails/HasAndBelongsToMany:
Enabled: false

Expand Down
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
source 'https://rubygems.org'

gemspec

gem 'listen'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed?

2 changes: 2 additions & 0 deletions app/controllers/graphql_devise/graphql_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ def auth
render json: result
end

attr_accessor :client_id, :token, :resource

private

def execute_params(item)
Expand Down
20 changes: 18 additions & 2 deletions app/graphql/graphql_devise/mutations/base.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
require 'devise_token_auth/version'

module GraphqlDevise
module Mutations
class Base < GraphQL::Schema::Mutation
private

def remove_resource
controller.resource = nil
controller.client_id = nil
controller.token = nil
end

def single_error_object(error)
{ success: false, errors: [error] }
end

def request
context[:controller].request
controller.request
end

def response
context[:controller].response
controller.response
end

def controller
Expand All @@ -26,6 +34,14 @@ def resource_class
def current_resource
context[:current_resource]
end

def client
if Gem::Version.new(DeviseTokenAuth::VERSION) <= Gem::Version.new('1.1.0')
controller.client_id
else
controller.token.client if controller.token.present?
end
end
end
end
end
5 changes: 2 additions & 3 deletions app/graphql/graphql_devise/mutations/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ class Login < Base
argument :email, String, required: true
argument :password, String, required: true

field :authenticable, GraphqlDevise::Types::AuthenticableType, null: true
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed?

field :success, Boolean, null: false
field :errors, [String], null: false
field :success, Boolean, null: false
field :errors, [String], null: false

def resolve(email:, password:)
resource = resource_class.find_by(email: email)
Expand Down
23 changes: 23 additions & 0 deletions app/graphql/graphql_devise/mutations/logout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module GraphqlDevise
module Mutations
class Logout < Base
field :success, Boolean, null: false
field :errors, [String], null: false

def resolve
if current_resource && client && current_resource.tokens[client]
current_resource.tokens.delete(client)
current_resource.save!

remove_resource

yield current_resource if block_given?

{ success: true, errors: [], authenticable: current_resource }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should authenticable be among the response fields?

else
{ success: false, errors: [I18n.t('graphql_devise.user_not_found')] }
end
end
end
end
end
1 change: 0 additions & 1 deletion app/graphql/graphql_devise/types/mutation_type.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module GraphqlDevise
module Types
class MutationType < GraphQL::Schema::Object
field :login, mutation: GraphqlDevise::Mutations::Login
end
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
en:
graphql_devise:
user_not_found: "User was not found or was not logged in."
sessions:
bad_credentials: "Invalid login credentials. Please try again."
not_confirmed: "A confirmation email was sent to your account at '%{email}'. You must follow the instructions in the email before your account can be activated"
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails4.2_graphql1.8.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

source "https://rubygems.org"

gem "listen"
gem "bundler", "~> 1.17"
gem "rails", github: "rails/rails", branch: "4-2-stable"
gem "graphql", "~> 1.8.0"
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails5.0_graphql1.8.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

source "https://rubygems.org"

gem "listen"
gem "rails", github: "rails/rails", branch: "5-0-stable"
gem "graphql", "~> 1.8.0"
gem "devise_token_auth", "0.1.37"
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails5.0_graphql1.9.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

source "https://rubygems.org"

gem "listen"
gem "rails", github: "rails/rails", branch: "5-0-stable"
gem "graphql", "~> 1.9.0"

Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails5.1_graphql1.8.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

source "https://rubygems.org"

gem "listen"
gem "rails", github: "rails/rails", branch: "5-1-stable"
gem "graphql", "~> 1.8.0"
gem "devise_token_auth", "0.1.42"
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails5.1_graphql1.9.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

source "https://rubygems.org"

gem "listen"
gem "rails", github: "rails/rails", branch: "5-1-stable"
gem "graphql", "~> 1.9.0"

Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails5.2_graphql1.8.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

source "https://rubygems.org"

gem "listen"
gem "rails", github: "rails/rails", branch: "5-2-stable"
gem "graphql", "~> 1.8.0"
gem "devise_token_auth", "0.1.43"
Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails5.2_graphql1.9.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

source "https://rubygems.org"

gem "listen"
gem "rails", github: "rails/rails", branch: "5-2-stable"
gem "graphql", "~> 1.9.0"

Expand Down
1 change: 0 additions & 1 deletion gemfiles/rails_edge_graphql_edge.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

source "https://rubygems.org"

gem "listen"
gem "sqlite3", "~> 1.4"
gem "devise_token_auth", github: "lynndylanhurley/devise_token_auth", branch: "master"
gem "devise", ">= 4.7"
Expand Down
2 changes: 1 addition & 1 deletion graphql_devise.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'pry'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec-rails'
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'rubocop', '0.68.1'
spec.add_development_dependency 'rubocop-performance'
spec.add_development_dependency 'rubocop-rspec'
spec.add_development_dependency 'sqlite3', '~> 1.3.6'
Expand Down
41 changes: 26 additions & 15 deletions lib/graphql_devise/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,40 @@
module ActionDispatch::Routing
class Mapper
def mount_graphql_devise_for(resource, opts = {})
options = {
skip: [
:sessions,
:registrations,
:passwords,
:confirmations,
:token_validations,
:omniauth_callbacks,
:unlocks
]
}.merge(opts)
mutation_options = opts[:mutations] || {}

path = opts.fetch(:at, '/')
mapping_name = resource.underscore.tr('/', '_')

devise_for(
resource.pluralize.underscore.tr('/', '_').to_sym,
class_name: resource,
module: :devise,
path: path,
skip: options[:skip] + [:omniauth_callbacks]
module: :devise,
skip: [:sessions, :registrations, :passwords, :confirmations, :omniauth_callbacks, :unlocks]
)

authenticable_type = opts[:authenticable_type] ||
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this kind indentation, I find it clearer:

authenticable_type = 
  opts[:authenticable_type] || 
  GraphqlDevise::Util::ClassGetter.call("Types::#{resource}Type") ||
  GraphqlDevise::Types::AuthenticableType

"Types::#{resource}Type".safe_constantize ||
GraphqlDevise::Types::AuthenticableType

default_mutations = {
login: GraphqlDevise::Mutations::Login,
logout: GraphqlDevise::Mutations::Logout
}.freeze

default_mutations.each do |action, mutation|
used_mutation = if mutation_options[action].present?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this style for indentation?

used_mutation = 
   if mutation_options[action].present?
     ...
   else
     ...
   end

mutation_options[action]
else
new_mutation = Class.new(mutation)
new_mutation.graphql_name("#{resource}#{action.to_s.titleize}")
new_mutation.field(:authenticable, authenticable_type, null: true)

new_mutation
end

GraphqlDevise::Types::MutationType.field("#{mapping_name}_#{action}", mutation: used_mutation)
end

devise_scope mapping_name.to_sym do
post "#{path}/graphql_auth", to: 'graphql_devise/graphql#auth'
end
Expand Down
14 changes: 14 additions & 0 deletions spec/dummy/app/graphql/mutations/login.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Mutations
class Login < GraphqlDevise::Mutations::Login
field :user, Types::UserType, null: true

def resolve(email:, password:)
original_payload = super do |user|
user.do_something
user.reload
end

original_payload.merge(user: original_payload[:authenticable])
end
end
end
7 changes: 7 additions & 0 deletions spec/dummy/app/graphql/types/user_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Types
class UserType < GraphQL::Schema::Object
field :email, String, null: false
field :name, String, null: false
field :sign_in_count, Int, null: true
end
end
6 changes: 6 additions & 0 deletions spec/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ class User < ApplicationRecord

include DeviseTokenAuth::Concerns::User

validates :name, presence: true

def valid_for_authentication?
auth_available && super
end

def do_something
'Nothing to see here!'
end
end
1 change: 0 additions & 1 deletion spec/dummy/bin/rails
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
1 change: 0 additions & 1 deletion spec/dummy/bin/rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env ruby
require_relative '../config/boot'
require 'rake'
Rake.application.run
4 changes: 0 additions & 4 deletions spec/dummy/config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,4 @@

# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr

# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end
4 changes: 3 additions & 1 deletion spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Rails.application.routes.draw do
mount_graphql_devise_for 'User', at: 'api/v1'
mount_graphql_devise_for 'User', at: 'api/v1', mutations: {
login: Mutations::Login
}
end
1 change: 1 addition & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
FactoryBot.define do
factory :user do
name { Faker::FunnyName.two_word_name }
email { Faker::Internet.unique.email }
password { Faker::Internet.password }

Expand Down
Loading