diff --git a/graphql_devise.gemspec b/graphql_devise.gemspec index 33171287..3a4c937c 100644 --- a/graphql_devise.gemspec +++ b/graphql_devise.gemspec @@ -35,7 +35,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'faker' spec.add_development_dependency 'generator_spec' spec.add_development_dependency 'github_changelog_generator' - spec.add_development_dependency 'pry' + spec.add_development_dependency 'pry', '~> 0.12.2' spec.add_development_dependency 'pry-byebug' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec-rails' diff --git a/lib/graphql_devise/rails/routes.rb b/lib/graphql_devise/rails/routes.rb index a8e61288..277c9369 100644 --- a/lib/graphql_devise/rails/routes.rb +++ b/lib/graphql_devise/rails/routes.rb @@ -37,8 +37,9 @@ def mount_graphql_devise_for(resource, opts = {}) devise_for( resource.pluralize.underscore.tr('/', '_').to_sym, - module: :devise, - skip: [:sessions, :registrations, :passwords, :confirmations, :omniauth_callbacks, :unlocks, :invitations] + module: :devise, + class_name: resource, + skip: [:sessions, :registrations, :passwords, :confirmations, :omniauth_callbacks, :unlocks, :invitations] ) authenticatable_type = opts[:authenticatable_type] || @@ -55,7 +56,7 @@ def mount_graphql_devise_for(resource, opts = {}) custom_operations[action] else new_mutation = Class.new(mutation) - new_mutation.graphql_name("#{resource}#{action.to_s.camelize(:upper)}") + new_mutation.graphql_name("#{resource.gsub('::', '')}#{action.to_s.camelize(:upper)}") new_mutation.field(:authenticatable, authenticatable_type, null: true) new_mutation @@ -83,7 +84,7 @@ def mount_graphql_devise_for(resource, opts = {}) custom_operations[action] else new_query = Class.new(query) - new_query.graphql_name("#{resource}#{action.to_s.camelize(:upper)}") + new_query.graphql_name("#{resource.gsub('::', '')}#{action.to_s.camelize(:upper)}") new_query.type(authenticatable_type, null: true) new_query diff --git a/spec/dummy/app/models/users.rb b/spec/dummy/app/models/users.rb new file mode 100644 index 00000000..c7af3cf2 --- /dev/null +++ b/spec/dummy/app/models/users.rb @@ -0,0 +1,5 @@ +module Users + def self.table_name_prefix + 'users_' + end +end diff --git a/spec/dummy/app/models/users/customer.rb b/spec/dummy/app/models/users/customer.rb new file mode 100644 index 00000000..bdffb1ad --- /dev/null +++ b/spec/dummy/app/models/users/customer.rb @@ -0,0 +1,9 @@ +module Users + class Customer < ApplicationRecord + devise :database_authenticatable, :validatable + + include GraphqlDevise::Concerns::Model + + validates :name, presence: true + end +end diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index 1246bcde..b7cbb357 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -21,5 +21,11 @@ at: '/api/v1/guest/graphql_auth' ) + mount_graphql_devise_for( + 'Users::Customer', + only: [:login], + at: '/api/v1/user_customer/graphql_auth' + ) + post '/api/v1/graphql', to: 'api/v1/graphql#graphql' end diff --git a/spec/dummy/db/migrate/20200321121807_create_users_customers.rb b/spec/dummy/db/migrate/20200321121807_create_users_customers.rb new file mode 100644 index 00000000..bcb72cf1 --- /dev/null +++ b/spec/dummy/db/migrate/20200321121807_create_users_customers.rb @@ -0,0 +1,22 @@ +class CreateUsersCustomers < ActiveRecord::Migration[6.0] + def change + create_table :users_customers do |t| + ## Required + t.string :provider, null: false, default: 'email' + t.string :uid, null: false, default: '' + + ## Database authenticatable + t.string :encrypted_password, null: false, default: '' + + ## User Info + t.string :email + + ## Tokens + t.text :tokens + + t.string :name, null: false + + t.timestamps + end + end +end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index fdc6e356..f11049b8 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_10_13_213045) do +ActiveRecord::Schema.define(version: 2020_03_21_121807) do create_table "admins", force: :cascade do |t| t.string "provider", default: "email", null: false @@ -87,4 +87,15 @@ t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true end + create_table "users_customers", force: :cascade do |t| + t.string "provider", default: "email", null: false + t.string "uid", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "email" + t.text "tokens" + t.string "name", null: false + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + end diff --git a/spec/factories/users_customers.rb b/spec/factories/users_customers.rb new file mode 100644 index 00000000..3f8c9e3f --- /dev/null +++ b/spec/factories/users_customers.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :users_customer, class: 'Users::Customer' do + name { Faker::FunnyName.two_word_name } + email { Faker::Internet.unique.email } + password { Faker::Internet.password } + end +end diff --git a/spec/requests/mutations/login_spec.rb b/spec/requests/mutations/login_spec.rb index f99cde28..1ddcca32 100644 --- a/spec/requests/mutations/login_spec.rb +++ b/spec/requests/mutations/login_spec.rb @@ -146,4 +146,28 @@ ) end end + + context 'when using the Users::Customer model' do + let(:customer) { create(:users_customer, password: password) } + let(:query) do + <<-GRAPHQL + mutation { + usersCustomerLogin( + email: "#{customer.email}", + password: "#{password}" + ) { + authenticatable { email } + } + } + GRAPHQL + end + + before { post_request } + + it 'works alongside the user mount point' do + expect(json_response[:data][:usersCustomerLogin]).to include( + authenticatable: { email: customer.email } + ) + end + end end