devise 3.2.2 → 4.6.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise might be problematic. Click here for more details.

Files changed (235) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +242 -978
  3. data/MIT-LICENSE +1 -1
  4. data/README.md +371 -100
  5. data/app/controllers/devise/confirmations_controller.rb +11 -5
  6. data/app/controllers/devise/omniauth_callbacks_controller.rb +12 -6
  7. data/app/controllers/devise/passwords_controller.rb +21 -8
  8. data/app/controllers/devise/registrations_controller.rb +59 -26
  9. data/app/controllers/devise/sessions_controller.rb +47 -17
  10. data/app/controllers/devise/unlocks_controller.rb +9 -4
  11. data/app/controllers/devise_controller.rb +67 -31
  12. data/app/helpers/devise_helper.rb +12 -19
  13. data/app/mailers/devise/mailer.rb +10 -0
  14. data/app/views/devise/confirmations/new.html.erb +9 -5
  15. data/app/views/devise/mailer/confirmation_instructions.html.erb +1 -1
  16. data/app/views/devise/mailer/email_changed.html.erb +7 -0
  17. data/app/views/devise/mailer/password_change.html.erb +3 -0
  18. data/app/views/devise/mailer/reset_password_instructions.html.erb +1 -1
  19. data/app/views/devise/mailer/unlock_instructions.html.erb +1 -1
  20. data/app/views/devise/passwords/edit.html.erb +16 -7
  21. data/app/views/devise/passwords/new.html.erb +9 -5
  22. data/app/views/devise/registrations/edit.html.erb +29 -15
  23. data/app/views/devise/registrations/new.html.erb +20 -9
  24. data/app/views/devise/sessions/new.html.erb +19 -10
  25. data/app/views/devise/shared/_error_messages.html.erb +15 -0
  26. data/app/views/devise/shared/{_links.erb → _links.html.erb} +9 -9
  27. data/app/views/devise/unlocks/new.html.erb +9 -5
  28. data/config/locales/en.yml +23 -17
  29. data/lib/devise/controllers/helpers.rb +112 -32
  30. data/lib/devise/controllers/rememberable.rb +15 -6
  31. data/lib/devise/controllers/scoped_views.rb +3 -1
  32. data/lib/devise/controllers/sign_in_out.rb +42 -26
  33. data/lib/devise/controllers/store_location.rb +31 -5
  34. data/lib/devise/controllers/url_helpers.rb +9 -7
  35. data/lib/devise/delegator.rb +2 -0
  36. data/lib/devise/encryptor.rb +24 -0
  37. data/lib/devise/failure_app.rb +125 -39
  38. data/lib/devise/hooks/activatable.rb +7 -6
  39. data/lib/devise/hooks/csrf_cleaner.rb +5 -1
  40. data/lib/devise/hooks/forgetable.rb +2 -0
  41. data/lib/devise/hooks/lockable.rb +7 -2
  42. data/lib/devise/hooks/proxy.rb +4 -2
  43. data/lib/devise/hooks/rememberable.rb +4 -2
  44. data/lib/devise/hooks/timeoutable.rb +16 -9
  45. data/lib/devise/hooks/trackable.rb +3 -1
  46. data/lib/devise/mailers/helpers.rb +15 -12
  47. data/lib/devise/mapping.rb +8 -2
  48. data/lib/devise/models/authenticatable.rb +82 -56
  49. data/lib/devise/models/confirmable.rb +125 -42
  50. data/lib/devise/models/database_authenticatable.rb +110 -32
  51. data/lib/devise/models/lockable.rb +30 -17
  52. data/lib/devise/models/omniauthable.rb +3 -1
  53. data/lib/devise/models/recoverable.rb +62 -26
  54. data/lib/devise/models/registerable.rb +4 -0
  55. data/lib/devise/models/rememberable.rb +62 -33
  56. data/lib/devise/models/timeoutable.rb +4 -8
  57. data/lib/devise/models/trackable.rb +20 -4
  58. data/lib/devise/models/validatable.rb +16 -9
  59. data/lib/devise/models.rb +3 -1
  60. data/lib/devise/modules.rb +12 -10
  61. data/lib/devise/omniauth/config.rb +2 -0
  62. data/lib/devise/omniauth/url_helpers.rb +14 -5
  63. data/lib/devise/omniauth.rb +2 -0
  64. data/lib/devise/orm/active_record.rb +5 -1
  65. data/lib/devise/orm/mongoid.rb +6 -2
  66. data/lib/devise/parameter_filter.rb +4 -0
  67. data/lib/devise/parameter_sanitizer.rb +139 -65
  68. data/lib/devise/rails/routes.rb +147 -116
  69. data/lib/devise/rails/warden_compat.rb +3 -10
  70. data/lib/devise/rails.rb +10 -13
  71. data/lib/devise/secret_key_finder.rb +27 -0
  72. data/lib/devise/strategies/authenticatable.rb +20 -9
  73. data/lib/devise/strategies/base.rb +3 -1
  74. data/lib/devise/strategies/database_authenticatable.rb +14 -6
  75. data/lib/devise/strategies/rememberable.rb +15 -3
  76. data/lib/devise/test/controller_helpers.rb +165 -0
  77. data/lib/devise/test/integration_helpers.rb +63 -0
  78. data/lib/devise/test_helpers.rb +7 -124
  79. data/lib/devise/time_inflector.rb +4 -2
  80. data/lib/devise/token_generator.rb +3 -41
  81. data/lib/devise/version.rb +3 -1
  82. data/lib/devise.rb +111 -84
  83. data/lib/generators/active_record/devise_generator.rb +49 -12
  84. data/lib/generators/active_record/templates/migration.rb +9 -7
  85. data/lib/generators/active_record/templates/migration_existing.rb +9 -7
  86. data/lib/generators/devise/controllers_generator.rb +46 -0
  87. data/lib/generators/devise/devise_generator.rb +7 -5
  88. data/lib/generators/devise/install_generator.rb +21 -0
  89. data/lib/generators/devise/orm_helpers.rb +10 -21
  90. data/lib/generators/devise/views_generator.rb +49 -28
  91. data/lib/generators/mongoid/devise_generator.rb +21 -19
  92. data/lib/generators/templates/README +5 -12
  93. data/lib/generators/templates/controllers/README +14 -0
  94. data/lib/generators/templates/controllers/confirmations_controller.rb +30 -0
  95. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +30 -0
  96. data/lib/generators/templates/controllers/passwords_controller.rb +34 -0
  97. data/lib/generators/templates/controllers/registrations_controller.rb +62 -0
  98. data/lib/generators/templates/controllers/sessions_controller.rb +27 -0
  99. data/lib/generators/templates/controllers/unlocks_controller.rb +30 -0
  100. data/lib/generators/templates/devise.rb +81 -36
  101. data/lib/generators/templates/markerb/confirmation_instructions.markerb +1 -1
  102. data/lib/generators/templates/markerb/email_changed.markerb +7 -0
  103. data/lib/generators/templates/markerb/password_change.markerb +3 -0
  104. data/lib/generators/templates/markerb/reset_password_instructions.markerb +1 -1
  105. data/lib/generators/templates/markerb/unlock_instructions.markerb +1 -1
  106. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +6 -2
  107. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +9 -4
  108. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +5 -2
  109. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +14 -6
  110. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +12 -4
  111. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +11 -6
  112. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +5 -2
  113. metadata +52 -280
  114. data/.gitignore +0 -10
  115. data/.travis.yml +0 -20
  116. data/.yardopts +0 -9
  117. data/CONTRIBUTING.md +0 -14
  118. data/Gemfile +0 -31
  119. data/Gemfile.lock +0 -160
  120. data/Rakefile +0 -35
  121. data/devise.gemspec +0 -27
  122. data/devise.png +0 -0
  123. data/gemfiles/Gemfile.rails-3.2.x +0 -31
  124. data/gemfiles/Gemfile.rails-3.2.x.lock +0 -159
  125. data/test/controllers/custom_strategy_test.rb +0 -62
  126. data/test/controllers/helpers_test.rb +0 -276
  127. data/test/controllers/internal_helpers_test.rb +0 -120
  128. data/test/controllers/passwords_controller_test.rb +0 -31
  129. data/test/controllers/sessions_controller_test.rb +0 -99
  130. data/test/controllers/url_helpers_test.rb +0 -59
  131. data/test/delegator_test.rb +0 -19
  132. data/test/devise_test.rb +0 -94
  133. data/test/failure_app_test.rb +0 -232
  134. data/test/generators/active_record_generator_test.rb +0 -103
  135. data/test/generators/devise_generator_test.rb +0 -39
  136. data/test/generators/install_generator_test.rb +0 -13
  137. data/test/generators/mongoid_generator_test.rb +0 -23
  138. data/test/generators/views_generator_test.rb +0 -67
  139. data/test/helpers/devise_helper_test.rb +0 -51
  140. data/test/integration/authenticatable_test.rb +0 -713
  141. data/test/integration/confirmable_test.rb +0 -284
  142. data/test/integration/database_authenticatable_test.rb +0 -84
  143. data/test/integration/http_authenticatable_test.rb +0 -105
  144. data/test/integration/lockable_test.rb +0 -239
  145. data/test/integration/omniauthable_test.rb +0 -133
  146. data/test/integration/recoverable_test.rb +0 -334
  147. data/test/integration/registerable_test.rb +0 -349
  148. data/test/integration/rememberable_test.rb +0 -167
  149. data/test/integration/timeoutable_test.rb +0 -183
  150. data/test/integration/trackable_test.rb +0 -92
  151. data/test/mailers/confirmation_instructions_test.rb +0 -115
  152. data/test/mailers/reset_password_instructions_test.rb +0 -96
  153. data/test/mailers/unlock_instructions_test.rb +0 -91
  154. data/test/mapping_test.rb +0 -127
  155. data/test/models/authenticatable_test.rb +0 -13
  156. data/test/models/confirmable_test.rb +0 -454
  157. data/test/models/database_authenticatable_test.rb +0 -249
  158. data/test/models/lockable_test.rb +0 -298
  159. data/test/models/omniauthable_test.rb +0 -7
  160. data/test/models/recoverable_test.rb +0 -184
  161. data/test/models/registerable_test.rb +0 -7
  162. data/test/models/rememberable_test.rb +0 -183
  163. data/test/models/serializable_test.rb +0 -49
  164. data/test/models/timeoutable_test.rb +0 -51
  165. data/test/models/trackable_test.rb +0 -13
  166. data/test/models/validatable_test.rb +0 -127
  167. data/test/models_test.rb +0 -144
  168. data/test/omniauth/config_test.rb +0 -57
  169. data/test/omniauth/url_helpers_test.rb +0 -54
  170. data/test/orm/active_record.rb +0 -10
  171. data/test/orm/mongoid.rb +0 -13
  172. data/test/parameter_sanitizer_test.rb +0 -81
  173. data/test/rails_app/Rakefile +0 -6
  174. data/test/rails_app/app/active_record/admin.rb +0 -6
  175. data/test/rails_app/app/active_record/shim.rb +0 -2
  176. data/test/rails_app/app/active_record/user.rb +0 -6
  177. data/test/rails_app/app/controllers/admins/sessions_controller.rb +0 -6
  178. data/test/rails_app/app/controllers/admins_controller.rb +0 -11
  179. data/test/rails_app/app/controllers/application_controller.rb +0 -9
  180. data/test/rails_app/app/controllers/home_controller.rb +0 -25
  181. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +0 -2
  182. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +0 -2
  183. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +0 -14
  184. data/test/rails_app/app/controllers/users_controller.rb +0 -31
  185. data/test/rails_app/app/helpers/application_helper.rb +0 -3
  186. data/test/rails_app/app/mailers/users/mailer.rb +0 -12
  187. data/test/rails_app/app/mongoid/admin.rb +0 -29
  188. data/test/rails_app/app/mongoid/shim.rb +0 -23
  189. data/test/rails_app/app/mongoid/user.rb +0 -39
  190. data/test/rails_app/app/views/admins/index.html.erb +0 -1
  191. data/test/rails_app/app/views/admins/sessions/new.html.erb +0 -2
  192. data/test/rails_app/app/views/home/admin_dashboard.html.erb +0 -1
  193. data/test/rails_app/app/views/home/index.html.erb +0 -1
  194. data/test/rails_app/app/views/home/join.html.erb +0 -1
  195. data/test/rails_app/app/views/home/private.html.erb +0 -1
  196. data/test/rails_app/app/views/home/user_dashboard.html.erb +0 -1
  197. data/test/rails_app/app/views/layouts/application.html.erb +0 -24
  198. data/test/rails_app/app/views/users/edit_form.html.erb +0 -1
  199. data/test/rails_app/app/views/users/index.html.erb +0 -1
  200. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +0 -1
  201. data/test/rails_app/app/views/users/sessions/new.html.erb +0 -1
  202. data/test/rails_app/bin/bundle +0 -3
  203. data/test/rails_app/bin/rails +0 -4
  204. data/test/rails_app/bin/rake +0 -4
  205. data/test/rails_app/config/application.rb +0 -40
  206. data/test/rails_app/config/boot.rb +0 -14
  207. data/test/rails_app/config/database.yml +0 -18
  208. data/test/rails_app/config/environment.rb +0 -5
  209. data/test/rails_app/config/environments/development.rb +0 -30
  210. data/test/rails_app/config/environments/production.rb +0 -80
  211. data/test/rails_app/config/environments/test.rb +0 -36
  212. data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  213. data/test/rails_app/config/initializers/devise.rb +0 -181
  214. data/test/rails_app/config/initializers/inflections.rb +0 -2
  215. data/test/rails_app/config/initializers/secret_token.rb +0 -8
  216. data/test/rails_app/config/initializers/session_store.rb +0 -1
  217. data/test/rails_app/config/routes.rb +0 -104
  218. data/test/rails_app/config.ru +0 -4
  219. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -71
  220. data/test/rails_app/db/schema.rb +0 -55
  221. data/test/rails_app/lib/shared_admin.rb +0 -17
  222. data/test/rails_app/lib/shared_user.rb +0 -29
  223. data/test/rails_app/public/404.html +0 -26
  224. data/test/rails_app/public/422.html +0 -26
  225. data/test/rails_app/public/500.html +0 -26
  226. data/test/rails_app/public/favicon.ico +0 -0
  227. data/test/routes_test.rb +0 -250
  228. data/test/support/assertions.rb +0 -40
  229. data/test/support/helpers.rb +0 -70
  230. data/test/support/integration.rb +0 -92
  231. data/test/support/locale/en.yml +0 -8
  232. data/test/support/webrat/integrations/rails.rb +0 -24
  233. data/test/test_helper.rb +0 -27
  234. data/test/test_helpers_test.rb +0 -173
  235. 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.png?branch=master)](http://travis-ci.org/plataformatec/devise)
6
- [![Code Climate](https://codeclimate.com/github/plataformatec/devise.png)](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,59 @@ 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 just what you really need.
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): encrypts 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://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Omniauthable): adds Omniauth (https://github.com/intridea/omniauth) support;
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 no activity in a specified period of time.
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
+ - [Additional information](#additional-information)
60
+ - [Heroku](#heroku)
61
+ - [Warden](#warden)
62
+ - [Contributors](#contributors)
63
+ - [License](#license)
64
+
65
+ <!-- /TOC -->
66
+
29
67
 
30
- Devise is guaranteed to be thread-safe on YARV. Thread-safety support on JRuby is on progress.
31
68
 
32
69
  ## Information
33
70
 
@@ -43,11 +80,15 @@ If you discover a problem with Devise, we would like to know about it. However,
43
80
 
44
81
  https://github.com/plataformatec/devise/wiki/Bug-reports
45
82
 
46
- If you found a security bug, do *NOT* use the GitHub issue tracker. Send an email to the maintainers listed at the bottom of the README.
83
+ If you have discovered a security related bug, please do *NOT* use the GitHub issue tracker. Send an email to opensource@plataformatec.com.br.
84
+
85
+ ### StackOverflow and Mailing List
47
86
 
48
- ### Mailing list
87
+ If you have any questions, comments, or concerns, please use StackOverflow instead of the GitHub issue tracker:
49
88
 
50
- If you have any questions, comments, or concerns, please use the Google Group instead of the GitHub issue tracker:
89
+ http://stackoverflow.com/questions/tagged/devise
90
+
91
+ The deprecated mailing list can still be read on
51
92
 
52
93
  https://groups.google.com/group/plataformatec-devise
53
94
 
@@ -77,51 +118,114 @@ We hope that you will consider contributing to Devise. Please read this short ov
77
118
 
78
119
  https://github.com/plataformatec/devise/wiki/Contributing
79
120
 
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 "bundle install" and "rake". For the tests to pass, you will need to have a MongoDB server (version 2.0 or newer) running on your system.
121
+ 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`.
122
+ 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`.
123
+
124
+ ### DEVISE_ORM
125
+ Since Devise support both Mongoid and ActiveRecord, we rely on this variable to run specific code for each ORM.
126
+ The default value of `DEVISE_ORM` is `active_record`. To run the tests for mongoid, you can pass `mongoid`:
127
+ ```
128
+ DEVISE_ORM=mongoid bin/test
129
+
130
+ ==> Devise.orm = :mongoid
131
+ ```
132
+ When running the tests for Mongoid, you will need to have a MongoDB server (version 2.0 or newer) running on your system.
133
+
134
+ Please note that the command output will show the variable value being used.
135
+
136
+ ### BUNDLE_GEMFILE
137
+ We can use this variable to tell bundler what Gemfile it should use (instead of the one in the current directory).
138
+ 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.
139
+ For example, if the tests broke using Ruby 2.4.2 and Rails 4.1, you can do the following:
140
+ ```bash
141
+ rbenv shell 2.4.2 # or rvm use 2.4.2
142
+ BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable bundle install
143
+ BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable bin/test
144
+ ```
145
+
146
+ You can also combine both of them if the tests broke for Mongoid:
147
+ ```bash
148
+ BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable bundle install
149
+ BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable DEVISE_ORM=mongoid bin/test
150
+ ```
151
+
152
+ ### Running tests
153
+ Devise uses [Mini Test](https://github.com/seattlerb/minitest) as test framework.
154
+
155
+ * Running all tests:
156
+ ```bash
157
+ bin/test
158
+ ```
159
+
160
+ * Running tests for an specific file:
161
+ ```bash
162
+ bin/test test/models/trackable_test.rb
163
+ ```
164
+
165
+ * Running a specific test given a regex:
166
+ ```bash
167
+ bin/test test/models/trackable_test.rb:16
168
+ ```
81
169
 
82
170
  ## Starting with Rails?
83
171
 
84
- If you are building your first Rails application, we recommend you to *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 two resources:
172
+ 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
173
 
86
- * Michael Hartl's online book: http://railstutorial.org/chapters/modeling-and-viewing-users-two#top
174
+ * Michael Hartl's online book: https://www.railstutorial.org/book/modeling_users
87
175
  * Ryan Bates' Railscast: http://railscasts.com/episodes/250-authentication-from-scratch
176
+ * Codecademy's Ruby on Rails: Authentication and Authorization: http://www.codecademy.com/en/learn/rails-auth
88
177
 
89
- Once you have solidified your understanding of Rails and authentication mechanisms, we assure you Devise will be very pleasant to work with. :)
178
+ Once you have solidified your understanding of Rails and authentication mechanisms, we assure you Devise will be very pleasant to work with. :smiley:
90
179
 
91
180
  ## Getting started
92
181
 
93
- Devise 3.0 works with Rails 3.2 onwards. You can add it to your Gemfile with:
182
+ Devise 4.0 works with Rails 4.1 onwards. Add the following line to your Gemfile:
94
183
 
95
184
  ```ruby
96
185
  gem 'devise'
97
186
  ```
98
187
 
99
- Run the bundle command to install it.
188
+ Then run `bundle install`
100
189
 
101
- After you install Devise and add it to your Gemfile, you need to run the generator:
190
+ Next, you need to run the generator:
102
191
 
103
192
  ```console
104
- rails generate devise:install
193
+ $ rails generate devise:install
194
+ ```
195
+
196
+ 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`:
197
+
198
+ ```ruby
199
+ config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
105
200
  ```
106
201
 
107
- The generator will install an initializer which describes ALL Devise's configuration options and you MUST take a look at it. When you are done, you are ready to add Devise to any of your models using the generator:
202
+ 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.
203
+
204
+
205
+ 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.
108
206
 
109
207
  ```console
110
- rails generate devise MODEL
208
+ $ rails generate devise MODEL
111
209
  ```
112
210
 
113
- Replace MODEL by the class name used for the applications users, it's frequently `User` but could also be `Admin`. This will create a model (if one does not exist) and configure it with default Devise modules. Next, you'll usually run `rake db:migrate` as the generator will have created a migration file (if your ORM supports them). This generator also configures your config/routes.rb file to point to the Devise controller.
211
+ 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.
114
212
 
115
- Note that you should re-start your app here if you've already started it. Otherwise you'll run into strange errors like users being unable to login and the route helpers being undefined.
213
+ Then run `rails db:migrate`
214
+
215
+ 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.
116
216
 
117
217
  ### Controller filters and helpers
118
218
 
119
- Devise will create some helpers to use inside your controllers and views. To set up a controller with user authentication, just add this before_filter:
219
+ 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'):
120
220
 
121
221
  ```ruby
122
- before_filter :authenticate_user!
222
+ before_action :authenticate_user!
123
223
  ```
124
224
 
225
+ 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`.
226
+
227
+ If your devise model is something other than User, replace "_user" with "_yourmodel". The same logic applies to the instructions below.
228
+
125
229
  To verify if a user is signed in, use the following helper:
126
230
 
127
231
  ```ruby
@@ -140,24 +244,18 @@ You can access the session for this scope:
140
244
  user_session
141
245
  ```
142
246
 
143
- After signing in a user, confirming the account or updating the password, Devise will look for a scoped root path to redirect. Example: For a :user resource, it will use `user_root_path` if it exists, otherwise default `root_path` will be used. This means that you need to set the root inside your routes:
247
+ 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:
144
248
 
145
249
  ```ruby
146
- root to: "home#index"
250
+ root to: 'home#index'
147
251
  ```
148
252
 
149
- You can also overwrite `after_sign_in_path_for` and `after_sign_out_path_for` to customize your redirect hooks.
150
-
151
- Finally, you need to set up default url options for the mailer in each environment. Here is the configuration for "config/environments/development.rb":
152
-
153
- ```ruby
154
- config.action_mailer.default_url_options = { :host => 'localhost:3000' }
155
- ```
253
+ You can also override `after_sign_in_path_for` and `after_sign_out_path_for` to customize your redirect hooks.
156
254
 
157
- Notice that if your devise model is not called "user" but "member", then the helpers you should use are:
255
+ Notice that if your Devise model is called `Member` instead of `User`, for example, then the helpers available are:
158
256
 
159
257
  ```ruby
160
- before_filter :authenticate_member!
258
+ before_action :authenticate_member!
161
259
 
162
260
  member_signed_in?
163
261
 
@@ -168,52 +266,88 @@ member_session
168
266
 
169
267
  ### Configuring Models
170
268
 
171
- The devise method in your models also accepts some options to configure its modules. For example, you can choose the cost of the encryption algorithm with:
269
+ 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
270
 
173
271
  ```ruby
174
- devise :database_authenticatable, :registerable, :confirmable, :recoverable, :stretches => 20
272
+ devise :database_authenticatable, :registerable, :confirmable, :recoverable, stretches: 12
175
273
  ```
176
274
 
177
- Besides :stretches, you can define :pepper, :encryptor, :confirm_within, :remember_for, :timeout_in, :unlock_in and other values. For details, see the initializer file that was created when you invoked the "devise:install" generator described above.
275
+ 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
276
 
179
277
  ### Strong Parameters
180
278
 
279
+ ![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)
280
+
281
+ *For previous Devise versions see https://github.com/plataformatec/devise/tree/3-stable#strong-parameters*
282
+
181
283
  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
284
 
183
- There are just three actions in Devise that allows any set of parameters to be passed down to the model, therefore requiring sanitization. Their names and the permitted parameters by default are:
285
+ 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
286
 
185
- * `sign_in` (`Devise::SessionsController#new`) - Permits only the authentication keys (like `email`)
287
+ * `sign_in` (`Devise::SessionsController#create`) - Permits only the authentication keys (like `email`)
186
288
  * `sign_up` (`Devise::RegistrationsController#create`) - Permits authentication keys plus `password` and `password_confirmation`
187
289
  * `account_update` (`Devise::RegistrationsController#update`) - Permits authentication keys plus `password`, `password_confirmation` and `current_password`
188
290
 
189
- In case you want to permit additional parameters (the lazy way™) you can do with a simple before filter in your `ApplicationController`:
291
+ In case you want to permit additional parameters (the lazy way™), you can do so using a simple before filter in your `ApplicationController`:
190
292
 
191
293
  ```ruby
192
294
  class ApplicationController < ActionController::Base
193
- before_filter :configure_permitted_parameters, if: :devise_controller?
295
+ before_action :configure_permitted_parameters, if: :devise_controller?
194
296
 
195
297
  protected
196
298
 
197
299
  def configure_permitted_parameters
198
- devise_parameter_sanitizer.for(:sign_up) << :username
300
+ devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
301
+ end
302
+ end
303
+ ```
304
+
305
+ 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:
306
+
307
+ ```ruby
308
+ class ApplicationController < ActionController::Base
309
+ before_action :configure_permitted_parameters, if: :devise_controller?
310
+
311
+ protected
312
+
313
+ def configure_permitted_parameters
314
+ devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, address_attributes: [:country, :state, :city, :area, :postal_code]])
315
+ end
316
+ end
317
+ ```
318
+
319
+ Devise allows you to completely change Devise defaults or invoke custom behaviour by passing a block:
320
+
321
+ To permit simple scalar values for username and email, use this
322
+
323
+ ```ruby
324
+ def configure_permitted_parameters
325
+ devise_parameter_sanitizer.permit(:sign_in) do |user_params|
326
+ user_params.permit(:username, :email)
199
327
  end
200
328
  end
201
329
  ```
202
330
 
203
- To completely change Devise defaults or invoke custom behaviour, you can also pass a block:
331
+ 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:
204
332
 
205
333
  ```ruby
206
334
  def configure_permitted_parameters
207
- devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email) }
335
+ devise_parameter_sanitizer.permit(:sign_up) do |user_params|
336
+ user_params.permit({ roles: [] }, :email, :password, :password_confirmation)
337
+ end
208
338
  end
209
339
  ```
340
+ For the list of permitted scalars, and how to declare permitted keys in nested hashes and arrays, see
341
+
342
+ https://github.com/rails/strong_parameters#nested-parameters
210
343
 
211
- 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 add your own logic:
344
+ 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:
212
345
 
213
346
  ```ruby
214
347
  class User::ParameterSanitizer < Devise::ParameterSanitizer
215
- def sign_in
216
- default_params.permit(:username, :email)
348
+ def initialize(*)
349
+ super
350
+ permit(:sign_up, keys: [:username, :email])
217
351
  end
218
352
  end
219
353
  ```
@@ -243,63 +377,113 @@ We built Devise to help you quickly develop an application that uses authenticat
243
377
  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:
244
378
 
245
379
  ```console
246
- rails generate devise:views
380
+ $ rails generate devise:views
247
381
  ```
248
382
 
249
- 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 "config/initializers/devise.rb".
383
+ 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.
250
384
 
251
- 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:
385
+ 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:
252
386
 
253
387
  ```console
254
- rails generate devise:views users
388
+ $ rails generate devise:views users
389
+ ```
390
+
391
+ If you would like to generate only a few sets of views, like the ones for the `registerable` and `confirmable` module,
392
+ you can pass a list of modules to the generator with the `-v` flag.
393
+
394
+ ```console
395
+ $ rails generate devise:views -v registrations confirmations
255
396
  ```
256
397
 
257
398
  ### Configuring controllers
258
399
 
259
400
  If the customization at the views level is not enough, you can customize each controller by following these steps:
260
401
 
261
- 1. Create your custom controller, for example a `Admins::SessionsController`:
402
+ 1. Create your custom controllers using the generator which requires a scope:
403
+
404
+ ```console
405
+ $ rails generate devise:controllers [scope]
406
+ ```
407
+
408
+ If you specify `users` as the scope, controllers will be created in `app/controllers/users/`.
409
+ And the sessions controller will look like this:
262
410
 
263
411
  ```ruby
264
- class Admins::SessionsController < Devise::SessionsController
412
+ class Users::SessionsController < Devise::SessionsController
413
+ # GET /resource/sign_in
414
+ # def new
415
+ # super
416
+ # end
417
+ ...
265
418
  end
266
419
  ```
267
-
268
- Note that in the above example, the controller needs to be created in the `app/controller/admins/` directory.
420
+ (Use the -c flag to specify a controller, for example: `rails generate devise:controllers users -c=sessions`)
269
421
 
270
422
  2. Tell the router to use this controller:
271
423
 
272
424
  ```ruby
273
- devise_for :admins, :controllers => { :sessions => "admins/sessions" }
425
+ devise_for :users, controllers: { sessions: 'users/sessions' }
274
426
  ```
275
427
 
276
- 3. And since we changed the controller, it won't use the `"devise/sessions"` views, so remember to copy `"devise/sessions"` to `"admin/sessions"`.
428
+ 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`.
429
+
430
+ 4. Finally, change or extend the desired controller actions.
431
+
432
+ You can completely override a controller action:
433
+
434
+ ```ruby
435
+ class Users::SessionsController < Devise::SessionsController
436
+ def create
437
+ # custom sign-in code
438
+ end
439
+ end
440
+ ```
277
441
 
278
- Remember that Devise uses flash messages to let users know if sign in was successful or failed. Devise expects your application to call `"flash[:notice]"` and `"flash[:alert]"` as appropriate. Do not print the entire flash hash, print specific keys or at least remove the `:timedout` key from the hash as Devise adds this key in some circumstances, this key is not meant for display.
442
+ Or you can simply add new behaviour to it:
443
+
444
+ ```ruby
445
+ class Users::SessionsController < Devise::SessionsController
446
+ def create
447
+ super do |resource|
448
+ BackgroundWorker.trigger(resource)
449
+ end
450
+ end
451
+ end
452
+ ```
453
+
454
+ This is useful for triggering background jobs or logging events during certain actions.
455
+
456
+ 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.
279
457
 
280
458
  ### Configuring routes
281
459
 
282
460
  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:
283
461
 
284
462
  ```ruby
285
- 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' }
463
+ 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' }
286
464
  ```
287
465
 
288
- Be sure to check `devise_for` documentation for details.
466
+ Be sure to check `devise_for` [documentation](http://www.rubydoc.info/github/plataformatec/devise/master/ActionDispatch/Routing/Mapper%3Adevise_for) for details.
289
467
 
290
- 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 to create your routes normally and wrap them in a `devise_scope` block in the router:
468
+ 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:
291
469
 
292
470
  ```ruby
293
471
  devise_scope :user do
294
- get "sign_in", :to => "devise/sessions#new"
472
+ get 'sign_in', to: 'devise/sessions#new'
295
473
  end
296
474
  ```
297
475
 
298
- 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.
476
+ 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
+
478
+ Please note: You will still need to add `devise_for` in your routes in order to use helper methods such as `current_user`.
479
+
480
+ ```ruby
481
+ devise_for :users, skip: :all
482
+ ```
299
483
 
300
484
  ### I18n
301
485
 
302
- Devise uses flash messages with I18n with the flash keys :notice and :alert. To customize your app, you can set up your locale file:
486
+ 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:
303
487
 
304
488
  ```yaml
305
489
  en:
@@ -337,54 +521,115 @@ Take a look at our locale file to check all available messages. You may also be
337
521
 
338
522
  https://github.com/plataformatec/devise/wiki/I18n
339
523
 
524
+ Caution: Devise Controllers inherit from ApplicationController. If your app uses multiple locales, you should be sure to set I18n.locale in ApplicationController.
525
+
340
526
  ### Test helpers
341
527
 
342
- Devise includes some tests helpers for functional specs. In order to use them, you need to include Devise in your functional tests by adding the following to the bottom of your `test/test_helper.rb` file:
528
+ Devise includes some test helpers for controller and integration tests.
529
+ In order to use them, you need to include the respective module in your test
530
+ cases/specs.
531
+
532
+ ### Controller tests
533
+
534
+ Controller tests require that you include `Devise::Test::ControllerHelpers` on
535
+ your test case or its parent `ActionController::TestCase` superclass.
536
+ For Rails 5, include `Devise::Test::IntegrationHelpers` instead, since the superclass
537
+ for controller tests has been changed to ActionDispatch::IntegrationTest
538
+ (for more details, see the [Integration tests](#integration-tests) section).
343
539
 
344
540
  ```ruby
345
- class ActionController::TestCase
346
- include Devise::TestHelpers
541
+ class PostsControllerTest < ActionController::TestCase
542
+ include Devise::Test::ControllerHelpers
347
543
  end
348
544
  ```
349
545
 
350
- If you're using RSpec, you can put the following inside a file named `spec/support/devise.rb`:
546
+ If you're using RSpec, you can put the following inside a file named
547
+ `spec/support/devise.rb` or in your `spec/spec_helper.rb` (or
548
+ `spec/rails_helper.rb` if you are using `rspec-rails`):
351
549
 
352
550
  ```ruby
353
551
  RSpec.configure do |config|
354
- config.include Devise::TestHelpers, :type => :controller
552
+ config.include Devise::Test::ControllerHelpers, type: :controller
553
+ config.include Devise::Test::ControllerHelpers, type: :view
554
+ end
555
+ ```
556
+
557
+ Just be sure that this inclusion is made *after* the `require 'rspec/rails'` directive.
558
+
559
+ Now you are ready to use the `sign_in` and `sign_out` methods on your controller
560
+ tests:
561
+
562
+ ```ruby
563
+ sign_in @user
564
+ sign_in @user, scope: :admin
565
+ ```
566
+
567
+ If you are testing Devise internal controllers or a controller that inherits
568
+ from Devise's, you need to tell Devise which mapping should be used before a
569
+ request. This is necessary because Devise gets this information from the router,
570
+ but since controller tests do not pass through the router, it needs to be stated
571
+ explicitly. For example, if you are testing the user scope, simply use:
572
+
573
+ ```ruby
574
+ test 'GET new' do
575
+ # Mimic the router behavior of setting the Devise scope through the env.
576
+ @request.env['devise.mapping'] = Devise.mappings[:user]
577
+
578
+ # Use the sign_in helper to sign in a fixture `User` record.
579
+ sign_in users(:alice)
580
+
581
+ get :new
582
+
583
+ # assert something
355
584
  end
356
585
  ```
357
586
 
358
- Now you are ready to use the `sign_in` and `sign_out` methods. Such methods have the same signature as in controllers:
587
+ ### Integration tests
588
+
589
+ Integration test helpers are available by including the
590
+ `Devise::Test::IntegrationHelpers` module.
359
591
 
360
592
  ```ruby
361
- sign_in :user, @user # sign_in(scope, resource)
362
- sign_in @user # sign_in(resource)
593
+ class PostsTests < ActionDispatch::IntegrationTest
594
+ include Devise::Test::IntegrationHelpers
595
+ end
596
+ ```
363
597
 
364
- sign_out :user # sign_out(scope)
365
- sign_out @user # sign_out(resource)
598
+ Now you can use the following `sign_in` and `sign_out` methods in your integration
599
+ tests:
600
+
601
+ ```ruby
602
+ sign_in users(:bob)
603
+ sign_in users(:bob), scope: :admin
604
+
605
+ sign_out :user
366
606
  ```
367
607
 
368
- There are two things that is important to keep in mind:
608
+ RSpec users can include the `IntegrationHelpers` module on their `:feature` specs.
369
609
 
370
- 1. These helpers are not going to work for integration tests driven by Capybara or Webrat. They are meant to be used with functional tests only. Instead, fill in the form or explicitly set the user in session;
610
+ ```ruby
611
+ RSpec.configure do |config|
612
+ config.include Devise::Test::IntegrationHelpers, type: :feature
613
+ end
614
+ ```
371
615
 
372
- 2. If you are testing Devise internal controllers or a controller that inherits from Devise's, you need to tell Devise which mapping should be used before a request. This is necessary because Devise gets this information from router, but since functional tests do not pass through the router, it needs to be told explicitly. For example, if you are testing the user scope, simply do:
616
+ Unlike controller tests, integration tests do not need to supply the
617
+ `devise.mapping` `env` value, as the mapping can be inferred by the routes that
618
+ are executed in your tests.
373
619
 
374
- ```ruby
375
- @request.env["devise.mapping"] = Devise.mappings[:user]
376
- get :new
377
- ```
620
+ You can read more about testing your Rails 3 - Rails 4 controllers with RSpec in the wiki:
378
621
 
379
- ### Omniauth
622
+ * https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29
380
623
 
381
- Devise comes with Omniauth support out of the box to authenticate with other providers. To use it, just specify your omniauth configuration in `config/initializers/devise.rb`:
624
+ ### OmniAuth
625
+
626
+ 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`:
382
627
 
383
628
  ```ruby
384
- config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
629
+ config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
385
630
  ```
386
631
 
387
- You can read more about Omniauth support in the wiki:
632
+ You can read more about OmniAuth support in the wiki:
388
633
 
389
634
  * https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
390
635
 
@@ -397,7 +642,7 @@ Devise allows you to set up as many Devise models as you want. If you want to ha
397
642
  create_table :admins do |t|
398
643
  t.string :email
399
644
  t.string :encrypted_password
400
- t.timestamps
645
+ t.timestamps null: false
401
646
  end
402
647
 
403
648
  # Inside your Admin model
@@ -407,7 +652,7 @@ devise :database_authenticatable, :timeoutable
407
652
  devise_for :admins
408
653
 
409
654
  # Inside your protected controller
410
- before_filter :authenticate_admin!
655
+ before_action :authenticate_admin!
411
656
 
412
657
  # Inside your controllers and views
413
658
  admin_signed_in?
@@ -417,17 +662,43 @@ admin_session
417
662
 
418
663
  Alternatively, you can simply run the Devise generator.
419
664
 
420
- 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 to use a role-based approach, by either providing a role column or using [CanCan](https://github.com/ryanb/cancan).
665
+ 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.
666
+
667
+ ### ActiveJob Integration
668
+
669
+ If you are using Rails 4.2 and ActiveJob to deliver ActionMailer messages in the
670
+ background through a queuing back-end, you can send Devise emails through your
671
+ existing queue by overriding the `send_devise_notification` method in your model.
672
+
673
+ ```ruby
674
+ def send_devise_notification(notification, *args)
675
+ devise_mailer.send(notification, self, *args).deliver_later
676
+ end
677
+ ```
678
+
679
+ ### Password reset tokens and Rails logs
680
+
681
+ 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:
682
+
683
+ 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.
684
+ 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.
685
+
686
+ 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`:
687
+
688
+ ```ruby
689
+ config.log_level = :warn
690
+ ```
691
+
421
692
 
422
693
  ### Other ORMs
423
694
 
424
- Devise supports ActiveRecord (default) and Mongoid. To choose other ORM, you just need to require it in the initializer file.
695
+ Devise supports ActiveRecord (default) and Mongoid. To select another ORM, simply require it in the initializer file.
425
696
 
426
697
  ## Additional information
427
698
 
428
699
  ### Heroku
429
700
 
430
- Using devise on Heroku with Ruby on Rails 3.1 requires setting:
701
+ Using Devise on Heroku with Ruby on Rails 3.2 requires setting:
431
702
 
432
703
  ```ruby
433
704
  config.assets.initialize_on_precompile = false
@@ -449,6 +720,6 @@ https://github.com/plataformatec/devise/graphs/contributors
449
720
 
450
721
  ## License
451
722
 
452
- MIT License. Copyright 2009-2013 Plataformatec. http://plataformatec.com.br
723
+ MIT License. Copyright 2009-2018 Plataformatec. http://plataformatec.com.br
453
724
 
454
- You are not granted rights or licenses to the trademarks of the Plataformatec, including without limitation the Devise name or logo.
725
+ You are not granted rights or licenses to the trademarks of Plataformatec, including without limitation the Devise name or logo.