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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
# rvm config files
.ruby-version
.ruby-gemset

.env
8 changes: 1 addition & 7 deletions app/controllers/graphql_devise/graphql_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

module GraphqlDevise
class GraphqlController < ApplicationController
before_action :set_user_by_token

def auth
result = if params[:_json]
GraphqlDevise::Schema.multiplex(
Expand All @@ -26,11 +24,7 @@ def execute_params(item)
{
operation_name: item[:operationName],
variables: ensure_hash(item[:variables]),
context: {
current_resource: @resource,
controller: self,
resource_class: resource_class
}
context: { controller: self }
}
end

Expand Down
6 changes: 0 additions & 6 deletions app/graphql/graphql_devise/resolvers/confirm_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ def resolve(confirmation_token:, redirect_url:)
raise_user_error(I18n.t('graphql_devise.confirmations.invalid_token'))
end
end

private

def resource_name
resource_class.to_s.underscore.tr('/', '_')
end
end
end
end
8 changes: 6 additions & 2 deletions lib/graphql_devise/concerns/controller_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ def controller
context[:controller]
end

def resource_name
self.class.instance_variable_get(:@resource_name)
end

def resource_class
context[:resource_class]
controller.send(:resource_class, resource_name)
end

def recoverable_enabled?
Expand All @@ -44,7 +48,7 @@ def confirmable_enabled?
end

def current_resource
context[:current_resource]
@current_resource ||= controller.send(:set_user_by_token, resource_name)
end

def client
Expand Down
12 changes: 7 additions & 5 deletions lib/graphql_devise/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class Mapper
def mount_graphql_devise_for(resource, opts = {})
custom_operations = opts[:operations] || {}

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

devise_for(
resource.pluralize.underscore.tr('/', '_').to_sym,
Expand Down Expand Up @@ -34,6 +34,7 @@ def mount_graphql_devise_for(resource, opts = {})

new_mutation
end
used_mutation.instance_variable_set(:@resource_name, mapping_name)

GraphqlDevise::Types::MutationType.field("#{mapping_name}_#{action}", mutation: used_mutation)
end
Expand All @@ -53,15 +54,16 @@ def mount_graphql_devise_for(resource, opts = {})

new_query
end
used_query.instance_variable_set(:@resource_name, mapping_name)

GraphqlDevise::Types::QueryType.field("#{mapping_name}_#{action}", resolver: used_query)
end

Devise.mailer.helper(GraphqlDevise::MailerHelper)

devise_scope mapping_name.to_sym do
post "#{path}/graphql_auth", to: 'graphql_devise/graphql#auth'
get "#{path}/graphql_auth", to: 'graphql_devise/graphql#auth'
devise_scope mapping_name do
post path, to: 'graphql_devise/graphql#auth'
get path, to: 'graphql_devise/graphql#auth'
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Rails.application.routes.draw do
mount_graphql_devise_for 'User', at: 'api/v1', operations: {
mount_graphql_devise_for 'User', at: '/api/v1/graphql_auth', operations: {
login: Mutations::Login,
sign_up: Mutations::SignUp
}

mount_graphql_devise_for(
'Admin',
authenticable_type: Types::CustomAdminType,
at: 'api/v1/admin'
at: '/api/v1/admin/graphql_auth'
)

post '/api/v1/graphql', to: 'api/v1/graphql#graphql'
Expand Down
23 changes: 23 additions & 0 deletions spec/requests/mutations/logout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,27 @@
)
end
end

context 'when using the admin model' do
let(:query) do
<<-GRAPHQL
mutation {
adminLogout {
authenticable { email }
}
}
GRAPHQL
end
let(:admin) { create(:admin, :confirmed) }
let(:headers) { admin.create_new_auth_token }

it 'logs out the admin' do
expect(response).not_to include_auth_headers
expect(admin.reload.tokens.keys).to be_empty
expect(json_response[:data][:adminLogout]).to match(
authenticable: { email: admin.email }
)
expect(json_response[:errors]).to be_nil
end
end
end