graphql_devise 0.11.2 → 0.12.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.rspec +1 -0
- data/.travis.yml +9 -3
- data/CHANGELOG.md +50 -1
- data/README.md +185 -32
- data/app/controllers/graphql_devise/application_controller.rb +4 -1
- data/app/controllers/graphql_devise/concerns/set_user_by_token.rb +25 -0
- data/app/controllers/graphql_devise/graphql_controller.rb +2 -0
- data/app/helpers/graphql_devise/mailer_helper.rb +2 -2
- data/app/views/graphql_devise/mailer/confirmation_instructions.html.erb +1 -1
- data/app/views/graphql_devise/mailer/reset_password_instructions.html.erb +1 -1
- data/config/locales/en.yml +1 -0
- data/config/routes.rb +2 -0
- data/graphql_devise.gemspec +6 -4
- data/lib/generators/graphql_devise/install_generator.rb +63 -30
- data/lib/graphql_devise.rb +24 -10
- data/lib/graphql_devise/default_operations/mutations.rb +6 -6
- data/lib/graphql_devise/default_operations/resolvers.rb +2 -2
- data/lib/graphql_devise/errors/authentication_error.rb +7 -0
- data/lib/graphql_devise/{detailed_user_error.rb → errors/detailed_user_error.rb} +1 -1
- data/lib/graphql_devise/errors/error_codes.rb +6 -0
- data/lib/graphql_devise/errors/execution_error.rb +4 -0
- data/lib/graphql_devise/{user_error.rb → errors/user_error.rb} +1 -1
- data/lib/graphql_devise/mount_method/operation_preparer.rb +2 -2
- data/lib/graphql_devise/mount_method/operation_preparers/default_operation_preparer.rb +6 -2
- data/lib/graphql_devise/mount_method/operation_preparers/gql_name_setter.rb +1 -1
- data/lib/graphql_devise/mount_method/operation_preparers/mutation_field_setter.rb +3 -2
- data/lib/graphql_devise/mount_method/operation_preparers/resolver_type_setter.rb +1 -1
- data/lib/graphql_devise/mount_method/operation_preparers/resource_name_setter.rb +2 -2
- data/lib/graphql_devise/mutations/resend_confirmation.rb +3 -5
- data/lib/graphql_devise/mutations/send_password_reset.rb +5 -2
- data/lib/graphql_devise/mutations/sign_up.rb +3 -6
- data/lib/graphql_devise/rails/routes.rb +5 -72
- data/lib/graphql_devise/resource_loader.rb +87 -0
- data/lib/graphql_devise/schema_plugin.rb +106 -0
- data/lib/graphql_devise/version.rb +1 -1
- data/spec/dummy/app/controllers/api/v1/graphql_controller.rb +41 -3
- data/spec/dummy/app/controllers/application_controller.rb +1 -0
- data/spec/dummy/app/graphql/dummy_schema.rb +18 -0
- data/spec/dummy/app/graphql/interpreter_schema.rb +9 -0
- data/spec/dummy/app/graphql/types/mutation_type.rb +1 -1
- data/spec/dummy/app/graphql/types/query_type.rb +10 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/generators/graphql_devise/install_generator_spec.rb +62 -30
- data/spec/rails_helper.rb +4 -1
- data/spec/requests/graphql_controller_spec.rb +80 -0
- data/spec/requests/mutations/resend_confirmation_spec.rb +44 -29
- data/spec/requests/mutations/send_password_reset_spec.rb +40 -12
- data/spec/requests/queries/confirm_account_spec.rb +7 -1
- data/spec/requests/user_controller_spec.rb +189 -24
- data/spec/services/mount_method/operation_preparer_spec.rb +8 -3
- data/spec/services/mount_method/operation_preparers/custom_operation_preparer_spec.rb +1 -1
- data/spec/services/mount_method/operation_preparers/default_operation_preparer_spec.rb +15 -8
- data/spec/services/mount_method/operation_preparers/mutation_field_setter_spec.rb +18 -4
- data/spec/services/mount_method/operation_preparers/resource_name_setter_spec.rb +1 -1
- data/spec/services/resource_loader_spec.rb +82 -0
- data/spec/services/schema_plugin_spec.rb +26 -0
- data/spec/spec_helper.rb +1 -1
- metadata +107 -89
- data/lib/graphql_devise/error_codes.rb +0 -5
- data/spec/support/generators/file_helpers.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce5d1b0155098a6df8da7563448cb688b00cac2bdff8b039ed201b3cfae6f189
|
4
|
+
data.tar.gz: d9c09f2ba1418aa4b1e75f744ae29bc9d8bc767544c8d6f2e705a59f2e444bfd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53b3ca8e55ddb9264fe1091e4767ee955f4c5c20664cbacdad850fc460582f5ed3e5633eedcaa7e1b7c7bfcfaf70eee792fd133ba2390c784867521bc9922acd
|
7
|
+
data.tar.gz: 6320f6ec2568659db16b80d03c8dce7275237f36865078fb724578b73e5f5879f90a221b87d7dd062e798016f329264b2ba48d23db67506b05e3746f1b0bc474
|
data/.gitignore
CHANGED
data/.rspec
CHANGED
data/.travis.yml
CHANGED
@@ -47,13 +47,19 @@ jobs:
|
|
47
47
|
rvm: 2.7.1
|
48
48
|
- gemfile: gemfiles/rails6.0_graphql_edge.gemfile
|
49
49
|
rvm: 2.6.6
|
50
|
-
env:
|
50
|
+
env:
|
51
|
+
- SKIP_COVERALLS=true
|
52
|
+
- EAGER_LOAD=true
|
51
53
|
- gemfile: gemfiles/rails6.0_graphql_edge.gemfile
|
52
54
|
rvm: 2.7.1
|
53
|
-
env:
|
55
|
+
env:
|
56
|
+
- SKIP_COVERALLS=true
|
57
|
+
- EAGER_LOAD=true
|
54
58
|
- gemfile: gemfiles/rails_edge_graphql_edge.gemfile
|
55
59
|
rvm: 2.7.1
|
56
|
-
env:
|
60
|
+
env:
|
61
|
+
- SKIP_COVERALLS=true
|
62
|
+
- EAGER_LOAD=true
|
57
63
|
exclude:
|
58
64
|
- gemfile: gemfiles/rails4.2_graphql1.8.gemfile
|
59
65
|
rvm: 2.7.1
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,55 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
## [v0.
|
3
|
+
## [v0.12.2](https://github.com/graphql-devise/graphql_devise/tree/v0.12.2) (2020-06-17)
|
4
|
+
|
5
|
+
[Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.12.1...v0.12.2)
|
6
|
+
|
7
|
+
**Breaking changes:**
|
8
|
+
|
9
|
+
- Fix set\_resource\_by\_token no mapping error in no eager load envs [\#107](https://github.com/graphql-devise/graphql_devise/pull/107) ([mcelicalderon](https://github.com/mcelicalderon))
|
10
|
+
|
11
|
+
**Fixed bugs:**
|
12
|
+
|
13
|
+
- Separate endpoint url for mailers even if mounting the gem in your own schema [\#105](https://github.com/graphql-devise/graphql_devise/issues/105)
|
14
|
+
- Devise mapping error [\#103](https://github.com/graphql-devise/graphql_devise/issues/103)
|
15
|
+
- Use the url where the schema is mounted in emails links [\#106](https://github.com/graphql-devise/graphql_devise/pull/106) ([00dav00](https://github.com/00dav00))
|
16
|
+
|
17
|
+
## [v0.12.1](https://github.com/graphql-devise/graphql_devise/tree/v0.12.1) (2020-06-12)
|
18
|
+
|
19
|
+
[Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.12.0...v0.12.1)
|
20
|
+
|
21
|
+
**Security fixes:**
|
22
|
+
|
23
|
+
- Insecure send password reset mutation? [\#98](https://github.com/graphql-devise/graphql_devise/issues/98)
|
24
|
+
- Avoid returning user information on password reset mutation [\#100](https://github.com/graphql-devise/graphql_devise/pull/100) ([00dav00](https://github.com/00dav00))
|
25
|
+
|
26
|
+
## [v0.12.0](https://github.com/graphql-devise/graphql_devise/tree/v0.12.0) (2020-06-12)
|
27
|
+
|
28
|
+
[Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.11.4...v0.12.0)
|
29
|
+
|
30
|
+
**Implemented enhancements:**
|
31
|
+
|
32
|
+
- Mount auth operations in main GQL schema [\#96](https://github.com/graphql-devise/graphql_devise/pull/96) ([mcelicalderon](https://github.com/mcelicalderon))
|
33
|
+
|
34
|
+
## [v0.11.4](https://github.com/graphql-devise/graphql_devise/tree/v0.11.4) (2020-05-23)
|
35
|
+
|
36
|
+
[Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.11.3...v0.11.4)
|
37
|
+
|
38
|
+
**Implemented enhancements:**
|
39
|
+
|
40
|
+
- Do nothing if forgery protection enabled in ApplicationController [\#93](https://github.com/graphql-devise/graphql_devise/pull/93) ([mcelicalderon](https://github.com/mcelicalderon))
|
41
|
+
|
42
|
+
## [v0.11.3](https://github.com/graphql-devise/graphql_devise/tree/v0.11.3) (2020-05-23)
|
43
|
+
|
44
|
+
[Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.11.2...v0.11.3)
|
45
|
+
|
46
|
+
**Implemented enhancements:**
|
47
|
+
|
48
|
+
- Default `change\_headers\_on\_each\_request` to false [\#76](https://github.com/graphql-devise/graphql_devise/issues/76)
|
49
|
+
- Replace the auth model concern on generator execution [\#53](https://github.com/graphql-devise/graphql_devise/issues/53)
|
50
|
+
- Generator. Use our modules, change defaults [\#91](https://github.com/graphql-devise/graphql_devise/pull/91) ([mcelicalderon](https://github.com/mcelicalderon))
|
51
|
+
|
52
|
+
## [v0.11.2](https://github.com/graphql-devise/graphql_devise/tree/v0.11.2) (2020-05-07)
|
4
53
|
|
5
54
|
[Full Changelog](https://github.com/graphql-devise/graphql_devise/compare/v0.11.1...v0.11.2)
|
6
55
|
|
data/README.md
CHANGED
@@ -12,25 +12,34 @@ GraphQL interface on top of the [Devise Token Auth](https://github.com/lynndylan
|
|
12
12
|
* [Table of Contents](#table-of-contents)
|
13
13
|
* [Introduction](#introduction)
|
14
14
|
* [Installation](#installation)
|
15
|
+
* [Running the Generator](#running-the-generator)
|
16
|
+
* [Mounting the Schema in a Separate Route](#mounting-the-schema-in-a-separate-route)
|
17
|
+
* [Mounting Operations in Your Own Schema](#mounting-operations-in-your-own-schema)
|
18
|
+
* [Important](#important)
|
15
19
|
* [Usage](#usage)
|
16
|
-
* [Mounting
|
17
|
-
|
20
|
+
* [Mounting Auth Schema on a Separate Route](#mounting-auth-schema-on-a-separate-route)
|
21
|
+
* [Mounting Operations Into Your Own Schema](#mounting-operations-into-your-own-schema)
|
22
|
+
* [Available Mount Options](#available-mount-options)
|
23
|
+
* [Available Operations](#available-operations)
|
18
24
|
* [Configuring Model](#configuring-model)
|
19
25
|
* [Customizing Email Templates](#customizing-email-templates)
|
20
26
|
* [I18n](#i18n)
|
21
27
|
* [Authenticating Controller Actions](#authenticating-controller-actions)
|
28
|
+
* [Authenticate Before Reaching Your GQL Schema](#authenticate-before-reaching-your-gql-schema)
|
29
|
+
* [Authenticate in Your GQL Schema](#authenticate-in-your-gql-schema)
|
22
30
|
* [Making Requests](#making-requests)
|
23
31
|
* [Mutations](#mutations)
|
24
32
|
* [Queries](#queries)
|
25
33
|
* [More Configuration Options](#more-configuration-options)
|
26
34
|
* [Devise Token Auth Initializer](#devise-token-auth-initializer)
|
27
35
|
* [Devise Initializer](#devise-initializer)
|
36
|
+
* [GraphQL Interpreter](#graphql-interpreter)
|
28
37
|
* [Using Alongside Standard Devise](#using-alongside-standard-devise)
|
29
38
|
* [Future Work](#future-work)
|
30
39
|
* [Contributing](#contributing)
|
31
40
|
* [License](#license)
|
32
41
|
|
33
|
-
<!-- Added by: mcelicalderon, at:
|
42
|
+
<!-- Added by: mcelicalderon, at: Wed Jun 10 22:10:26 -05 2020 -->
|
34
43
|
|
35
44
|
<!--te-->
|
36
45
|
|
@@ -54,13 +63,11 @@ gem 'graphql_devise'
|
|
54
63
|
```
|
55
64
|
|
56
65
|
And then execute:
|
66
|
+
```bash
|
67
|
+
$ bundle
|
68
|
+
```
|
57
69
|
|
58
|
-
|
59
|
-
|
60
|
-
Next, you need to run the generator:
|
61
|
-
|
62
|
-
$ bundle exec rails generate graphql_devise:install
|
63
|
-
|
70
|
+
### Running the Generator
|
64
71
|
Graphql Devise generator will execute `Devise` and `Devise Token Auth`
|
65
72
|
generators for you. These will make the required changes for the gems to
|
66
73
|
work correctly. All configurations for [Devise](https://github.com/plataformatec/devise) and
|
@@ -68,6 +75,11 @@ work correctly. All configurations for [Devise](https://github.com/plataformatec
|
|
68
75
|
so you can read the docs there to customize your options.
|
69
76
|
Configurations are done via initializer files as usual, one per gem.
|
70
77
|
|
78
|
+
#### Mounting the Schema in a Separate Route
|
79
|
+
```bash
|
80
|
+
$ bundle exec rails generate graphql_devise:install
|
81
|
+
```
|
82
|
+
|
71
83
|
The generator accepts 2 params: `user_class` and `mount_path`. The params
|
72
84
|
will be used to mount the route in `config/routes.rb`. For instance the executing:
|
73
85
|
|
@@ -87,14 +99,32 @@ Will do the following:
|
|
87
99
|
`Admin` could be any model name you are going to be using for authentication,
|
88
100
|
and `api/auth` could be any mount path you would like to use for auth.
|
89
101
|
|
90
|
-
|
102
|
+
#### Mounting Operations in Your Own Schema
|
103
|
+
Now you can provide to the generator an option specifying
|
104
|
+
the name of your GQL schema. Doing this will skip the insertion of the mount method in the
|
105
|
+
routes file and will also add our `SchemaPlugin` to the specified schema. `user_class` param is still optional (`Admin`) in the following example.
|
106
|
+
|
107
|
+
```bash
|
108
|
+
$ bundle exec rails g graphql_devise:install Admin --mount MySchema
|
109
|
+
```
|
110
|
+
|
111
|
+
### Important
|
112
|
+
Remember that by default this gem mounts a completely separate GraphQL schema on a separate controller in the route
|
91
113
|
provided by the `at` option in the `mount_graphql_devise_for` method in the `config/routes.rb` file. If no `at`
|
92
|
-
option is provided, the route will be `/graphql_auth`.
|
93
|
-
|
114
|
+
option is provided, the route will be `/graphql_auth`.
|
115
|
+
|
116
|
+
**Starting with `v0.12.0`** you can opt-in to load this gem's queries and mutations into your
|
117
|
+
own application's schema. You can actually mount a resource's auth schema in a separate route
|
118
|
+
and in your app's schema at the same time, but that's probably not a common scenario. More on
|
119
|
+
this in the next section.
|
94
120
|
|
95
121
|
## Usage
|
96
|
-
### Mounting
|
97
|
-
|
122
|
+
### Mounting Auth Schema on a Separate Route
|
123
|
+
The generator can do this step for you by default. Remember now you can mount this gem's
|
124
|
+
auth operations into your own schema as described in [this section](#mounting-operations-into-your-own-schema).
|
125
|
+
|
126
|
+
|
127
|
+
Routes can be added using the generator or manually.
|
98
128
|
You can mount this gem's GraphQL auth schema in your routes file like this:
|
99
129
|
|
100
130
|
```ruby
|
@@ -120,11 +150,83 @@ Rails.application.routes.draw do
|
|
120
150
|
)
|
121
151
|
end
|
122
152
|
```
|
153
|
+
The second argument of the `mount_graphql_devise` method is a hash of options where you can
|
154
|
+
customize how the queries and mutations are mounted into the schema. For a list of available
|
155
|
+
options go [here](#available-mount-options)
|
123
156
|
|
124
|
-
|
157
|
+
### Mounting Operations Into Your Own Schema
|
158
|
+
Starting with `v0.12.0` you can now mount the GQL operations provided by this gem into your
|
159
|
+
app's main schema.
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
# app/graphql/dummy_schema.rb
|
163
|
+
|
164
|
+
class DummySchema < GraphQL::Schema
|
165
|
+
# It's important that this line goes before setting the query and mutation type on your
|
166
|
+
# schema in graphql versions < 1.10.0
|
167
|
+
use GraphqlDevise::SchemaPlugin.new(
|
168
|
+
query: Types::QueryType,
|
169
|
+
mutation: Types::MutationType,
|
170
|
+
resource_loaders: [
|
171
|
+
GraphqlDevise::ResourceLoader.new('User', only: [:login, :confirm_account])
|
172
|
+
]
|
173
|
+
)
|
174
|
+
|
175
|
+
mutation(Types::MutationType)
|
176
|
+
query(Types::QueryType)
|
177
|
+
end
|
178
|
+
```
|
179
|
+
The example above describes just one of the possible scenarios you might need.
|
180
|
+
The second argument of the `GraphqlDevise::ResourceLoader` initializer is a hash of
|
181
|
+
options where you can customize how the queries and mutations are mounted into the schema.
|
182
|
+
For a list of available options go [here](#available-mount-options).
|
183
|
+
|
184
|
+
It's important to use the plugin in your schema before assigning the mutation and query type to
|
185
|
+
it in graphql versions `< 1.10.0`. Otherwise the auth operations won't be available.
|
186
|
+
|
187
|
+
You can provide as many resource loaders as you need to the `resource_loaders` option, and each
|
188
|
+
of those will be loaded into your schema. These are the options you can initialize the
|
189
|
+
`SchemaPlugin` with:
|
190
|
+
|
191
|
+
1. `query`: This param is mandatory unless you skip all queries via the resource loader
|
192
|
+
options. This should be the same `QueryType` you provide to the `query` method
|
193
|
+
in your schema.
|
194
|
+
1. `mutation`: This param mandatory unless you skip all mutations via the resource loader
|
195
|
+
options. This should be the same `MutationType` you provide to the `mutation` method
|
196
|
+
in your schema.
|
197
|
+
1. `resource_loaders`: This is an optional array of `GraphqlDevise::ResourceLoader` instances.
|
198
|
+
Here is where you specify the operations that you want to load into your app's schema.
|
199
|
+
If no loader is provided, no operations will be added to your schema, but you will still be
|
200
|
+
able to authenticate queries and mutations selectively. More on this in the controller
|
201
|
+
authentication [section](#authenticating-controller-actions).
|
202
|
+
1. `authenticate_default`: This is a boolean value which is `true` by default. This value
|
203
|
+
defines what is the default behavior for authentication in your schema fields. `true` means
|
204
|
+
every root level field requires authentication unless specified otherwise using the
|
205
|
+
`authenticate: false` option on the field. `false` means your root level fields won't require
|
206
|
+
authentication unless specified otherwise using the `authenticate: true` option on the field.
|
207
|
+
1. `unauthenticated_proc`: This param is optional. Here you can provide a proc that receives
|
208
|
+
one argument (field name) and is called whenever a field that requires authentication
|
209
|
+
is called without an authenticated resource. By default a `GraphQL::ExecutionError` will be
|
210
|
+
raised if authentication fails. This will provide a GQL like error message on the response.
|
211
|
+
|
212
|
+
### Available Mount Options
|
213
|
+
Both the `mount_graphql_devise_for` method and the `GraphqlDevise::ResourceLoader` class
|
214
|
+
take the same options. So, wether you decide to mount this gem in a separate route
|
215
|
+
from your main application's schema or you use our `GraphqlDevise::SchemaPlugin` to load
|
216
|
+
this gem's auth operation into your schema, these are the options you can provide as a hash.
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
# Using the mount method in your config/routes.rb file
|
220
|
+
mount_graphql_devise_for('User', {})
|
125
221
|
|
126
|
-
|
127
|
-
|
222
|
+
# Providing options to a GraphqlDevise::ResourceLoader
|
223
|
+
GraphqlDevise::ResourceLoader.new('User', {})
|
224
|
+
```
|
225
|
+
|
226
|
+
1. `at`: Route where the GraphQL schema will be mounted on the Rails server.
|
227
|
+
In [this example](#mounting-auth-schema-on-a-separate-route) your API will have
|
228
|
+
these two routes: `POST /api/v1/graphql_auth` and `GET /api/v1/graphql_auth`.
|
229
|
+
If this option is not specified, the schema will be mounted at `/graphql_auth`. **This option only works if you are using the mount method.**
|
128
230
|
1. `operations`: Specifying this is optional. Here you can override default
|
129
231
|
behavior by specifying your own mutations and queries for every GraphQL operation.
|
130
232
|
Check available operations in this file [mutations](https://github.com/graphql-devise/graphql_devise/blob/b5985036e01ea064e43e457b4f0c8516f172471c/lib/graphql_devise/rails/routes.rb#L19)
|
@@ -163,7 +265,7 @@ or [base resolver](https://github.com/graphql-devise/graphql_devise/blob/master/
|
|
163
265
|
respectively, to take advantage of some of the methods provided by devise
|
164
266
|
just like with `devise_scope`
|
165
267
|
|
166
|
-
|
268
|
+
### Available Operations
|
167
269
|
The following is a list of the symbols you can provide to the `operations`, `skip` and `only` options of the mount method:
|
168
270
|
```ruby
|
169
271
|
:login
|
@@ -175,7 +277,6 @@ The following is a list of the symbols you can provide to the `operations`, `ski
|
|
175
277
|
:check_password_token
|
176
278
|
```
|
177
279
|
|
178
|
-
|
179
280
|
### Configuring Model
|
180
281
|
Just like with Devise and DTA, you need to include a module in your authenticatable model,
|
181
282
|
so with our example, your user model will have to look like this:
|
@@ -193,16 +294,12 @@ class User < ApplicationRecord
|
|
193
294
|
:confirmable
|
194
295
|
|
195
296
|
# including after calling the `devise` method is important.
|
196
|
-
# include DeviseTokenAuth::Concerns::User # is also valid (generator includes this one).
|
197
297
|
include GraphqlDevise::Concerns::Model
|
198
298
|
end
|
199
299
|
```
|
200
300
|
|
201
301
|
The install generator can do this for you if you specify the `user_class` option.
|
202
302
|
See [Installation](#installation) for details.
|
203
|
-
The generator will include a different module in your model, `DeviseTokenAuth::Concerns::User` which is also correct,
|
204
|
-
we just made an alias on our namespace for consistency and possible extension.
|
205
|
-
Generators have to be updated to generate our module.
|
206
303
|
|
207
304
|
### Customizing Email Templates
|
208
305
|
The approach of this gem is a bit different from DeviseTokenAuth. We have placed our templates in `app/views/graphql_devise/mailer`,
|
@@ -220,13 +317,15 @@ Keep in mind that if your app uses multiple locales, you should set the `I18n.lo
|
|
220
317
|
|
221
318
|
### Authenticating Controller Actions
|
222
319
|
Just like with Devise or DTA, you will need to authenticate users in your controllers.
|
320
|
+
For this you have two alternatives.
|
321
|
+
|
322
|
+
#### Authenticate Before Reaching Your GQL Schema
|
223
323
|
For this you need to call `authenticate_<model>!` in a before_action hook of your controller.
|
224
324
|
In our example our model is `User`, so it would look like this:
|
225
325
|
```ruby
|
226
326
|
# app/controllers/my_controller.rb
|
227
327
|
|
228
328
|
class MyController < ApplicationController
|
229
|
-
# include DeviseTokenAuth::Concerns::SetUserByToken # is also valid (generator includes this one).
|
230
329
|
include GraphqlDevise::Concerns::SetUserByToken
|
231
330
|
|
232
331
|
before_action :authenticate_user!
|
@@ -239,9 +338,56 @@ end
|
|
239
338
|
|
240
339
|
The install generator can do this for you because it executes DTA installer.
|
241
340
|
See [Installation](#Installation) for details.
|
242
|
-
|
243
|
-
|
244
|
-
|
341
|
+
If authentication fails for the request for whatever reason, execution of the request is halted
|
342
|
+
and an error is returned in a REST format as the request never reaches your GQL schema.
|
343
|
+
|
344
|
+
#### Authenticate in Your GQL Schema
|
345
|
+
For this you will need to add the `GraphqlDevise::SchemaPlugin` to your schema as described
|
346
|
+
[here](#mounting-operations-into-your-own-schema).
|
347
|
+
|
348
|
+
```ruby
|
349
|
+
# app/controllers/my_controller.rb
|
350
|
+
|
351
|
+
class MyController < ApplicationController
|
352
|
+
include GraphqlDevise::Concerns::SetUserByToken
|
353
|
+
|
354
|
+
def my_action
|
355
|
+
render json: DummySchema.execute(params[:query], context: graphql_context(:user))
|
356
|
+
end
|
357
|
+
end
|
358
|
+
```
|
359
|
+
The `graphql_context` method receives a symbol identifying the resource you are trying
|
360
|
+
to authenticate. So if you mounted the `'User'` resource, the symbol is `:user`. You can use
|
361
|
+
this snippet to find the symbol for more complex scenarios
|
362
|
+
`resource_klass.to_s.underscore.tr('/', '_').to_sym`. `graphql_context` can also take an
|
363
|
+
array of resources if you mounted more than one into your schema. The gem will try to
|
364
|
+
authenticate a resource for each element on the array until it finds one.
|
365
|
+
|
366
|
+
Internally in your own mutations and queries a key `current_resource` will be available in
|
367
|
+
the context if a resource was successfully authenticated or `nil` otherwise.
|
368
|
+
|
369
|
+
Keep in mind that sending multiple values to the `graphql_context` method means that depending
|
370
|
+
on who makes the request, the context value `current_resource` might contain instances of the
|
371
|
+
different models you might have mounted into the schema.
|
372
|
+
|
373
|
+
Please note that by using this mechanism your GQL schema will be in control of what queries are
|
374
|
+
restricted to authenticated users and you can only do this at the root level fields of your GQL
|
375
|
+
schema. Configure the plugin as explained [here](#mounting-operations-into-your-own-schema)
|
376
|
+
so this can work.
|
377
|
+
|
378
|
+
In you main app's schema this is how you might specify if a field needs to be authenticated or not:
|
379
|
+
```ruby
|
380
|
+
module Types
|
381
|
+
class QueryType < Types::BaseObject
|
382
|
+
# user field used the default set in the Plugin's initializer
|
383
|
+
field :user, resolver: Resolvers::UserShow
|
384
|
+
# this field will never require authentication
|
385
|
+
field :public_field, String, null: false, authenticate: false
|
386
|
+
# this field requires authentication
|
387
|
+
field :private_field, String, null: false, authenticate: true
|
388
|
+
end
|
389
|
+
end
|
390
|
+
```
|
245
391
|
|
246
392
|
### Making Requests
|
247
393
|
Here is a list of the available mutations and queries assuming your mounted model is `User`.
|
@@ -290,10 +436,10 @@ as comments. You can also use
|
|
290
436
|
**[DTA's docs](https://devise-token-auth.gitbook.io/devise-token-auth/config/initialization)** as a reference.
|
291
437
|
In this section the most important configurations will be highlighted.
|
292
438
|
|
293
|
-
- **change_headers_on_each_request:** This configurations defaults to `
|
294
|
-
|
295
|
-
Setting this to `
|
296
|
-
|
439
|
+
- **change_headers_on_each_request:** This configurations defaults to `false`. This will allow you to store the
|
440
|
+
credentials for as long as the token life_span permits. And you can send the same credentials in each request.
|
441
|
+
Setting this to `true` means that tokens will change on each request you make, and the new values will be returned
|
442
|
+
in the headers. So your client needs to handle this.
|
297
443
|
- **batch_request_buffer_throttle:** When change_headers_on_each_request is set to true, you might still want your
|
298
444
|
credentials to be valid more than once as you might send parallel request. The duration you set here will
|
299
445
|
determine how long the same credentials work after the first request is received.
|
@@ -316,6 +462,14 @@ In this section the most important configurations will be highlighted.
|
|
316
462
|
|
317
463
|
**Note:** Remember this gem adds a layer on top of Devise, so some configurations might not apply.
|
318
464
|
|
465
|
+
### GraphQL Interpreter
|
466
|
+
GraphQL-Ruby `>= 1.9.0` includes a new runtime module which you may use for your schema.
|
467
|
+
Eventually, it will become the default. You can read more about it
|
468
|
+
[here](https://graphql-ruby.org/queries/interpreter).
|
469
|
+
|
470
|
+
This gem supports schemas using the interpreter and it is recommended as it introduces several
|
471
|
+
improvements which focus mainly on performance.
|
472
|
+
|
319
473
|
### Using Alongside Standard Devise
|
320
474
|
The DeviseTokenAuth gem allows experimental use of the standard Devise gem to be configured at the same time, for more
|
321
475
|
information you can check [this answer here](https://github.com/lynndylanhurley/devise_token_auth/blob/2a32f18ccce15638a74e72f6cfde5cf15a808d3f/docs/faq.md#can-i-use-this-gem-alongside-standard-devise).
|
@@ -326,7 +480,6 @@ standard Devise templates.
|
|
326
480
|
## Future Work
|
327
481
|
We will continue to improve the gem and add better docs.
|
328
482
|
|
329
|
-
1. Add mount option that will create a separate schema for the mounted resource.
|
330
483
|
1. Make sure this gem can correctly work alongside DTA and the original Devise gem.
|
331
484
|
1. Improve DOCS.
|
332
485
|
1. Add support for unlockable and other Devise modules.
|