You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -43,47 +57,85 @@ class User < ActiveRecord::Base
43
57
end
44
58
```
45
59
60
+
Method `acts_as_jwt_authenticatable` extends Model with several methods: `:jwt_token`, `:generate_authentication_token!`
61
+
and some others. Obviously, `jwt_token` returns token for current record and `:generate_authentication_token!` updates record with new authentication_token.
62
+
46
63
If the model or models you chose have no `:authentication_token` attribute, add them one (with an index):
47
64
48
65
```bash
49
66
rails g jwt_authentication MODEL
50
67
```
51
68
This will add 'acts_as_jwt_authenticatable' to specified MODEL. Also, this will generate migration for adding 'authentication_token' to MODEL.
52
-
To skip generating migration, add '-m' parameter: rails g jwt_authentication User -m
69
+
To skip generating migration, add '-m' parameter: rails g jwt_authentication User -m.
70
+
Migration looks like:
71
+
```ruby
72
+
defchange
73
+
add_column :users, :authentication_token, :string
74
+
add_index :users, :authentication_token
75
+
end
76
+
```
53
77
54
78
55
79
### Allow controllers to handle jwt authentication
56
80
57
-
Finally define which controllers will handle jwt authentication (typ. `ApplicationController`) for which _jwt authenticatable_ models:
81
+
Define controllers, which will handle jwt authentication (typ. `HomeController`) for which _jwt authenticatable_ models:
58
82
59
83
```ruby
60
-
# app/controllers/application_controller.rb
84
+
# app/controllers/home_controller.rb
61
85
62
-
classApplicationController < ActionController::Base# or ActionController::API
86
+
classHomeController < ActionController::Base# or ActionController::API
63
87
# ...
64
88
65
89
acts_as_jwt_authentication_handler
66
90
# Note: you can specify several parameters for handling authentication for this controller:
67
-
# :model (which "acts as jwt authenticatable") for authenticating
68
-
#
69
-
# :key_field. Name of the field in _payload_ of decoded jwt. Entity will be searched in database by this field.
70
-
#
71
-
# :before_filter. Should the before_filter (with selected authenticate method) be injected in controller.
72
-
#
73
-
# :fallback. What to do, if jwt_authentication falls.
74
-
#
75
-
# :sign_in. How to authenticate entity in controller.
91
+
# :models (which "acts as jwt authenticatable") for authenticating, hash, that specifies models
92
+
# and those authentication parameters :header_name, :param_name, :sign_in
You'll find details for `:fallback` parameters in in [Fallback](#fallback)
153
-
You'll find details for `:sign_in` parameters in in [Sign in](#sign-in)
154
186
155
-
Usage
187
+
Authentication
156
188
-----
157
189
158
-
### Tokens Generation
159
-
160
-
Assuming `user` is an instance of `User`, which is _jwt authenticatable_: each time `user` will be saved, and `user.authentication_token.blank?` it receives a new and unique authentication token (via `Devise.friendly_token`).
161
-
162
-
### Authentication Method 1: Query Params
163
-
164
-
You can authenticate passing the `user_token` params as query params:
165
-
166
-
```
167
-
GET https://secure.example.com?user_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJ...iGhux7wDwM_QFpU
168
-
```
169
-
170
-
The _token authentication handler_ (e.g. `ApplicationController`) will perform the user sign in if both are correct.
171
-
172
-
### Authentication Method 2: Request Headers
173
-
174
-
You can also use request headers (which may be simpler when authenticating against an API):
*`:devise`_(default)_ standard devise _sign_in_ call with _entity_m, that was authenticated
211
-
*`:devise_with_session` the same as _:devise_, but with saving devise session
212
-
*`:simplified` just creates `@user` (or other specified @entity) controller instance variable
219
+
# config/routes.rb
220
+
...
221
+
devise_for :users, module: :jwt_authentication
222
+
...
213
223
214
-
### Fallback
215
-
There are 4 variants of fallback - `:none`, `:devise`, `:response`, `:error`
216
-
*`:none`_(default)_ nothing happens if entity could not be authenticated
217
-
*`:devise` control is given to devise strategies
218
-
*`:response` process will be interrupted and 'not authenticated' error is returned in json
219
-
*`:error` process will be interrupted with NotAuthenticated error throwing
220
-
221
-
Devise controllers
222
-
-----
223
-
224
-
You may override Devise controllers for working via JSON.
225
-
For doing this, uncomment `override_devise_controllers` method in _jwt_authentication.rb_ initializer and specify controllers to be overridden.
226
-
`override_devise_controllers` will create alias method chains for needed actions: create -> create_with_token, create_without_token, etc.
227
-
Dependently on accept headers in request, actions will be called. IF _json_ was requested, create_with_token will be called, create_without_token otherwise.
0 commit comments