Skip to content

Commit 4fc3214

Browse files
Merge pull request #181 from graphql-devise/document-authenticate-with-callable
Document authenticate with callable
2 parents 0343c07 + e1666fd commit 4fc3214

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ GraphQL interface on top of the [Devise Token Auth](https://github.com/lynndylan
2828
* [I18n](#i18n)
2929
* [Authenticating Controller Actions](#authenticating-controller-actions)
3030
* [Authenticate Resource in the Controller (>= v0.15.0)](#authenticate-resource-in-the-controller--v0150)
31+
* [Authentication Options](#authentication-options)
3132
* [Authenticate Before Reaching Your GQL Schema (Deprecated)](#authenticate-before-reaching-your-gql-schema-deprecated)
3233
* [Authenticate in Your GQL Schema (Deprecated)](#authenticate-in-your-gql-schema-deprecated)
34+
* [Authentication Options](#authentication-options-1)
3335
* [Important](#important-2)
3436
* [Making Requests](#making-requests)
3537
* [Introspection query](#introspection-query)
@@ -45,7 +47,7 @@ GraphQL interface on top of the [Devise Token Auth](https://github.com/lynndylan
4547
* [Contributing](#contributing)
4648
* [License](#license)
4749

48-
<!-- Added by: mcelicalderon, at: Sat May 8 12:32:03 -05 2021 -->
50+
<!-- Added by: mcelicalderon, at: Wed May 19 21:25:22 -05 2021 -->
4951

5052
<!--te-->
5153

@@ -432,7 +434,13 @@ restricted to authenticated users and you can only do this at the root level fie
432434
schema. Configure the plugin as explained [here](#mounting-operations-into-your-own-schema)
433435
so this can work.
434436

435-
In you main app's schema this is how you might specify if a field needs to be authenticated or not:
437+
##### Authentication Options
438+
Wether you setup authentications as a default in the plugin, or you do it at the field level,
439+
these are the options you can use:
440+
1. **Any truthy value:** If `current_resource` is not `.present?`, query will return an authentication error.
441+
1. **A callable object:** Provided object will be called with `current_resource` as the only argument if `current_resource` is `.present?`. If return value of the callable object is false, query will return an authentication error.
442+
443+
In your main app's schema this is how you might specify if a field needs to be authenticated or not:
436444
```ruby
437445
module Types
438446
class QueryType < Types::BaseObject
@@ -442,13 +450,11 @@ module Types
442450
field :public_field, String, null: false, authenticate: false
443451
# this field requires authentication
444452
field :private_field, String, null: false, authenticate: true
453+
# this field requires authenticated users to also be admins
454+
field :admin_field, String, null: false, authenticate: ->(user) { user.admin? }
445455
end
446456
end
447457
```
448-
**Important:** Currently, the only check the plugin does to see if the user is authenticated or not when executing
449-
the query, is verifying that `context[:current_resource].present?` in the GraphQL context.
450-
So, be careful not to populate that key of the context with values other than what `gql_devise_context`
451-
returns. The option to do more complex verifications will be added in the future.
452458

453459
#### Authenticate Before Reaching Your GQL Schema (Deprecated)
454460
For this you will need to call `authenticate_<model>!` in a `before_action` controller hook.
@@ -506,7 +512,13 @@ restricted to authenticated users and you can only do this at the root level fie
506512
schema. Configure the plugin as explained [here](#mounting-operations-into-your-own-schema)
507513
so this can work.
508514

509-
In you main app's schema this is how you might specify if a field needs to be authenticated or not:
515+
##### Authentication Options
516+
Wether you setup authentications as a default in the plugin, or you do it at the field level,
517+
these are the options you can use:
518+
1. **Any truthy value:** If `current_resource` is not `.present?`, query will return an authentication error.
519+
1. **A callable object:** Provided object will be called with `current_resource` as the only argument if `current_resource` is `.present?`. If return value of the callable object is false, query will return an authentication error.
520+
521+
In your main app's schema this is how you might specify if a field needs to be authenticated or not:
510522
```ruby
511523
module Types
512524
class QueryType < Types::BaseObject
@@ -516,6 +528,8 @@ module Types
516528
field :public_field, String, null: false, authenticate: false
517529
# this field requires authentication
518530
field :private_field, String, null: false, authenticate: true
531+
# this field requires authenticated users to also be admins
532+
field :admin_field, String, null: false, authenticate: ->(user) { user.admin? }
519533
end
520534
end
521535
```

0 commit comments

Comments
 (0)