devise 3.2.4 → 4.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +259 -994
- data/MIT-LICENSE +1 -1
- data/README.md +336 -99
- data/app/controllers/devise/confirmations_controller.rb +9 -3
- data/app/controllers/devise/omniauth_callbacks_controller.rb +12 -6
- data/app/controllers/devise/passwords_controller.rb +19 -6
- data/app/controllers/devise/registrations_controller.rb +55 -22
- data/app/controllers/devise/sessions_controller.rb +44 -14
- data/app/controllers/devise/unlocks_controller.rb +7 -2
- data/app/controllers/devise_controller.rb +65 -29
- data/app/helpers/devise_helper.rb +12 -19
- data/app/mailers/devise/mailer.rb +10 -0
- data/app/views/devise/confirmations/new.html.erb +8 -4
- data/app/views/devise/mailer/email_changed.html.erb +7 -0
- data/app/views/devise/mailer/password_change.html.erb +3 -0
- data/app/views/devise/passwords/edit.html.erb +15 -6
- data/app/views/devise/passwords/new.html.erb +8 -4
- data/app/views/devise/registrations/edit.html.erb +27 -13
- data/app/views/devise/registrations/new.html.erb +19 -8
- data/app/views/devise/sessions/new.html.erb +18 -9
- data/app/views/devise/shared/_error_messages.html.erb +15 -0
- data/app/views/devise/shared/{_links.erb → _links.html.erb} +9 -9
- data/app/views/devise/unlocks/new.html.erb +8 -4
- data/config/locales/en.yml +22 -16
- data/lib/devise/controllers/helpers.rb +109 -29
- data/lib/devise/controllers/rememberable.rb +12 -3
- data/lib/devise/controllers/scoped_views.rb +2 -0
- data/lib/devise/controllers/sign_in_out.rb +36 -20
- data/lib/devise/controllers/store_location.rb +31 -5
- data/lib/devise/controllers/url_helpers.rb +9 -7
- data/lib/devise/delegator.rb +2 -0
- data/lib/devise/encryptor.rb +24 -0
- data/lib/devise/failure_app.rb +116 -36
- data/lib/devise/hooks/activatable.rb +5 -4
- data/lib/devise/hooks/csrf_cleaner.rb +5 -1
- data/lib/devise/hooks/forgetable.rb +2 -0
- data/lib/devise/hooks/lockable.rb +6 -1
- data/lib/devise/hooks/proxy.rb +3 -1
- data/lib/devise/hooks/rememberable.rb +2 -0
- data/lib/devise/hooks/timeoutable.rb +15 -8
- data/lib/devise/hooks/trackable.rb +2 -0
- data/lib/devise/mailers/helpers.rb +7 -4
- data/lib/devise/mapping.rb +8 -2
- data/lib/devise/models/authenticatable.rb +76 -51
- data/lib/devise/models/confirmable.rb +129 -34
- data/lib/devise/models/database_authenticatable.rb +107 -30
- data/lib/devise/models/lockable.rb +19 -9
- data/lib/devise/models/omniauthable.rb +2 -0
- data/lib/devise/models/recoverable.rb +62 -26
- data/lib/devise/models/registerable.rb +4 -0
- data/lib/devise/models/rememberable.rb +58 -29
- data/lib/devise/models/timeoutable.rb +2 -6
- data/lib/devise/models/trackable.rb +20 -4
- data/lib/devise/models/validatable.rb +12 -5
- data/lib/devise/models.rb +3 -1
- data/lib/devise/modules.rb +2 -0
- data/lib/devise/omniauth/config.rb +2 -0
- data/lib/devise/omniauth/url_helpers.rb +14 -5
- data/lib/devise/omniauth.rb +2 -0
- data/lib/devise/orm/active_record.rb +5 -1
- data/lib/devise/orm/mongoid.rb +6 -2
- data/lib/devise/parameter_filter.rb +4 -0
- data/lib/devise/parameter_sanitizer.rb +139 -65
- data/lib/devise/rails/routes.rb +80 -61
- data/lib/devise/rails/warden_compat.rb +3 -10
- data/lib/devise/rails.rb +8 -17
- data/lib/devise/secret_key_finder.rb +27 -0
- data/lib/devise/strategies/authenticatable.rb +18 -7
- data/lib/devise/strategies/base.rb +2 -0
- data/lib/devise/strategies/database_authenticatable.rb +13 -5
- data/lib/devise/strategies/rememberable.rb +15 -3
- data/lib/devise/test/controller_helpers.rb +165 -0
- data/lib/devise/test/integration_helpers.rb +63 -0
- data/lib/devise/test_helpers.rb +7 -124
- data/lib/devise/time_inflector.rb +2 -0
- data/lib/devise/token_generator.rb +3 -41
- data/lib/devise/version.rb +3 -1
- data/lib/devise.rb +106 -79
- data/lib/generators/active_record/devise_generator.rb +44 -7
- data/lib/generators/active_record/templates/migration.rb +5 -3
- data/lib/generators/active_record/templates/migration_existing.rb +5 -3
- data/lib/generators/devise/controllers_generator.rb +46 -0
- data/lib/generators/devise/devise_generator.rb +4 -2
- data/lib/generators/devise/install_generator.rb +17 -0
- data/lib/generators/devise/orm_helpers.rb +10 -21
- data/lib/generators/devise/views_generator.rb +21 -11
- data/lib/generators/mongoid/devise_generator.rb +7 -5
- data/lib/generators/templates/README +2 -9
- data/lib/generators/templates/controllers/README +14 -0
- data/lib/generators/templates/controllers/confirmations_controller.rb +30 -0
- data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +30 -0
- data/lib/generators/templates/controllers/passwords_controller.rb +34 -0
- data/lib/generators/templates/controllers/registrations_controller.rb +62 -0
- data/lib/generators/templates/controllers/sessions_controller.rb +27 -0
- data/lib/generators/templates/controllers/unlocks_controller.rb +30 -0
- data/lib/generators/templates/devise.rb +69 -30
- data/lib/generators/templates/markerb/confirmation_instructions.markerb +1 -1
- data/lib/generators/templates/markerb/email_changed.markerb +7 -0
- data/lib/generators/templates/markerb/password_change.markerb +3 -0
- data/lib/generators/templates/markerb/reset_password_instructions.markerb +1 -1
- data/lib/generators/templates/markerb/unlock_instructions.markerb +1 -1
- data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +5 -1
- data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +10 -2
- data/lib/generators/templates/simple_form_for/passwords/new.html.erb +4 -1
- data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +11 -3
- data/lib/generators/templates/simple_form_for/registrations/new.html.erb +11 -3
- data/lib/generators/templates/simple_form_for/sessions/new.html.erb +9 -4
- data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +4 -1
- metadata +31 -259
- data/.gitignore +0 -11
- data/.travis.yml +0 -28
- data/.yardopts +0 -9
- data/CONTRIBUTING.md +0 -14
- data/Gemfile +0 -29
- data/Gemfile.lock +0 -160
- data/Rakefile +0 -35
- data/devise.gemspec +0 -27
- data/devise.png +0 -0
- data/gemfiles/Gemfile.rails-3.2-stable +0 -29
- data/gemfiles/Gemfile.rails-4.0-stable +0 -29
- data/gemfiles/Gemfile.rails-head +0 -29
- data/test/controllers/custom_strategy_test.rb +0 -62
- data/test/controllers/helpers_test.rb +0 -276
- data/test/controllers/internal_helpers_test.rb +0 -123
- data/test/controllers/passwords_controller_test.rb +0 -31
- data/test/controllers/sessions_controller_test.rb +0 -103
- data/test/controllers/url_helpers_test.rb +0 -59
- data/test/delegator_test.rb +0 -19
- data/test/devise_test.rb +0 -94
- data/test/failure_app_test.rb +0 -232
- data/test/generators/active_record_generator_test.rb +0 -103
- data/test/generators/devise_generator_test.rb +0 -39
- data/test/generators/install_generator_test.rb +0 -13
- data/test/generators/mongoid_generator_test.rb +0 -23
- data/test/generators/views_generator_test.rb +0 -96
- data/test/helpers/devise_helper_test.rb +0 -51
- data/test/integration/authenticatable_test.rb +0 -713
- data/test/integration/confirmable_test.rb +0 -284
- data/test/integration/database_authenticatable_test.rb +0 -84
- data/test/integration/http_authenticatable_test.rb +0 -105
- data/test/integration/lockable_test.rb +0 -239
- data/test/integration/omniauthable_test.rb +0 -133
- data/test/integration/recoverable_test.rb +0 -334
- data/test/integration/registerable_test.rb +0 -349
- data/test/integration/rememberable_test.rb +0 -167
- data/test/integration/timeoutable_test.rb +0 -183
- data/test/integration/trackable_test.rb +0 -92
- data/test/mailers/confirmation_instructions_test.rb +0 -115
- data/test/mailers/reset_password_instructions_test.rb +0 -96
- data/test/mailers/unlock_instructions_test.rb +0 -91
- data/test/mapping_test.rb +0 -127
- data/test/models/authenticatable_test.rb +0 -13
- data/test/models/confirmable_test.rb +0 -454
- data/test/models/database_authenticatable_test.rb +0 -249
- data/test/models/lockable_test.rb +0 -316
- data/test/models/omniauthable_test.rb +0 -7
- data/test/models/recoverable_test.rb +0 -184
- data/test/models/registerable_test.rb +0 -7
- data/test/models/rememberable_test.rb +0 -183
- data/test/models/serializable_test.rb +0 -49
- data/test/models/timeoutable_test.rb +0 -51
- data/test/models/trackable_test.rb +0 -13
- data/test/models/validatable_test.rb +0 -127
- data/test/models_test.rb +0 -144
- data/test/omniauth/config_test.rb +0 -57
- data/test/omniauth/url_helpers_test.rb +0 -54
- data/test/orm/active_record.rb +0 -10
- data/test/orm/mongoid.rb +0 -13
- data/test/parameter_sanitizer_test.rb +0 -81
- data/test/rails_app/Rakefile +0 -6
- data/test/rails_app/app/active_record/admin.rb +0 -6
- data/test/rails_app/app/active_record/shim.rb +0 -2
- data/test/rails_app/app/active_record/user.rb +0 -6
- data/test/rails_app/app/controllers/admins/sessions_controller.rb +0 -6
- data/test/rails_app/app/controllers/admins_controller.rb +0 -11
- data/test/rails_app/app/controllers/application_controller.rb +0 -9
- data/test/rails_app/app/controllers/home_controller.rb +0 -25
- data/test/rails_app/app/controllers/publisher/registrations_controller.rb +0 -2
- data/test/rails_app/app/controllers/publisher/sessions_controller.rb +0 -2
- data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +0 -14
- data/test/rails_app/app/controllers/users_controller.rb +0 -31
- data/test/rails_app/app/helpers/application_helper.rb +0 -3
- data/test/rails_app/app/mailers/users/mailer.rb +0 -12
- data/test/rails_app/app/mongoid/admin.rb +0 -29
- data/test/rails_app/app/mongoid/shim.rb +0 -23
- data/test/rails_app/app/mongoid/user.rb +0 -39
- data/test/rails_app/app/views/admins/index.html.erb +0 -1
- data/test/rails_app/app/views/admins/sessions/new.html.erb +0 -2
- data/test/rails_app/app/views/home/admin_dashboard.html.erb +0 -1
- data/test/rails_app/app/views/home/index.html.erb +0 -1
- data/test/rails_app/app/views/home/join.html.erb +0 -1
- data/test/rails_app/app/views/home/private.html.erb +0 -1
- data/test/rails_app/app/views/home/user_dashboard.html.erb +0 -1
- data/test/rails_app/app/views/layouts/application.html.erb +0 -24
- data/test/rails_app/app/views/users/edit_form.html.erb +0 -1
- data/test/rails_app/app/views/users/index.html.erb +0 -1
- data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +0 -1
- data/test/rails_app/app/views/users/sessions/new.html.erb +0 -1
- data/test/rails_app/bin/bundle +0 -3
- data/test/rails_app/bin/rails +0 -4
- data/test/rails_app/bin/rake +0 -4
- data/test/rails_app/config/application.rb +0 -40
- data/test/rails_app/config/boot.rb +0 -14
- data/test/rails_app/config/database.yml +0 -18
- data/test/rails_app/config/environment.rb +0 -5
- data/test/rails_app/config/environments/development.rb +0 -30
- data/test/rails_app/config/environments/production.rb +0 -80
- data/test/rails_app/config/environments/test.rb +0 -36
- data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -7
- data/test/rails_app/config/initializers/devise.rb +0 -181
- data/test/rails_app/config/initializers/inflections.rb +0 -2
- data/test/rails_app/config/initializers/secret_token.rb +0 -8
- data/test/rails_app/config/initializers/session_store.rb +0 -1
- data/test/rails_app/config/routes.rb +0 -105
- data/test/rails_app/config.ru +0 -4
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -71
- data/test/rails_app/db/schema.rb +0 -55
- data/test/rails_app/lib/shared_admin.rb +0 -17
- data/test/rails_app/lib/shared_user.rb +0 -29
- data/test/rails_app/public/404.html +0 -26
- data/test/rails_app/public/422.html +0 -26
- data/test/rails_app/public/500.html +0 -26
- data/test/rails_app/public/favicon.ico +0 -0
- data/test/routes_test.rb +0 -262
- data/test/support/action_controller/record_identifier.rb +0 -10
- data/test/support/assertions.rb +0 -40
- data/test/support/helpers.rb +0 -70
- data/test/support/integration.rb +0 -92
- data/test/support/locale/en.yml +0 -8
- data/test/support/mongoid.yml +0 -6
- data/test/support/webrat/integrations/rails.rb +0 -24
- data/test/test_helper.rb +0 -27
- data/test/test_helpers_test.rb +0 -173
- data/test/test_models.rb +0 -33
data/README.md
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
|
3
3
|
By [Plataformatec](http://plataformatec.com.br/).
|
4
4
|
|
5
|
-
[![Build Status](https://api.travis-ci.org/plataformatec/devise.
|
6
|
-
[![Code Climate](https://codeclimate.com/github/plataformatec/devise.
|
5
|
+
[![Build Status](https://api.travis-ci.org/plataformatec/devise.svg?branch=master)](http://travis-ci.org/plataformatec/devise)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/plataformatec/devise.svg)](https://codeclimate.com/github/plataformatec/devise)
|
7
7
|
|
8
8
|
This README is [also available in a friendly navigable format](http://devise.plataformatec.com.br/).
|
9
9
|
|
@@ -12,22 +12,60 @@ Devise is a flexible authentication solution for Rails based on Warden. It:
|
|
12
12
|
* Is Rack based;
|
13
13
|
* Is a complete MVC solution based on Rails engines;
|
14
14
|
* Allows you to have multiple models signed in at the same time;
|
15
|
-
* Is based on a modularity concept: use
|
15
|
+
* Is based on a modularity concept: use only what you really need.
|
16
16
|
|
17
17
|
It's composed of 10 modules:
|
18
18
|
|
19
|
-
* [Database Authenticatable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/DatabaseAuthenticatable):
|
20
|
-
* [Omniauthable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Omniauthable): adds
|
21
|
-
* [Confirmable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Confirmable): sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.
|
22
|
-
* [Recoverable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Recoverable): resets the user password and sends reset instructions.
|
23
|
-
* [Registerable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Registerable): handles signing up users through a registration process, also allowing them to edit and destroy their account.
|
24
|
-
* [Rememberable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Rememberable): manages generating and clearing a token for remembering the user from a saved cookie.
|
25
|
-
* [Trackable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Trackable): tracks sign in count, timestamps and IP address.
|
26
|
-
* [Timeoutable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Timeoutable): expires sessions that have
|
27
|
-
* [Validatable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Validatable): provides validations of email and password. It's optional and can be customized, so you're able to define your own validations.
|
28
|
-
* [Lockable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Lockable): locks an account after a specified number of failed sign-in attempts. Can unlock via email or after a specified time period.
|
19
|
+
* [Database Authenticatable](http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/DatabaseAuthenticatable): hashes and stores a password in the database to validate the authenticity of a user while signing in. The authentication can be done both through POST requests or HTTP Basic Authentication.
|
20
|
+
* [Omniauthable](http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Omniauthable): adds OmniAuth (https://github.com/omniauth/omniauth) support.
|
21
|
+
* [Confirmable](http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Confirmable): sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.
|
22
|
+
* [Recoverable](http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Recoverable): resets the user password and sends reset instructions.
|
23
|
+
* [Registerable](http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Registerable): handles signing up users through a registration process, also allowing them to edit and destroy their account.
|
24
|
+
* [Rememberable](http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Rememberable): manages generating and clearing a token for remembering the user from a saved cookie.
|
25
|
+
* [Trackable](http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Trackable): tracks sign in count, timestamps and IP address.
|
26
|
+
* [Timeoutable](http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Timeoutable): expires sessions that have not been active in a specified period of time.
|
27
|
+
* [Validatable](http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Validatable): provides validations of email and password. It's optional and can be customized, so you're able to define your own validations.
|
28
|
+
* [Lockable](http://www.rubydoc.info/github/plataformatec/devise/master/Devise/Models/Lockable): locks an account after a specified number of failed sign-in attempts. Can unlock via email or after a specified time period.
|
29
|
+
|
30
|
+
## Table of Contents
|
31
|
+
|
32
|
+
<!-- TOC depthFrom:1 depthTo:6 withLinks:1 orderedList:0 -->
|
33
|
+
|
34
|
+
- [Information](#information)
|
35
|
+
- [The Devise wiki](#the-devise-wiki)
|
36
|
+
- [Bug reports](#bug-reports)
|
37
|
+
- [StackOverflow and Mailing List](#stackoverflow-and-mailing-list)
|
38
|
+
- [RDocs](#rdocs)
|
39
|
+
- [Example applications](#example-applications)
|
40
|
+
- [Extensions](#extensions)
|
41
|
+
- [Contributing](#contributing)
|
42
|
+
- [Starting with Rails?](#starting-with-rails)
|
43
|
+
- [Getting started](#getting-started)
|
44
|
+
- [Controller filters and helpers](#controller-filters-and-helpers)
|
45
|
+
- [Configuring Models](#configuring-models)
|
46
|
+
- [Strong Parameters](#strong-parameters)
|
47
|
+
- [Configuring views](#configuring-views)
|
48
|
+
- [Configuring controllers](#configuring-controllers)
|
49
|
+
- [Configuring routes](#configuring-routes)
|
50
|
+
- [I18n](#i18n)
|
51
|
+
- [Test helpers](#test-helpers)
|
52
|
+
- [Controller tests](#controller-tests)
|
53
|
+
- [Integration tests](#integration-tests)
|
54
|
+
- [OmniAuth](#omniauth)
|
55
|
+
- [Configuring multiple models](#configuring-multiple-models)
|
56
|
+
- [ActiveJob Integration](#activejob-integration)
|
57
|
+
- [Password reset tokens and Rails logs](#password-reset-tokens-and-rails-logs)
|
58
|
+
- [Other ORMs](#other-orms)
|
59
|
+
- [Rails API mode](#rails-api-mode)
|
60
|
+
- [Additional information](#additional-information)
|
61
|
+
- [Heroku](#heroku)
|
62
|
+
- [Warden](#warden)
|
63
|
+
- [Contributors](#contributors)
|
64
|
+
- [License](#license)
|
65
|
+
|
66
|
+
<!-- /TOC -->
|
67
|
+
|
29
68
|
|
30
|
-
Devise is guaranteed to be thread-safe on YARV. Thread-safety support on JRuby is on progress.
|
31
69
|
|
32
70
|
## Information
|
33
71
|
|
@@ -43,11 +81,15 @@ If you discover a problem with Devise, we would like to know about it. However,
|
|
43
81
|
|
44
82
|
https://github.com/plataformatec/devise/wiki/Bug-reports
|
45
83
|
|
46
|
-
If you
|
84
|
+
If you have discovered a security related bug, please do *NOT* use the GitHub issue tracker. Send an email to opensource@plataformatec.com.br.
|
85
|
+
|
86
|
+
### StackOverflow and Mailing List
|
47
87
|
|
48
|
-
|
88
|
+
If you have any questions, comments, or concerns, please use StackOverflow instead of the GitHub issue tracker:
|
49
89
|
|
50
|
-
|
90
|
+
http://stackoverflow.com/questions/tagged/devise
|
91
|
+
|
92
|
+
The deprecated mailing list can still be read on
|
51
93
|
|
52
94
|
https://groups.google.com/group/plataformatec-devise
|
53
95
|
|
@@ -77,57 +119,114 @@ We hope that you will consider contributing to Devise. Please read this short ov
|
|
77
119
|
|
78
120
|
https://github.com/plataformatec/devise/wiki/Contributing
|
79
121
|
|
80
|
-
You will usually want to write tests for your changes. To run the test suite, go into Devise's top-level directory and run
|
122
|
+
You will usually want to write tests for your changes. To run the test suite, go into Devise's top-level directory and run `bundle install` and `bin/test`.
|
123
|
+
Devise works with multiple Ruby and Rails versions, and ActiveRecord and Mongoid ORMs, which means you can run the test suite with some modifiers: `DEVISE_ORM` and `BUNDLE_GEMFILE`.
|
124
|
+
|
125
|
+
### DEVISE_ORM
|
126
|
+
Since Devise support both Mongoid and ActiveRecord, we rely on this variable to run specific code for each ORM.
|
127
|
+
The default value of `DEVISE_ORM` is `active_record`. To run the tests for mongoid, you can pass `mongoid`:
|
128
|
+
```
|
129
|
+
DEVISE_ORM=mongoid bin/test
|
130
|
+
|
131
|
+
==> Devise.orm = :mongoid
|
132
|
+
```
|
133
|
+
When running the tests for Mongoid, you will need to have a MongoDB server (version 2.0 or newer) running on your system.
|
134
|
+
|
135
|
+
Please note that the command output will show the variable value being used.
|
136
|
+
|
137
|
+
### BUNDLE_GEMFILE
|
138
|
+
We can use this variable to tell bundler what Gemfile it should use (instead of the one in the current directory).
|
139
|
+
Inside the [gemfiles](https://github.com/plataformatec/devise/tree/master/gemfiles) directory, we have one for each version of Rails we support. When you send us a pull request, it may happen that the test suite breaks on Travis using some of them. If that's the case, you can simulate the same environment using the `BUNDLE_GEMFILE` variable.
|
140
|
+
For example, if the tests broke using Ruby 2.4.2 and Rails 4.1, you can do the following:
|
141
|
+
```bash
|
142
|
+
rbenv shell 2.4.2 # or rvm use 2.4.2
|
143
|
+
BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable bundle install
|
144
|
+
BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable bin/test
|
145
|
+
```
|
146
|
+
|
147
|
+
You can also combine both of them if the tests broke for Mongoid:
|
148
|
+
```bash
|
149
|
+
BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable bundle install
|
150
|
+
BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable DEVISE_ORM=mongoid bin/test
|
151
|
+
```
|
152
|
+
|
153
|
+
### Running tests
|
154
|
+
Devise uses [Mini Test](https://github.com/seattlerb/minitest) as test framework.
|
155
|
+
|
156
|
+
* Running all tests:
|
157
|
+
```bash
|
158
|
+
bin/test
|
159
|
+
```
|
160
|
+
|
161
|
+
* Running tests for an specific file:
|
162
|
+
```bash
|
163
|
+
bin/test test/models/trackable_test.rb
|
164
|
+
```
|
165
|
+
|
166
|
+
* Running a specific test given a regex:
|
167
|
+
```bash
|
168
|
+
bin/test test/models/trackable_test.rb:16
|
169
|
+
```
|
81
170
|
|
82
171
|
## Starting with Rails?
|
83
172
|
|
84
|
-
If you are building your first Rails application, we recommend you
|
173
|
+
If you are building your first Rails application, we recommend you *do not* use Devise. Devise requires a good understanding of the Rails Framework. In such cases, we advise you to start a simple authentication system from scratch. Today, we have three resources that should help you get started:
|
85
174
|
|
86
|
-
* Michael Hartl's online book:
|
175
|
+
* Michael Hartl's online book: https://www.railstutorial.org/book/modeling_users
|
87
176
|
* Ryan Bates' Railscast: http://railscasts.com/episodes/250-authentication-from-scratch
|
177
|
+
* Codecademy's Ruby on Rails: Authentication and Authorization: https://www.codecademy.com/learn/rails-auth
|
88
178
|
|
89
|
-
Once you have solidified your understanding of Rails and authentication mechanisms, we assure you Devise will be very pleasant to work with. :
|
179
|
+
Once you have solidified your understanding of Rails and authentication mechanisms, we assure you Devise will be very pleasant to work with. :smiley:
|
90
180
|
|
91
181
|
## Getting started
|
92
182
|
|
93
|
-
Devise
|
183
|
+
Devise 4.0 works with Rails 4.1 onwards. Add the following line to your Gemfile:
|
94
184
|
|
95
185
|
```ruby
|
96
186
|
gem 'devise'
|
97
187
|
```
|
98
188
|
|
99
|
-
|
189
|
+
Then run `bundle install`
|
100
190
|
|
101
|
-
|
191
|
+
Next, you need to run the generator:
|
102
192
|
|
103
193
|
```console
|
104
|
-
rails generate devise:install
|
194
|
+
$ rails generate devise:install
|
105
195
|
```
|
106
196
|
|
107
|
-
|
197
|
+
At this point, a number of instructions will appear in the console. Among these instructions, you'll need to set up the default URL options for the Devise mailer in each environment. Here is a possible configuration for `config/environments/development.rb`:
|
108
198
|
|
109
|
-
```
|
110
|
-
|
199
|
+
```ruby
|
200
|
+
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
|
111
201
|
```
|
112
202
|
|
113
|
-
|
203
|
+
The generator will install an initializer which describes ALL of Devise's configuration options. It is *imperative* that you take a look at it. When you are done, you are ready to add Devise to any of your models using the generator.
|
114
204
|
|
115
|
-
Next, you need to set up the default url options for the Devise mailer in each environment. Here is a possible configuration for `config/environments/development.rb`:
|
116
205
|
|
117
|
-
|
118
|
-
|
206
|
+
In the following command you will replace `MODEL` with the class name used for the application’s users (it’s frequently `User` but could also be `Admin`). This will create a model (if one does not exist) and configure it with the default Devise modules. The generator also configures your `config/routes.rb` file to point to the Devise controller.
|
207
|
+
|
208
|
+
```console
|
209
|
+
$ rails generate devise MODEL
|
119
210
|
```
|
120
211
|
|
121
|
-
|
212
|
+
Next, check the MODEL for any additional configuration options you might want to add, such as confirmable or lockable. If you add an option, be sure to inspect the migration file (created by the generator if your ORM supports them) and uncomment the appropriate section. For example, if you add the confirmable option in the model, you'll need to uncomment the Confirmable section in the migration.
|
213
|
+
|
214
|
+
Then run `rails db:migrate`
|
215
|
+
|
216
|
+
You should restart your application after changing Devise's configuration options (this includes stopping spring). Otherwise, you will run into strange errors, for example, users being unable to login and route helpers being undefined.
|
122
217
|
|
123
218
|
### Controller filters and helpers
|
124
219
|
|
125
|
-
Devise will create some helpers to use inside your controllers and views. To set up a controller with user authentication, just add this
|
220
|
+
Devise will create some helpers to use inside your controllers and views. To set up a controller with user authentication, just add this before_action (assuming your devise model is 'User'):
|
126
221
|
|
127
222
|
```ruby
|
128
|
-
|
223
|
+
before_action :authenticate_user!
|
129
224
|
```
|
130
225
|
|
226
|
+
For Rails 5, note that `protect_from_forgery` is no longer prepended to the `before_action` chain, so if you have set `authenticate_user` before `protect_from_forgery`, your request will result in "Can't verify CSRF token authenticity." To resolve this, either change the order in which you call them, or use `protect_from_forgery prepend: true`.
|
227
|
+
|
228
|
+
If your devise model is something other than User, replace "_user" with "_yourmodel". The same logic applies to the instructions below.
|
229
|
+
|
131
230
|
To verify if a user is signed in, use the following helper:
|
132
231
|
|
133
232
|
```ruby
|
@@ -146,10 +245,10 @@ You can access the session for this scope:
|
|
146
245
|
user_session
|
147
246
|
```
|
148
247
|
|
149
|
-
After signing in a user, confirming the account or updating the password, Devise will look for a scoped root path to redirect. For instance,
|
248
|
+
After signing in a user, confirming the account or updating the password, Devise will look for a scoped root path to redirect to. For instance, when using a `:user` resource, the `user_root_path` will be used if it exists; otherwise, the default `root_path` will be used. This means that you need to set the root inside your routes:
|
150
249
|
|
151
250
|
```ruby
|
152
|
-
root to:
|
251
|
+
root to: 'home#index'
|
153
252
|
```
|
154
253
|
|
155
254
|
You can also override `after_sign_in_path_for` and `after_sign_out_path_for` to customize your redirect hooks.
|
@@ -157,7 +256,7 @@ You can also override `after_sign_in_path_for` and `after_sign_out_path_for` to
|
|
157
256
|
Notice that if your Devise model is called `Member` instead of `User`, for example, then the helpers available are:
|
158
257
|
|
159
258
|
```ruby
|
160
|
-
|
259
|
+
before_action :authenticate_member!
|
161
260
|
|
162
261
|
member_signed_in?
|
163
262
|
|
@@ -168,65 +267,88 @@ member_session
|
|
168
267
|
|
169
268
|
### Configuring Models
|
170
269
|
|
171
|
-
The Devise method in your models also accepts some options to configure its modules. For example, you can choose the cost of the
|
270
|
+
The Devise method in your models also accepts some options to configure its modules. For example, you can choose the cost of the hashing algorithm with:
|
172
271
|
|
173
272
|
```ruby
|
174
|
-
devise :database_authenticatable, :registerable, :confirmable, :recoverable, stretches:
|
273
|
+
devise :database_authenticatable, :registerable, :confirmable, :recoverable, stretches: 12
|
175
274
|
```
|
176
275
|
|
177
|
-
Besides `:stretches`, you can define `:pepper`, `:encryptor`, `:confirm_within`, `:remember_for`, `:timeout_in`, `:unlock_in` among other options. For more details, see the initializer file that was created when you invoked the "devise:install" generator described above.
|
276
|
+
Besides `:stretches`, you can define `:pepper`, `:encryptor`, `:confirm_within`, `:remember_for`, `:timeout_in`, `:unlock_in` among other options. For more details, see the initializer file that was created when you invoked the "devise:install" generator described above. This file is usually located at `/config/initializers/devise.rb`.
|
178
277
|
|
179
278
|
### Strong Parameters
|
180
279
|
|
280
|
+
![The Parameter Sanitizer API has changed for Devise 4](http://messages.hellobits.com/warning.svg?message=The%20Parameter%20Sanitizer%20API%20has%20changed%20for%20Devise%204)
|
281
|
+
|
282
|
+
*For previous Devise versions see https://github.com/plataformatec/devise/tree/3-stable#strong-parameters*
|
283
|
+
|
181
284
|
When you customize your own views, you may end up adding new attributes to forms. Rails 4 moved the parameter sanitization from the model to the controller, causing Devise to handle this concern at the controller as well.
|
182
285
|
|
183
|
-
There are just three actions in Devise that
|
286
|
+
There are just three actions in Devise that allow any set of parameters to be passed down to the model, therefore requiring sanitization. Their names and default permitted parameters are:
|
184
287
|
|
185
|
-
* `sign_in` (`Devise::SessionsController#
|
288
|
+
* `sign_in` (`Devise::SessionsController#create`) - Permits only the authentication keys (like `email`)
|
186
289
|
* `sign_up` (`Devise::RegistrationsController#create`) - Permits authentication keys plus `password` and `password_confirmation`
|
187
290
|
* `account_update` (`Devise::RegistrationsController#update`) - Permits authentication keys plus `password`, `password_confirmation` and `current_password`
|
188
291
|
|
189
|
-
In case you want to permit additional parameters (the lazy way™) you can do
|
292
|
+
In case you want to permit additional parameters (the lazy way™), you can do so using a simple before filter in your `ApplicationController`:
|
293
|
+
|
294
|
+
```ruby
|
295
|
+
class ApplicationController < ActionController::Base
|
296
|
+
before_action :configure_permitted_parameters, if: :devise_controller?
|
297
|
+
|
298
|
+
protected
|
299
|
+
|
300
|
+
def configure_permitted_parameters
|
301
|
+
devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
|
302
|
+
end
|
303
|
+
end
|
304
|
+
```
|
305
|
+
|
306
|
+
The above works for any additional fields where the parameters are simple scalar types. If you have nested attributes (say you're using `accepts_nested_attributes_for`), then you will need to tell devise about those nestings and types:
|
190
307
|
|
191
308
|
```ruby
|
192
309
|
class ApplicationController < ActionController::Base
|
193
|
-
|
310
|
+
before_action :configure_permitted_parameters, if: :devise_controller?
|
194
311
|
|
195
312
|
protected
|
196
313
|
|
197
314
|
def configure_permitted_parameters
|
198
|
-
devise_parameter_sanitizer.
|
315
|
+
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, address_attributes: [:country, :state, :city, :area, :postal_code]])
|
199
316
|
end
|
200
317
|
end
|
201
318
|
```
|
202
319
|
|
203
|
-
|
320
|
+
Devise allows you to completely change Devise defaults or invoke custom behaviour by passing a block:
|
204
321
|
|
205
322
|
To permit simple scalar values for username and email, use this
|
206
323
|
|
207
324
|
```ruby
|
208
325
|
def configure_permitted_parameters
|
209
|
-
devise_parameter_sanitizer.
|
326
|
+
devise_parameter_sanitizer.permit(:sign_in) do |user_params|
|
327
|
+
user_params.permit(:username, :email)
|
328
|
+
end
|
210
329
|
end
|
211
330
|
```
|
212
331
|
|
213
|
-
If you have some checkboxes that express the roles a user may take on registration, the browser will send those selected checkboxes as an array. An array is not one of Strong Parameters permitted scalars, so we need to configure Devise
|
332
|
+
If you have some checkboxes that express the roles a user may take on registration, the browser will send those selected checkboxes as an array. An array is not one of Strong Parameters' permitted scalars, so we need to configure Devise in the following way:
|
214
333
|
|
215
334
|
```ruby
|
216
335
|
def configure_permitted_parameters
|
217
|
-
devise_parameter_sanitizer.
|
336
|
+
devise_parameter_sanitizer.permit(:sign_up) do |user_params|
|
337
|
+
user_params.permit({ roles: [] }, :email, :password, :password_confirmation)
|
338
|
+
end
|
218
339
|
end
|
219
340
|
```
|
220
341
|
For the list of permitted scalars, and how to declare permitted keys in nested hashes and arrays, see
|
221
342
|
|
222
343
|
https://github.com/rails/strong_parameters#nested-parameters
|
223
344
|
|
224
|
-
If you have multiple Devise models, you may want to set up different parameter sanitizer per model. In this case, we recommend inheriting from `Devise::ParameterSanitizer` and
|
345
|
+
If you have multiple Devise models, you may want to set up a different parameter sanitizer per model. In this case, we recommend inheriting from `Devise::ParameterSanitizer` and adding your own logic:
|
225
346
|
|
226
347
|
```ruby
|
227
348
|
class User::ParameterSanitizer < Devise::ParameterSanitizer
|
228
|
-
def
|
229
|
-
|
349
|
+
def initialize(*)
|
350
|
+
super
|
351
|
+
permit(:sign_up, keys: [:username, :email])
|
230
352
|
end
|
231
353
|
end
|
232
354
|
```
|
@@ -256,7 +378,7 @@ We built Devise to help you quickly develop an application that uses authenticat
|
|
256
378
|
Since Devise is an engine, all its views are packaged inside the gem. These views will help you get started, but after some time you may want to change them. If this is the case, you just need to invoke the following generator, and it will copy all views to your application:
|
257
379
|
|
258
380
|
```console
|
259
|
-
rails generate devise:views
|
381
|
+
$ rails generate devise:views
|
260
382
|
```
|
261
383
|
|
262
384
|
If you have more than one Devise model in your application (such as `User` and `Admin`), you will notice that Devise uses the same views for all models. Fortunately, Devise offers an easy way to customize views. All you need to do is set `config.scoped_views = true` inside the `config/initializers/devise.rb` file.
|
@@ -264,43 +386,54 @@ If you have more than one Devise model in your application (such as `User` and `
|
|
264
386
|
After doing so, you will be able to have views based on the role like `users/sessions/new` and `admins/sessions/new`. If no view is found within the scope, Devise will use the default view at `devise/sessions/new`. You can also use the generator to generate scoped views:
|
265
387
|
|
266
388
|
```console
|
267
|
-
rails generate devise:views users
|
389
|
+
$ rails generate devise:views users
|
268
390
|
```
|
269
391
|
|
270
|
-
If you
|
392
|
+
If you would like to generate only a few sets of views, like the ones for the `registerable` and `confirmable` module,
|
271
393
|
you can pass a list of modules to the generator with the `-v` flag.
|
272
394
|
|
273
395
|
```console
|
274
|
-
rails generate devise:views -v registrations confirmations
|
396
|
+
$ rails generate devise:views -v registrations confirmations
|
275
397
|
```
|
276
398
|
|
277
399
|
### Configuring controllers
|
278
400
|
|
279
401
|
If the customization at the views level is not enough, you can customize each controller by following these steps:
|
280
402
|
|
281
|
-
1. Create your custom
|
403
|
+
1. Create your custom controllers using the generator which requires a scope:
|
404
|
+
|
405
|
+
```console
|
406
|
+
$ rails generate devise:controllers [scope]
|
407
|
+
```
|
408
|
+
|
409
|
+
If you specify `users` as the scope, controllers will be created in `app/controllers/users/`.
|
410
|
+
And the sessions controller will look like this:
|
282
411
|
|
283
412
|
```ruby
|
284
|
-
class
|
413
|
+
class Users::SessionsController < Devise::SessionsController
|
414
|
+
# GET /resource/sign_in
|
415
|
+
# def new
|
416
|
+
# super
|
417
|
+
# end
|
418
|
+
...
|
285
419
|
end
|
286
420
|
```
|
287
|
-
|
288
|
-
Note that in the above example, the controller needs to be created in the `app/controller/admins/` directory.
|
421
|
+
(Use the -c flag to specify a controller, for example: `rails generate devise:controllers users -c=sessions`)
|
289
422
|
|
290
423
|
2. Tell the router to use this controller:
|
291
424
|
|
292
425
|
```ruby
|
293
|
-
devise_for :
|
426
|
+
devise_for :users, controllers: { sessions: 'users/sessions' }
|
294
427
|
```
|
295
428
|
|
296
|
-
3. Copy the views from `devise/sessions` to `
|
429
|
+
3. Copy the views from `devise/sessions` to `users/sessions`. Since the controller was changed, it won't use the default views located in `devise/sessions`.
|
297
430
|
|
298
431
|
4. Finally, change or extend the desired controller actions.
|
299
432
|
|
300
433
|
You can completely override a controller action:
|
301
434
|
|
302
435
|
```ruby
|
303
|
-
class
|
436
|
+
class Users::SessionsController < Devise::SessionsController
|
304
437
|
def create
|
305
438
|
# custom sign-in code
|
306
439
|
end
|
@@ -310,7 +443,7 @@ If the customization at the views level is not enough, you can customize each co
|
|
310
443
|
Or you can simply add new behaviour to it:
|
311
444
|
|
312
445
|
```ruby
|
313
|
-
class
|
446
|
+
class Users::SessionsController < Devise::SessionsController
|
314
447
|
def create
|
315
448
|
super do |resource|
|
316
449
|
BackgroundWorker.trigger(resource)
|
@@ -321,31 +454,37 @@ If the customization at the views level is not enough, you can customize each co
|
|
321
454
|
|
322
455
|
This is useful for triggering background jobs or logging events during certain actions.
|
323
456
|
|
324
|
-
Remember that Devise uses flash messages to let users know if sign in was successful or
|
457
|
+
Remember that Devise uses flash messages to let users know if sign in was successful or unsuccessful. Devise expects your application to call `flash[:notice]` and `flash[:alert]` as appropriate. Do not print the entire flash hash, print only specific keys. In some circumstances, Devise adds a `:timedout` key to the flash hash, which is not meant for display. Remove this key from the hash if you intend to print the entire hash.
|
325
458
|
|
326
459
|
### Configuring routes
|
327
460
|
|
328
461
|
Devise also ships with default routes. If you need to customize them, you should probably be able to do it through the devise_for method. It accepts several options like :class_name, :path_prefix and so on, including the possibility to change path names for I18n:
|
329
462
|
|
330
463
|
```ruby
|
331
|
-
devise_for :users, path:
|
464
|
+
devise_for :users, path: 'auth', path_names: { sign_in: 'login', sign_out: 'logout', password: 'secret', confirmation: 'verification', unlock: 'unblock', registration: 'register', sign_up: 'cmon_let_me_in' }
|
332
465
|
```
|
333
466
|
|
334
|
-
Be sure to check `devise_for` documentation for details.
|
467
|
+
Be sure to check `devise_for` [documentation](http://www.rubydoc.info/github/plataformatec/devise/master/ActionDispatch/Routing/Mapper%3Adevise_for) for details.
|
335
468
|
|
336
|
-
If you have the need for more deep customization, for instance to also allow "/sign_in" besides "/users/sign_in", all you need to do is
|
469
|
+
If you have the need for more deep customization, for instance to also allow "/sign_in" besides "/users/sign_in", all you need to do is create your routes normally and wrap them in a `devise_scope` block in the router:
|
337
470
|
|
338
471
|
```ruby
|
339
472
|
devise_scope :user do
|
340
|
-
get
|
473
|
+
get 'sign_in', to: 'devise/sessions#new'
|
341
474
|
end
|
342
475
|
```
|
343
476
|
|
344
|
-
This way you tell Devise to use the scope `:user` when "/sign_in" is accessed. Notice `devise_scope` is also aliased as `as` in your router.
|
477
|
+
This way, you tell Devise to use the scope `:user` when "/sign_in" is accessed. Notice `devise_scope` is also aliased as `as` in your router.
|
478
|
+
|
479
|
+
Please note: You will still need to add `devise_for` in your routes in order to use helper methods such as `current_user`.
|
480
|
+
|
481
|
+
```ruby
|
482
|
+
devise_for :users, skip: :all
|
483
|
+
```
|
345
484
|
|
346
485
|
### I18n
|
347
486
|
|
348
|
-
Devise uses flash messages with I18n with the flash keys :notice and :alert. To customize your app, you can set up your locale file:
|
487
|
+
Devise uses flash messages with I18n, in conjunction with the flash keys :notice and :alert. To customize your app, you can set up your locale file:
|
349
488
|
|
350
489
|
```yaml
|
351
490
|
en:
|
@@ -383,54 +522,115 @@ Take a look at our locale file to check all available messages. You may also be
|
|
383
522
|
|
384
523
|
https://github.com/plataformatec/devise/wiki/I18n
|
385
524
|
|
525
|
+
Caution: Devise Controllers inherit from ApplicationController. If your app uses multiple locales, you should be sure to set I18n.locale in ApplicationController.
|
526
|
+
|
386
527
|
### Test helpers
|
387
528
|
|
388
|
-
Devise includes some test helpers for
|
529
|
+
Devise includes some test helpers for controller and integration tests.
|
530
|
+
In order to use them, you need to include the respective module in your test
|
531
|
+
cases/specs.
|
532
|
+
|
533
|
+
### Controller tests
|
534
|
+
|
535
|
+
Controller tests require that you include `Devise::Test::ControllerHelpers` on
|
536
|
+
your test case or its parent `ActionController::TestCase` superclass.
|
537
|
+
For Rails 5, include `Devise::Test::IntegrationHelpers` instead, since the superclass
|
538
|
+
for controller tests has been changed to ActionDispatch::IntegrationTest
|
539
|
+
(for more details, see the [Integration tests](#integration-tests) section).
|
389
540
|
|
390
541
|
```ruby
|
391
|
-
class ActionController::TestCase
|
392
|
-
include Devise::
|
542
|
+
class PostsControllerTest < ActionController::TestCase
|
543
|
+
include Devise::Test::ControllerHelpers
|
393
544
|
end
|
394
545
|
```
|
395
546
|
|
396
|
-
If you're using RSpec, you can put the following inside a file named
|
547
|
+
If you're using RSpec, you can put the following inside a file named
|
548
|
+
`spec/support/devise.rb` or in your `spec/spec_helper.rb` (or
|
549
|
+
`spec/rails_helper.rb` if you are using `rspec-rails`):
|
397
550
|
|
398
551
|
```ruby
|
399
552
|
RSpec.configure do |config|
|
400
|
-
config.include Devise::
|
553
|
+
config.include Devise::Test::ControllerHelpers, type: :controller
|
554
|
+
config.include Devise::Test::ControllerHelpers, type: :view
|
401
555
|
end
|
402
556
|
```
|
403
557
|
|
404
|
-
|
558
|
+
Just be sure that this inclusion is made *after* the `require 'rspec/rails'` directive.
|
559
|
+
|
560
|
+
Now you are ready to use the `sign_in` and `sign_out` methods on your controller
|
561
|
+
tests:
|
405
562
|
|
406
563
|
```ruby
|
407
|
-
sign_in
|
408
|
-
sign_in @user
|
564
|
+
sign_in @user
|
565
|
+
sign_in @user, scope: :admin
|
566
|
+
```
|
567
|
+
|
568
|
+
If you are testing Devise internal controllers or a controller that inherits
|
569
|
+
from Devise's, you need to tell Devise which mapping should be used before a
|
570
|
+
request. This is necessary because Devise gets this information from the router,
|
571
|
+
but since controller tests do not pass through the router, it needs to be stated
|
572
|
+
explicitly. For example, if you are testing the user scope, simply use:
|
409
573
|
|
410
|
-
|
411
|
-
|
574
|
+
```ruby
|
575
|
+
test 'GET new' do
|
576
|
+
# Mimic the router behavior of setting the Devise scope through the env.
|
577
|
+
@request.env['devise.mapping'] = Devise.mappings[:user]
|
578
|
+
|
579
|
+
# Use the sign_in helper to sign in a fixture `User` record.
|
580
|
+
sign_in users(:alice)
|
581
|
+
|
582
|
+
get :new
|
583
|
+
|
584
|
+
# assert something
|
585
|
+
end
|
412
586
|
```
|
413
587
|
|
414
|
-
|
588
|
+
### Integration tests
|
415
589
|
|
416
|
-
|
590
|
+
Integration test helpers are available by including the
|
591
|
+
`Devise::Test::IntegrationHelpers` module.
|
417
592
|
|
418
|
-
|
593
|
+
```ruby
|
594
|
+
class PostsTests < ActionDispatch::IntegrationTest
|
595
|
+
include Devise::Test::IntegrationHelpers
|
596
|
+
end
|
597
|
+
```
|
419
598
|
|
420
|
-
|
421
|
-
|
422
|
-
get :new
|
423
|
-
```
|
599
|
+
Now you can use the following `sign_in` and `sign_out` methods in your integration
|
600
|
+
tests:
|
424
601
|
|
425
|
-
|
602
|
+
```ruby
|
603
|
+
sign_in users(:bob)
|
604
|
+
sign_in users(:bob), scope: :admin
|
605
|
+
|
606
|
+
sign_out :user
|
607
|
+
```
|
426
608
|
|
427
|
-
|
609
|
+
RSpec users can include the `IntegrationHelpers` module on their `:feature` specs.
|
610
|
+
|
611
|
+
```ruby
|
612
|
+
RSpec.configure do |config|
|
613
|
+
config.include Devise::Test::IntegrationHelpers, type: :feature
|
614
|
+
end
|
615
|
+
```
|
616
|
+
|
617
|
+
Unlike controller tests, integration tests do not need to supply the
|
618
|
+
`devise.mapping` `env` value, as the mapping can be inferred by the routes that
|
619
|
+
are executed in your tests.
|
620
|
+
|
621
|
+
You can read more about testing your Rails 3 - Rails 4 controllers with RSpec in the wiki:
|
622
|
+
|
623
|
+
* https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-(and-RSpec)
|
624
|
+
|
625
|
+
### OmniAuth
|
626
|
+
|
627
|
+
Devise comes with OmniAuth support out of the box to authenticate with other providers. To use it, simply specify your OmniAuth configuration in `config/initializers/devise.rb`:
|
428
628
|
|
429
629
|
```ruby
|
430
630
|
config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
|
431
631
|
```
|
432
632
|
|
433
|
-
You can read more about
|
633
|
+
You can read more about OmniAuth support in the wiki:
|
434
634
|
|
435
635
|
* https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
|
436
636
|
|
@@ -443,7 +643,7 @@ Devise allows you to set up as many Devise models as you want. If you want to ha
|
|
443
643
|
create_table :admins do |t|
|
444
644
|
t.string :email
|
445
645
|
t.string :encrypted_password
|
446
|
-
t.timestamps
|
646
|
+
t.timestamps null: false
|
447
647
|
end
|
448
648
|
|
449
649
|
# Inside your Admin model
|
@@ -453,7 +653,7 @@ devise :database_authenticatable, :timeoutable
|
|
453
653
|
devise_for :admins
|
454
654
|
|
455
655
|
# Inside your protected controller
|
456
|
-
|
656
|
+
before_action :authenticate_admin!
|
457
657
|
|
458
658
|
# Inside your controllers and views
|
459
659
|
admin_signed_in?
|
@@ -463,17 +663,54 @@ admin_session
|
|
463
663
|
|
464
664
|
Alternatively, you can simply run the Devise generator.
|
465
665
|
|
466
|
-
Keep in mind that those models will have completely different routes. They **do not** and **cannot** share the same controller for sign in, sign out and so on. In case you want to have different roles sharing the same actions, we recommend you
|
666
|
+
Keep in mind that those models will have completely different routes. They **do not** and **cannot** share the same controller for sign in, sign out and so on. In case you want to have different roles sharing the same actions, we recommend that you use a role-based approach, by either providing a role column or using a dedicated gem for authorization.
|
667
|
+
|
668
|
+
### ActiveJob Integration
|
669
|
+
|
670
|
+
If you are using Rails 4.2 and ActiveJob to deliver ActionMailer messages in the
|
671
|
+
background through a queuing back-end, you can send Devise emails through your
|
672
|
+
existing queue by overriding the `send_devise_notification` method in your model.
|
673
|
+
|
674
|
+
```ruby
|
675
|
+
def send_devise_notification(notification, *args)
|
676
|
+
devise_mailer.send(notification, self, *args).deliver_later
|
677
|
+
end
|
678
|
+
```
|
679
|
+
|
680
|
+
### Password reset tokens and Rails logs
|
681
|
+
|
682
|
+
If you enable the [Recoverable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Recoverable) module, note that a stolen password reset token could give an attacker access to your application. Devise takes effort to generate random, secure tokens, and stores only token digests in the database, never plaintext. However the default logging behavior in Rails can cause plaintext tokens to leak into log files:
|
683
|
+
|
684
|
+
1. Action Mailer logs the entire contents of all outgoing emails to the DEBUG level. Password reset tokens delivered to users in email will be leaked.
|
685
|
+
2. Active Job logs all arguments to every enqueued job at the INFO level. If you configure Devise to use `deliver_later` to send password reset emails, password reset tokens will be leaked.
|
686
|
+
|
687
|
+
Rails sets the production logger level to DEBUG by default. Consider changing your production logger level to WARN if you wish to prevent tokens from being leaked into your logs. In `config/environments/production.rb`:
|
688
|
+
|
689
|
+
```ruby
|
690
|
+
config.log_level = :warn
|
691
|
+
```
|
692
|
+
|
467
693
|
|
468
694
|
### Other ORMs
|
469
695
|
|
470
|
-
Devise supports ActiveRecord (default) and Mongoid. To
|
696
|
+
Devise supports ActiveRecord (default) and Mongoid. To select another ORM, simply require it in the initializer file.
|
697
|
+
|
698
|
+
### Rails API Mode
|
699
|
+
|
700
|
+
Rails 5+ has a built-in [API Mode](https://edgeguides.rubyonrails.org/api_app.html) which optimizes Rails for use as an API (only). One of the side effects is that it changes the order of the middleware stack, and this can cause problems for `Devise::Test::IntegrationHelpers`. This problem usually surfaces as an ```undefined method `[]=' for nil:NilClass``` error when using integration test helpers, such as `#sign_in`. The solution is simply to reorder the middlewares by adding the following to test.rb:
|
701
|
+
|
702
|
+
```ruby
|
703
|
+
Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Cookies
|
704
|
+
Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Session::CookieStore
|
705
|
+
```
|
706
|
+
|
707
|
+
For a deeper understanding of this, review [this issue](https://github.com/plataformatec/devise/issues/4696).
|
471
708
|
|
472
709
|
## Additional information
|
473
710
|
|
474
711
|
### Heroku
|
475
712
|
|
476
|
-
Using Devise on Heroku with Ruby on Rails 3.
|
713
|
+
Using Devise on Heroku with Ruby on Rails 3.2 requires setting:
|
477
714
|
|
478
715
|
```ruby
|
479
716
|
config.assets.initialize_on_precompile = false
|
@@ -495,6 +732,6 @@ https://github.com/plataformatec/devise/graphs/contributors
|
|
495
732
|
|
496
733
|
## License
|
497
734
|
|
498
|
-
MIT License. Copyright 2009-
|
735
|
+
MIT License. Copyright 2009-2019 Plataformatec. http://plataformatec.com.br
|
499
736
|
|
500
|
-
You are not granted rights or licenses to the trademarks of
|
737
|
+
You are not granted rights or licenses to the trademarks of Plataformatec, including without limitation the Devise name or logo.
|