rodauth-rails 1.9.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +145 -225
- data/lib/rodauth/rails/version.rb +1 -1
- data/lib/rodauth/rails.rb +10 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adfcbce27e52d0a53b5cd670489635c841dff9e2809c92be059286943894db6c
|
4
|
+
data.tar.gz: e98584823e926c810bc66366496df5fb3c8eed0f38e291b76b9f132480941a9c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 845b037926a9372522da9b3507b5fd2959454bc24198f72a7832d9025ccd27f9bac3790823577e1e68c3a1e1a076472e1629825973c37cc932e2b63299e1408b
|
7
|
+
data.tar.gz: af417d7bbe9732c677ca15c6796817554fe68d5f8f3db8001b56a736db33382f3efd8f86952509d038f45f449f76d3c9ea61f1a7f5a29c1cef5f45e72cf1c9ce
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -24,6 +24,7 @@ Provides Rails integration for the [Rodauth] authentication framework.
|
|
24
24
|
* [How to build an OIDC provider using rodauth-oauth on Rails](https://honeyryderchuck.gitlab.io/httpx/2021/03/15/oidc-provider-on-rails-using-rodauth-oauth.html)
|
25
25
|
* [What It Took to Build a Rails Integration for Rodauth](https://janko.io/what-it-took-to-build-a-rails-integration-for-rodauth/)
|
26
26
|
* [Social Login in Rails with Rodauth](https://janko.io/social-login-in-rails-with-rodauth/)
|
27
|
+
* [Passkey Authentication with Rodauth](https://janko.io/passkey-authentication-with-rodauth/)
|
27
28
|
|
28
29
|
## Why Rodauth?
|
29
30
|
|
@@ -185,25 +186,6 @@ current_account #=> #<Account id=123 email="user@example.com">
|
|
185
186
|
current_account.email #=> "user@example.com"
|
186
187
|
```
|
187
188
|
|
188
|
-
If the session is logged in, but the account doesn't exist in the database, the
|
189
|
-
session will be reset.
|
190
|
-
|
191
|
-
#### Custom account model
|
192
|
-
|
193
|
-
The `#rails_account` method will try to infer the account model class from the
|
194
|
-
configured accounts table name. However, if the model class cannot be inferred
|
195
|
-
from the table name, you can configure it manually:
|
196
|
-
|
197
|
-
```rb
|
198
|
-
# app/misc/rodauth_main.rb
|
199
|
-
class RodauthMain < Rodauth::Rails::Auth
|
200
|
-
configure do
|
201
|
-
# ...
|
202
|
-
rails_account_model { Authentication::Account } # custom model name
|
203
|
-
end
|
204
|
-
end
|
205
|
-
```
|
206
|
-
|
207
189
|
### Requiring authentication
|
208
190
|
|
209
191
|
You'll likely want to require authentication for certain parts of your app,
|
@@ -219,8 +201,8 @@ class RodauthApp < Rodauth::Rails::App
|
|
219
201
|
# ...
|
220
202
|
r.rodauth # route rodauth requests
|
221
203
|
|
222
|
-
# require authentication for /dashboard/*
|
223
|
-
if r.path.start_with?("/dashboard")
|
204
|
+
# require authentication for /dashboard/* routes
|
205
|
+
if r.path.start_with?("/dashboard")
|
224
206
|
rodauth.require_account # redirect to login page if not authenticated
|
225
207
|
end
|
226
208
|
end
|
@@ -245,12 +227,6 @@ class DashboardController < ApplicationController
|
|
245
227
|
before_action :authenticate
|
246
228
|
end
|
247
229
|
```
|
248
|
-
```rb
|
249
|
-
# app/controllers/posts_controller.rb
|
250
|
-
class PostsController < ApplicationController
|
251
|
-
before_action :authenticate, except: [:index, :show]
|
252
|
-
end
|
253
|
-
```
|
254
230
|
|
255
231
|
#### Routing constraints
|
256
232
|
|
@@ -279,42 +255,83 @@ Rails.application.routes.draw do
|
|
279
255
|
end
|
280
256
|
```
|
281
257
|
|
282
|
-
|
258
|
+
You can specify a different Rodauth configuration by passing the configuration name:
|
283
259
|
|
284
260
|
```rb
|
285
261
|
# config/routes.rb
|
286
262
|
Rails.application.routes.draw do
|
287
|
-
|
288
|
-
constraints Rodauth::Rails.authenticated { |rodauth| rodauth.rails_account.admin? } do
|
263
|
+
constraints Rodauth::Rails.authenticated(:admin) do
|
289
264
|
# ...
|
290
265
|
end
|
291
266
|
end
|
292
267
|
```
|
293
268
|
|
294
|
-
|
269
|
+
If you need something more custom, you can always create the routing constraint
|
270
|
+
manually:
|
295
271
|
|
296
272
|
```rb
|
297
273
|
# config/routes.rb
|
298
274
|
Rails.application.routes.draw do
|
299
|
-
constraints
|
300
|
-
#
|
275
|
+
constraints -> (r) { !r.env["rodauth"].logged_in? } do # or env["rodauth.admin"]
|
276
|
+
# routes when the user is not logged in
|
301
277
|
end
|
302
278
|
end
|
303
279
|
```
|
304
280
|
|
305
|
-
|
306
|
-
|
281
|
+
### Controller
|
282
|
+
|
283
|
+
Your Rodauth configuration is connected to a Rails controller (`RodauthController` by default), and
|
284
|
+
it automatically executes any callbacks and rescue handlers defined on it (or the parent controller)
|
285
|
+
around Rodauth endpoints.
|
307
286
|
|
308
287
|
```rb
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
288
|
+
class RodauthController < ApplicationController
|
289
|
+
before_action :set_locale # executes before Rodauth endpoints
|
290
|
+
rescue_from("MyApp::SomeError") { |exception| ... } # rescues around Rodauth endpoints
|
291
|
+
end
|
292
|
+
```
|
293
|
+
|
294
|
+
#### Calling controller methods
|
295
|
+
|
296
|
+
You can call any controller methods from your Rodauth configuration via `rails_controller_eval`:
|
297
|
+
|
298
|
+
```rb
|
299
|
+
# app/controllers/application_controller.rb
|
300
|
+
class ApplicationController < ActionController::Base
|
301
|
+
private
|
302
|
+
def setup_tracking(account_id)
|
303
|
+
# ... some implementation ...
|
304
|
+
end
|
305
|
+
end
|
306
|
+
```
|
307
|
+
```rb
|
308
|
+
# app/misc/rodauth_main.rb
|
309
|
+
class RodauthMain < Rodauth::Rails::Auth
|
310
|
+
configure do
|
311
|
+
after_create_account do
|
312
|
+
rails_controller_eval { setup_tracking(account_id) }
|
313
|
+
end
|
313
314
|
end
|
314
315
|
end
|
315
316
|
```
|
316
317
|
|
317
|
-
###
|
318
|
+
### Rails URL helpers
|
319
|
+
|
320
|
+
Inside Rodauth configuration and the `route` block you can access Rails route
|
321
|
+
helpers through `#rails_routes`:
|
322
|
+
|
323
|
+
```rb
|
324
|
+
# app/misc/rodauth_main.rb
|
325
|
+
class RodauthMain < Rodauth::Rails::Auth
|
326
|
+
configure do
|
327
|
+
login_redirect { rails_routes.activity_path }
|
328
|
+
change_password_redirect { rails_routes.profile_path }
|
329
|
+
change_login_redirect { rails_routes.profile_path }
|
330
|
+
end
|
331
|
+
end
|
332
|
+
```
|
333
|
+
|
334
|
+
## Views
|
318
335
|
|
319
336
|
The templates built into Rodauth are useful when getting started, but soon
|
320
337
|
you'll want to start editing the markup. You can run the following command to
|
@@ -349,7 +366,7 @@ Use `--name` to generate views for a different Rodauth configuration:
|
|
349
366
|
$ rails generate rodauth:views webauthn two_factor_base --name admin
|
350
367
|
```
|
351
368
|
|
352
|
-
|
369
|
+
### Page titles
|
353
370
|
|
354
371
|
The generated configuration sets `title_instance_variable` to make page titles
|
355
372
|
available in your views via `@page_title` instance variable, which you can then
|
@@ -359,9 +376,7 @@ use in your layout:
|
|
359
376
|
# app/misc/rodauth_main.rb
|
360
377
|
class RodauthMain < Rodauth::Rails::Auth
|
361
378
|
configure do
|
362
|
-
# ...
|
363
379
|
title_instance_variable :@page_title
|
364
|
-
# ...
|
365
380
|
end
|
366
381
|
end
|
367
382
|
```
|
@@ -373,28 +388,11 @@ end
|
|
373
388
|
<title><%= @page_title || "Default title" %></title>
|
374
389
|
<!-- ... -->
|
375
390
|
</head>
|
376
|
-
|
377
|
-
<!-- ... -->
|
378
|
-
</body>
|
391
|
+
<!-- ... -->
|
379
392
|
</html>
|
380
393
|
```
|
381
394
|
|
382
|
-
|
383
|
-
generated Rodauth views, giving it the result of the corresponding
|
384
|
-
`*_page_title` method:
|
385
|
-
|
386
|
-
```erb
|
387
|
-
<!-- app/views/rodauth/login.html.erb -->
|
388
|
-
<%= content_for :page_title, rodauth.login_page_title %>
|
389
|
-
<!-- ... -->
|
390
|
-
```
|
391
|
-
```erb
|
392
|
-
<!-- app/views/rodauth/change_password.html.erb -->
|
393
|
-
<%= content_for :page_title, rodauth.change_password_page_title %>
|
394
|
-
<!-- ... -->
|
395
|
-
```
|
396
|
-
|
397
|
-
#### Layout
|
395
|
+
### Layout
|
398
396
|
|
399
397
|
To use different layouts for different Rodauth views, you can compare the
|
400
398
|
request path in the layout method:
|
@@ -422,7 +420,7 @@ class RodauthController < ApplicationController
|
|
422
420
|
end
|
423
421
|
```
|
424
422
|
|
425
|
-
|
423
|
+
### Turbo
|
426
424
|
|
427
425
|
[Turbo] has been disabled by default on all built-in and generated view
|
428
426
|
templates, because some Rodauth actions (multi-phase login, adding recovery
|
@@ -431,7 +429,7 @@ codes) aren't Turbo-compatible, as they return 200 responses on POST requests.
|
|
431
429
|
That being said, most of Rodauth *is* Turbo-compatible, so feel free to enable
|
432
430
|
Turbo for actions where you want to use it.
|
433
431
|
|
434
|
-
|
432
|
+
## Mailer
|
435
433
|
|
436
434
|
The install generator will create `RodauthMailer` with default email templates,
|
437
435
|
and configure Rodauth features that send emails as part of the authentication
|
@@ -440,73 +438,45 @@ flow to use it.
|
|
440
438
|
```rb
|
441
439
|
# app/mailers/rodauth_mailer.rb
|
442
440
|
class RodauthMailer < ApplicationMailer
|
443
|
-
def verify_account(account_id, key)
|
444
|
-
|
445
|
-
end
|
446
|
-
def
|
447
|
-
|
448
|
-
end
|
449
|
-
def verify_login_change(account_id, old_login, new_login, key)
|
450
|
-
# ...
|
451
|
-
end
|
452
|
-
def password_changed(account_id)
|
453
|
-
# ...
|
454
|
-
end
|
455
|
-
# def email_auth(account_id, key)
|
456
|
-
# ...
|
457
|
-
# end
|
458
|
-
# def unlock_account(account_id, key)
|
459
|
-
# ...
|
460
|
-
# end
|
441
|
+
def verify_account(account_id, key) ... end
|
442
|
+
def reset_password(account_id, key) ... end
|
443
|
+
def verify_login_change(account_id, key) ... end
|
444
|
+
def password_changed(account_id) ... end
|
445
|
+
# def email_auth(account_id, key) ... end
|
446
|
+
# def unlock_account(account_id, key) ... end
|
461
447
|
end
|
462
448
|
```
|
463
449
|
```rb
|
464
450
|
# app/misc/rodauth_main.rb
|
465
451
|
class RodauthMain < Rodauth::Rails::Auth
|
466
452
|
configure do
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
end
|
474
|
-
create_verify_login_change_email do |_login|
|
475
|
-
RodauthMailer.verify_login_change(account_id, verify_login_change_old_login, verify_login_change_new_login, verify_login_change_key_value)
|
476
|
-
end
|
477
|
-
create_password_changed_email do
|
478
|
-
RodauthMailer.password_changed(account_id)
|
479
|
-
end
|
480
|
-
# create_email_auth_email do
|
481
|
-
# RodauthMailer.email_auth(account_id, email_auth_key_value)
|
482
|
-
# end
|
483
|
-
# create_unlock_account_email do
|
484
|
-
# RodauthMailer.unlock_account(account_id, unlock_account_key_value)
|
485
|
-
# end
|
453
|
+
create_reset_password_email { RodauthMailer.reset_password(account_id, reset_password_key_value) }
|
454
|
+
create_verify_account_email { RodauthMailer.verify_account(account_id, verify_account_key_value) }
|
455
|
+
create_verify_login_change_email { |_login| RodauthMailer.verify_login_change(account_id, verify_login_change_key_value) }
|
456
|
+
create_password_changed_email { RodauthMailer.password_changed(account_id) }
|
457
|
+
# create_email_auth_email { RodauthMailer.email_auth(account_id, email_auth_key_value) }
|
458
|
+
# create_unlock_account_email { RodauthMailer.unlock_account(account_id, unlock_account_key_value) }
|
486
459
|
send_email do |email|
|
487
460
|
# queue email delivery on the mailer after the transaction commits
|
488
461
|
db.after_commit { email.deliver_later }
|
489
462
|
end
|
490
|
-
# ...
|
491
463
|
end
|
492
464
|
end
|
493
465
|
```
|
494
466
|
|
495
467
|
This configuration calls `#deliver_later`, which uses Active Job to deliver
|
496
|
-
emails in a background job.
|
497
|
-
|
498
|
-
deliveries. However, if you want to send emails synchronously, you can modify
|
499
|
-
the configuration to call `#deliver_now` instead.
|
468
|
+
emails in a background job. If you want to send emails synchronously, you can
|
469
|
+
modify the configuration to call `#deliver_now` instead.
|
500
470
|
|
501
471
|
If you're using a background processing library without an Active Job adapter,
|
502
472
|
or a 3rd-party service for sending transactional emails, see [this wiki
|
503
473
|
page][custom mailer worker] on how to set it up.
|
504
474
|
|
505
|
-
|
475
|
+
## Migrations
|
506
476
|
|
507
477
|
The install generator will create a migration for tables used by the Rodauth
|
508
478
|
features enabled by default. For any additional features, you can use the
|
509
|
-
migration generator
|
479
|
+
migration generator to create the required tables:
|
510
480
|
|
511
481
|
```sh
|
512
482
|
$ rails generate rodauth:migration otp sms_codes recovery_codes
|
@@ -522,10 +492,10 @@ class CreateRodauthOtpSmsCodesRecoveryCodes < ActiveRecord::Migration
|
|
522
492
|
end
|
523
493
|
```
|
524
494
|
|
525
|
-
|
495
|
+
### Table prefix
|
526
496
|
|
527
497
|
If you're storing account records in a table other than `accounts`, you'll want
|
528
|
-
to specify the
|
498
|
+
to specify the appropriate table prefix when generating new migrations:
|
529
499
|
|
530
500
|
```sh
|
531
501
|
$ rails generate rodauth:migration base active_sessions --prefix user
|
@@ -546,7 +516,7 @@ class CreateRodauthUserBaseActiveSessions < ActiveRecord::Migration
|
|
546
516
|
end
|
547
517
|
```
|
548
518
|
|
549
|
-
|
519
|
+
### Custom migration name
|
550
520
|
|
551
521
|
You can change the default migration name:
|
552
522
|
|
@@ -570,37 +540,23 @@ tables used by enabled authentication features.
|
|
570
540
|
|
571
541
|
```rb
|
572
542
|
class Account < ActiveRecord::Base # Sequel::Model
|
573
|
-
include Rodauth::Rails.model # or
|
543
|
+
include Rodauth::Rails.model # or Rodauth::Rails.model(:admin)
|
574
544
|
end
|
575
545
|
```
|
576
|
-
|
577
|
-
The password attribute can be used to set or clear the password hash. It
|
578
|
-
handles both storing the password hash in a column on the accounts table, or in
|
579
|
-
a separate table.
|
580
|
-
|
581
546
|
```rb
|
547
|
+
# setting password hash
|
582
548
|
account = Account.create!(email: "user@example.com", password: "secret123")
|
583
|
-
|
584
|
-
# when password hash is stored in a column on the accounts table
|
585
549
|
account.password_hash #=> "$2a$12$k/Ub1I2iomi84RacqY89Hu4.M0vK7klRnRtzorDyvOkVI.hKhkNw."
|
586
550
|
|
587
|
-
#
|
588
|
-
account.
|
589
|
-
account.password_hash.password_hash #=> "$2a$12$k/Ub1..." (inaccessible when using database authentication functions)
|
590
|
-
|
591
|
-
account.password = nil # clears password hash
|
551
|
+
# clearing password hash
|
552
|
+
account.password = nil
|
592
553
|
account.password_hash #=> nil
|
593
|
-
```
|
594
|
-
|
595
|
-
The associations are defined for tables used by enabled authentication features:
|
596
554
|
|
597
|
-
|
555
|
+
# associations
|
598
556
|
account.remember_key #=> #<Account::RememberKey> (record from `account_remember_keys` table)
|
599
557
|
account.active_session_keys #=> [#<Account::ActiveSessionKey>,...] (records from `account_active_session_keys` table)
|
600
558
|
```
|
601
559
|
|
602
|
-
See the [rodauth-model] documentation for more details.
|
603
|
-
|
604
560
|
## Multiple configurations
|
605
561
|
|
606
562
|
If you need to handle multiple types of accounts that require different
|
@@ -646,43 +602,9 @@ Then in your application you can reference the secondary Rodauth instance:
|
|
646
602
|
rodauth(:admin).login_path #=> "/admin/login"
|
647
603
|
```
|
648
604
|
|
649
|
-
|
650
|
-
configuration to the database
|
651
|
-
that
|
652
|
-
|
653
|
-
### Sharing configuration
|
654
|
-
|
655
|
-
If there are common settings that you want to share between Rodauth
|
656
|
-
configurations, you can do so via inheritance:
|
657
|
-
|
658
|
-
```rb
|
659
|
-
# app/misc/rodauth_base.rb
|
660
|
-
class RodauthBase < Rodauth::Rails::Auth
|
661
|
-
# common settings that are shared between multiple configurations
|
662
|
-
configure do
|
663
|
-
enable :login, :logout
|
664
|
-
login_return_to_requested_location? true
|
665
|
-
logout_redirect "/"
|
666
|
-
# ...
|
667
|
-
end
|
668
|
-
end
|
669
|
-
```
|
670
|
-
```rb
|
671
|
-
# app/misc/rodauth_main.rb
|
672
|
-
class RodauthMain < RodauthBase # inherit common settings
|
673
|
-
configure do
|
674
|
-
# ... customize main ...
|
675
|
-
end
|
676
|
-
end
|
677
|
-
```
|
678
|
-
```rb
|
679
|
-
# app/misc/rodauth_admin.rb
|
680
|
-
class RodauthAdmin < RodauthBase # inherit common settings
|
681
|
-
configure do
|
682
|
-
# ... customize admin ...
|
683
|
-
end
|
684
|
-
end
|
685
|
-
```
|
605
|
+
You'll likely want to save the information of which account belongs to which
|
606
|
+
configuration to the database, see [this guide][account types] on how you can do
|
607
|
+
that. Note that you can also [share configuration via inheritance][inheritance].
|
686
608
|
|
687
609
|
## Outside of a request
|
688
610
|
|
@@ -769,6 +691,34 @@ Rodauth::Rails.rodauth(session: { two_factor_auth_setup: true })
|
|
769
691
|
Rodauth::Rails.rodauth(:admin, params: { "param" => "value" })
|
770
692
|
```
|
771
693
|
|
694
|
+
### Using as a library
|
695
|
+
|
696
|
+
Rodauth offers a `Rodauth.lib` method for configuring Rodauth so that it can be used as a library, instead of routing requests (see [internal_request] feature). This gem provides a `Rodauth::Rails.lib` counterpart that does the same but with Rails integration:
|
697
|
+
|
698
|
+
```rb
|
699
|
+
# app/misc/rodauth_main.rb
|
700
|
+
require "rodauth/rails"
|
701
|
+
require "sequel/core"
|
702
|
+
|
703
|
+
RodauthMain = Rodauth::Rails.lib do
|
704
|
+
enable :create_account, :login, :close_account
|
705
|
+
db Sequel.postgres(extensions: :activerecord_connection, keep_reference: false)
|
706
|
+
# ...
|
707
|
+
end
|
708
|
+
```
|
709
|
+
```rb
|
710
|
+
RodauthMain.create_account(login: "email@example.com", password: "secret123")
|
711
|
+
RodauthMain.login(login: "email@example.com", password: "secret123")
|
712
|
+
RodauthMain.close_account(account_login: "email@example.com")
|
713
|
+
```
|
714
|
+
|
715
|
+
Note that you'll want to skip requiring `rodauth-rails` on Rails boot, so that it doesn't insert the middleware automatically, and remove the initializer.
|
716
|
+
|
717
|
+
```rb
|
718
|
+
# Gemfile
|
719
|
+
gem "rodauth-rails", require: false
|
720
|
+
```
|
721
|
+
|
772
722
|
## Testing
|
773
723
|
|
774
724
|
For system and integration tests, which run the whole middleware stack,
|
@@ -788,13 +738,13 @@ end
|
|
788
738
|
```
|
789
739
|
|
790
740
|
One can write `ActionDispatch::IntegrationTest` test helpers for `login` and
|
791
|
-
`logout` by making requests to the
|
741
|
+
`logout` by making requests to the Rodauth endpoints:
|
792
742
|
|
793
743
|
```rb
|
794
744
|
# test/controllers/articles_controller_test.rb
|
795
745
|
class ArticlesControllerTest < ActionDispatch::IntegrationTest
|
796
|
-
def login(
|
797
|
-
post "/login", params: {
|
746
|
+
def login(email, password)
|
747
|
+
post "/login", params: { email: email, password: password }
|
798
748
|
assert_redirected_to "/"
|
799
749
|
end
|
800
750
|
|
@@ -846,6 +796,15 @@ methods:
|
|
846
796
|
| `rails_controller` | Controller class to use for rendering and CSRF protection. |
|
847
797
|
| `rails_account_model` | Model class connected with the accounts table. |
|
848
798
|
|
799
|
+
```rb
|
800
|
+
class RodauthMain < Rodauth::Rails::Auth
|
801
|
+
configure do
|
802
|
+
rails_controller { Authentication::RodauthController }
|
803
|
+
rails_account_model { Authentication::Account }
|
804
|
+
end
|
805
|
+
end
|
806
|
+
```
|
807
|
+
|
849
808
|
For the list of configuration methods provided by Rodauth, see the [feature
|
850
809
|
documentation].
|
851
810
|
|
@@ -875,53 +834,10 @@ end
|
|
875
834
|
rodauth.admin? #=> true
|
876
835
|
```
|
877
836
|
|
878
|
-
### Rails URL helpers
|
879
|
-
|
880
|
-
Inside Rodauth configuration and the `route` block you can access Rails route
|
881
|
-
helpers through `#rails_routes`:
|
882
|
-
|
883
|
-
```rb
|
884
|
-
# app/misc/rodauth_main.rb
|
885
|
-
class RodauthMain < Rodauth::Rails::Auth
|
886
|
-
configure do
|
887
|
-
login_redirect { rails_routes.activity_path }
|
888
|
-
change_password_redirect { rails_routes.profile_path }
|
889
|
-
change_login_redirect { rails_routes.profile_path }
|
890
|
-
end
|
891
|
-
end
|
892
|
-
```
|
893
|
-
|
894
|
-
### Calling controller methods
|
895
|
-
|
896
|
-
When using Rodauth before/after hooks or generally overriding your Rodauth
|
897
|
-
configuration, in some cases you might want to call methods defined on your
|
898
|
-
controllers. You can do so with `rails_controller_eval`, for example:
|
899
|
-
|
900
|
-
```rb
|
901
|
-
# app/controllers/application_controller.rb
|
902
|
-
class ApplicationController < ActionController::Base
|
903
|
-
private
|
904
|
-
def setup_tracking(account_id)
|
905
|
-
# ... some implementation ...
|
906
|
-
end
|
907
|
-
end
|
908
|
-
```
|
909
|
-
```rb
|
910
|
-
# app/misc/rodauth_main.rb
|
911
|
-
class RodauthMain < Rodauth::Rails::Auth
|
912
|
-
configure do
|
913
|
-
after_create_account do
|
914
|
-
rails_controller_eval { setup_tracking(account_id) }
|
915
|
-
end
|
916
|
-
end
|
917
|
-
end
|
918
|
-
```
|
919
|
-
|
920
837
|
### Single-file configuration
|
921
838
|
|
922
|
-
If you would prefer
|
923
|
-
|
924
|
-
anonymous auth class.
|
839
|
+
If you would prefer, you can have all your Rodauth logic contained inside the
|
840
|
+
Rodauth app class:
|
925
841
|
|
926
842
|
```rb
|
927
843
|
# app/misc/rodauth_app.rb
|
@@ -944,6 +860,19 @@ class RodauthApp < Rodauth::Rails::App
|
|
944
860
|
end
|
945
861
|
```
|
946
862
|
|
863
|
+
### Manually inserting middleware
|
864
|
+
|
865
|
+
You can choose to insert the Rodauth middleware somewhere earlier than
|
866
|
+
in front of the Rails router:
|
867
|
+
|
868
|
+
```rb
|
869
|
+
Rodauth::Rails.configure do |config|
|
870
|
+
config.middleware = false # disable auto-insertion
|
871
|
+
end
|
872
|
+
|
873
|
+
Rails.application.config.middleware.insert_before AnotherMiddleware, Rodauth::Rails::Middleware
|
874
|
+
```
|
875
|
+
|
947
876
|
## How it works
|
948
877
|
|
949
878
|
### Rack middleware
|
@@ -958,16 +887,6 @@ $ rails middleware
|
|
958
887
|
# run MyApp::Application.routes
|
959
888
|
```
|
960
889
|
|
961
|
-
It can be inserted at any point in the middleware stack:
|
962
|
-
|
963
|
-
```rb
|
964
|
-
Rodauth::Rails.configure do |config|
|
965
|
-
config.middleware = false # disable auto-insertion
|
966
|
-
end
|
967
|
-
|
968
|
-
Rails.application.config.middleware.insert_before AnotherMiddleware, Rodauth::Rails::Middleware
|
969
|
-
```
|
970
|
-
|
971
890
|
The middleware retrieves the Rodauth app via `Rodauth::Rails.app`, which is
|
972
891
|
specified as a string to keep the class autoloadable and reloadable in
|
973
892
|
development.
|
@@ -1239,3 +1158,4 @@ conduct](CODE_OF_CONDUCT.md).
|
|
1239
1158
|
[Turbo]: https://turbo.hotwired.dev/
|
1240
1159
|
[rodauth-model]: https://github.com/janko/rodauth-model
|
1241
1160
|
[JSON API]: https://github.com/janko/rodauth-rails/wiki/JSON-API
|
1161
|
+
[inheritance]: http://rodauth.jeremyevans.net/rdoc/files/doc/guides/share_configuration_rdoc.html
|
data/lib/rodauth/rails.rb
CHANGED
@@ -16,6 +16,16 @@ module Rodauth
|
|
16
16
|
@middleware = true
|
17
17
|
|
18
18
|
class << self
|
19
|
+
def lib(&block)
|
20
|
+
c = Class.new(Rodauth::Rails::App)
|
21
|
+
c.configure(json: false) do
|
22
|
+
enable :internal_request
|
23
|
+
instance_exec(&block)
|
24
|
+
end
|
25
|
+
c.freeze
|
26
|
+
c.rodauth
|
27
|
+
end
|
28
|
+
|
19
29
|
def rodauth(name = nil, account: nil, **options)
|
20
30
|
auth_class = app.rodauth!(name)
|
21
31
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rodauth-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janko Marohnić
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|