devise 3.2.1 → 4.4.3

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