-
Notifications
You must be signed in to change notification settings - Fork 45
Allow controller level authentication #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
941cf68
82155a8
45bda76
2155f5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,33 @@ module Concerns | |
| SetUserByToken.module_eval do | ||
| attr_accessor :client_id, :token, :resource | ||
|
|
||
| class_methods do | ||
| def set_resource_by_model(models, **kwargs) | ||
| Array(models).each do |model| | ||
| GraphqlDevise.configure_warden_serializer_for_model(model) | ||
| end | ||
|
|
||
| before_action(**kwargs) do | ||
| authenticate_model(models) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def authenticate_model(models) | ||
| Array(models).each do |model| | ||
| set_resource_by_token(model) | ||
| return @resource if @resource.present? | ||
| end | ||
|
|
||
| nil | ||
| end | ||
|
|
||
| def resource_class(resource = nil) | ||
| return resource if resource.respond_to?(:find_by) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a comment here indicating this is meant to assert an AR or MongoId instance? |
||
|
|
||
| super | ||
| end | ||
|
|
||
| def full_url_without_params | ||
| request.base_url + request.path | ||
| end | ||
|
|
@@ -16,10 +43,13 @@ def set_resource_by_token(resource) | |
| end | ||
|
|
||
| def graphql_context(resource_name) | ||
| { | ||
| context = { | ||
| resource_name: resource_name, | ||
| controller: self | ||
| } | ||
| context[:current_resource] = @resource if @resource.present? | ||
|
|
||
| context | ||
| end | ||
|
|
||
| def build_redirect_headers(access_token, client, redirect_header_options = {}) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,22 +20,36 @@ def self.load_schema | |
| @schema_loaded = true | ||
| end | ||
|
|
||
| def self.resource_mounted?(mapping_name) | ||
| @mounted_resources.include?(mapping_name) | ||
| def self.resource_mounted?(model) | ||
| @mounted_resources.include?(model) | ||
| end | ||
|
|
||
| def self.mount_resource(mapping_name) | ||
| @mounted_resources << mapping_name | ||
| def self.mount_resource(model) | ||
| @mounted_resources << model | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check the resource is not already there before adding?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we do on the resource_loader
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would less error prone if the check is closer to the assignation, like with
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With |
||
| end | ||
|
|
||
| def self.add_mapping(mapping_name, resource) | ||
| return if Devise.mappings.key?(mapping_name) | ||
| return if Devise.mappings.key?(mapping_name.to_sym) | ||
|
|
||
| Devise.add_mapping( | ||
| mapping_name.to_s.pluralize.to_sym, | ||
| module: :devise, class_name: resource | ||
| ) | ||
| end | ||
|
|
||
| def self.to_mapping_name(resource) | ||
| resource.to_s.underscore.tr('/', '_') | ||
| end | ||
|
|
||
| def self.configure_warden_serializer_for_model(model) | ||
| Devise.warden_config.serialize_into_session(to_mapping_name(model)) do |record| | ||
| model.serialize_into_session(record) | ||
| end | ||
|
|
||
| Devise.warden_config.serialize_from_session(to_mapping_name(model)) do |args| | ||
| model.serialize_from_session(*args) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| require 'graphql_devise/engine' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,7 +78,9 @@ | |
| let(:query) do | ||
| <<-GRAPHQL | ||
| query { | ||
| user(id: #{user.id}) { | ||
| user( | ||
| id: #{user.id} | ||
| ) { | ||
| id | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.