devise 3.0.0 → 4.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (242) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +351 -0
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +422 -130
  5. data/app/controllers/devise/confirmations_controller.rb +17 -6
  6. data/app/controllers/devise/omniauth_callbacks_controller.rb +12 -6
  7. data/app/controllers/devise/passwords_controller.rb +23 -8
  8. data/app/controllers/devise/registrations_controller.rb +70 -28
  9. data/app/controllers/devise/sessions_controller.rb +49 -17
  10. data/app/controllers/devise/unlocks_controller.rb +11 -4
  11. data/app/controllers/devise_controller.rb +74 -34
  12. data/app/helpers/devise_helper.rb +23 -18
  13. data/app/mailers/devise/mailer.rb +25 -10
  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} +10 -10
  27. data/app/views/devise/unlocks/new.html.erb +9 -5
  28. data/config/locales/en.yml +26 -20
  29. data/lib/devise/controllers/helpers.rb +122 -125
  30. data/lib/devise/controllers/rememberable.rb +14 -14
  31. data/lib/devise/controllers/scoped_views.rb +3 -1
  32. data/lib/devise/controllers/sign_in_out.rb +121 -0
  33. data/lib/devise/controllers/store_location.rb +76 -0
  34. data/lib/devise/controllers/url_helpers.rb +10 -8
  35. data/lib/devise/delegator.rb +2 -0
  36. data/lib/devise/encryptor.rb +24 -0
  37. data/lib/devise/failure_app.rb +132 -42
  38. data/lib/devise/hooks/activatable.rb +7 -6
  39. data/lib/devise/hooks/csrf_cleaner.rb +9 -0
  40. data/lib/devise/hooks/forgetable.rb +3 -1
  41. data/lib/devise/hooks/lockable.rb +5 -3
  42. data/lib/devise/hooks/proxy.rb +23 -0
  43. data/lib/devise/hooks/rememberable.rb +7 -4
  44. data/lib/devise/hooks/timeoutable.rb +18 -8
  45. data/lib/devise/hooks/trackable.rb +3 -1
  46. data/lib/devise/mailers/helpers.rb +15 -18
  47. data/lib/devise/mapping.rb +9 -3
  48. data/lib/devise/models/authenticatable.rb +102 -80
  49. data/lib/devise/models/confirmable.rb +154 -72
  50. data/lib/devise/models/database_authenticatable.rb +125 -25
  51. data/lib/devise/models/lockable.rb +50 -29
  52. data/lib/devise/models/omniauthable.rb +3 -1
  53. data/lib/devise/models/recoverable.rb +72 -50
  54. data/lib/devise/models/registerable.rb +4 -0
  55. data/lib/devise/models/rememberable.rb +65 -32
  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 +6 -13
  60. data/lib/devise/modules.rb +12 -11
  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 +4 -5
  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 +144 -34
  68. data/lib/devise/rails/deprecated_constant_accessor.rb +39 -0
  69. data/lib/devise/rails/routes.rb +191 -127
  70. data/lib/devise/rails/warden_compat.rb +2 -1
  71. data/lib/devise/rails.rb +13 -20
  72. data/lib/devise/secret_key_finder.rb +27 -0
  73. data/lib/devise/strategies/authenticatable.rb +21 -22
  74. data/lib/devise/strategies/base.rb +3 -1
  75. data/lib/devise/strategies/database_authenticatable.rb +15 -4
  76. data/lib/devise/strategies/rememberable.rb +15 -3
  77. data/lib/devise/test/controller_helpers.rb +167 -0
  78. data/lib/devise/test/integration_helpers.rb +63 -0
  79. data/lib/devise/test_helpers.rb +7 -123
  80. data/lib/devise/time_inflector.rb +4 -2
  81. data/lib/devise/token_generator.rb +32 -0
  82. data/lib/devise/version.rb +3 -1
  83. data/lib/devise.rb +124 -78
  84. data/lib/generators/active_record/devise_generator.rb +64 -15
  85. data/lib/generators/active_record/templates/migration.rb +9 -8
  86. data/lib/generators/active_record/templates/migration_existing.rb +9 -8
  87. data/lib/generators/devise/controllers_generator.rb +46 -0
  88. data/lib/generators/devise/devise_generator.rb +10 -6
  89. data/lib/generators/devise/install_generator.rb +19 -1
  90. data/lib/generators/devise/orm_helpers.rb +17 -9
  91. data/lib/generators/devise/views_generator.rb +51 -28
  92. data/lib/generators/mongoid/devise_generator.rb +24 -24
  93. data/lib/generators/templates/README +13 -12
  94. data/lib/generators/templates/controllers/README +14 -0
  95. data/lib/generators/templates/controllers/confirmations_controller.rb +30 -0
  96. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +30 -0
  97. data/lib/generators/templates/controllers/passwords_controller.rb +34 -0
  98. data/lib/generators/templates/controllers/registrations_controller.rb +62 -0
  99. data/lib/generators/templates/controllers/sessions_controller.rb +27 -0
  100. data/lib/generators/templates/controllers/unlocks_controller.rb +30 -0
  101. data/lib/generators/templates/devise.rb +118 -53
  102. data/lib/generators/templates/markerb/confirmation_instructions.markerb +1 -1
  103. data/lib/generators/templates/markerb/email_changed.markerb +7 -0
  104. data/lib/generators/templates/markerb/password_change.markerb +3 -0
  105. data/lib/generators/templates/markerb/reset_password_instructions.markerb +1 -1
  106. data/lib/generators/templates/markerb/unlock_instructions.markerb +1 -1
  107. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +6 -2
  108. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +12 -4
  109. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +5 -2
  110. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +14 -6
  111. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +12 -4
  112. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +11 -6
  113. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +5 -2
  114. metadata +73 -294
  115. data/.gitignore +0 -10
  116. data/.travis.yml +0 -20
  117. data/.yardopts +0 -9
  118. data/CHANGELOG.rdoc +0 -941
  119. data/CONTRIBUTING.md +0 -14
  120. data/Gemfile +0 -31
  121. data/Gemfile.lock +0 -159
  122. data/Rakefile +0 -35
  123. data/app/views/devise/_links.erb +0 -3
  124. data/devise.gemspec +0 -26
  125. data/devise.png +0 -0
  126. data/gemfiles/Gemfile.rails-3.2.x +0 -31
  127. data/gemfiles/Gemfile.rails-3.2.x.lock +0 -156
  128. data/lib/devise/models/token_authenticatable.rb +0 -89
  129. data/lib/devise/strategies/token_authenticatable.rb +0 -91
  130. data/test/controllers/custom_strategy_test.rb +0 -62
  131. data/test/controllers/helpers_test.rb +0 -253
  132. data/test/controllers/internal_helpers_test.rb +0 -120
  133. data/test/controllers/passwords_controller_test.rb +0 -32
  134. data/test/controllers/sessions_controller_test.rb +0 -99
  135. data/test/controllers/url_helpers_test.rb +0 -59
  136. data/test/delegator_test.rb +0 -19
  137. data/test/devise_test.rb +0 -83
  138. data/test/failure_app_test.rb +0 -221
  139. data/test/generators/active_record_generator_test.rb +0 -73
  140. data/test/generators/devise_generator_test.rb +0 -39
  141. data/test/generators/install_generator_test.rb +0 -13
  142. data/test/generators/mongoid_generator_test.rb +0 -23
  143. data/test/generators/views_generator_test.rb +0 -67
  144. data/test/helpers/devise_helper_test.rb +0 -51
  145. data/test/integration/authenticatable_test.rb +0 -699
  146. data/test/integration/confirmable_test.rb +0 -299
  147. data/test/integration/database_authenticatable_test.rb +0 -84
  148. data/test/integration/http_authenticatable_test.rb +0 -115
  149. data/test/integration/lockable_test.rb +0 -242
  150. data/test/integration/omniauthable_test.rb +0 -133
  151. data/test/integration/recoverable_test.rb +0 -335
  152. data/test/integration/registerable_test.rb +0 -349
  153. data/test/integration/rememberable_test.rb +0 -165
  154. data/test/integration/timeoutable_test.rb +0 -150
  155. data/test/integration/token_authenticatable_test.rb +0 -205
  156. data/test/integration/trackable_test.rb +0 -92
  157. data/test/mailers/confirmation_instructions_test.rb +0 -111
  158. data/test/mailers/reset_password_instructions_test.rb +0 -92
  159. data/test/mailers/unlock_instructions_test.rb +0 -87
  160. data/test/mapping_test.rb +0 -127
  161. data/test/models/authenticatable_test.rb +0 -13
  162. data/test/models/confirmable_test.rb +0 -452
  163. data/test/models/database_authenticatable_test.rb +0 -226
  164. data/test/models/lockable_test.rb +0 -282
  165. data/test/models/omniauthable_test.rb +0 -7
  166. data/test/models/recoverable_test.rb +0 -222
  167. data/test/models/registerable_test.rb +0 -7
  168. data/test/models/rememberable_test.rb +0 -175
  169. data/test/models/serializable_test.rb +0 -49
  170. data/test/models/timeoutable_test.rb +0 -46
  171. data/test/models/token_authenticatable_test.rb +0 -55
  172. data/test/models/trackable_test.rb +0 -13
  173. data/test/models/validatable_test.rb +0 -127
  174. data/test/models_test.rb +0 -163
  175. data/test/omniauth/config_test.rb +0 -57
  176. data/test/omniauth/url_helpers_test.rb +0 -54
  177. data/test/orm/active_record.rb +0 -10
  178. data/test/orm/mongoid.rb +0 -13
  179. data/test/parameter_sanitizer_test.rb +0 -58
  180. data/test/rails_app/Rakefile +0 -6
  181. data/test/rails_app/app/active_record/admin.rb +0 -6
  182. data/test/rails_app/app/active_record/shim.rb +0 -2
  183. data/test/rails_app/app/active_record/user.rb +0 -6
  184. data/test/rails_app/app/controllers/admins/sessions_controller.rb +0 -6
  185. data/test/rails_app/app/controllers/admins_controller.rb +0 -11
  186. data/test/rails_app/app/controllers/application_controller.rb +0 -9
  187. data/test/rails_app/app/controllers/home_controller.rb +0 -25
  188. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +0 -2
  189. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +0 -2
  190. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +0 -14
  191. data/test/rails_app/app/controllers/users_controller.rb +0 -31
  192. data/test/rails_app/app/helpers/application_helper.rb +0 -3
  193. data/test/rails_app/app/mailers/users/mailer.rb +0 -12
  194. data/test/rails_app/app/mongoid/admin.rb +0 -29
  195. data/test/rails_app/app/mongoid/shim.rb +0 -23
  196. data/test/rails_app/app/mongoid/user.rb +0 -42
  197. data/test/rails_app/app/views/admins/index.html.erb +0 -1
  198. data/test/rails_app/app/views/admins/sessions/new.html.erb +0 -2
  199. data/test/rails_app/app/views/home/admin_dashboard.html.erb +0 -1
  200. data/test/rails_app/app/views/home/index.html.erb +0 -1
  201. data/test/rails_app/app/views/home/join.html.erb +0 -1
  202. data/test/rails_app/app/views/home/private.html.erb +0 -1
  203. data/test/rails_app/app/views/home/user_dashboard.html.erb +0 -1
  204. data/test/rails_app/app/views/layouts/application.html.erb +0 -24
  205. data/test/rails_app/app/views/users/edit_form.html.erb +0 -1
  206. data/test/rails_app/app/views/users/index.html.erb +0 -1
  207. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +0 -1
  208. data/test/rails_app/app/views/users/sessions/new.html.erb +0 -1
  209. data/test/rails_app/bin/bundle +0 -3
  210. data/test/rails_app/bin/rails +0 -4
  211. data/test/rails_app/bin/rake +0 -4
  212. data/test/rails_app/config/application.rb +0 -40
  213. data/test/rails_app/config/boot.rb +0 -8
  214. data/test/rails_app/config/database.yml +0 -18
  215. data/test/rails_app/config/environment.rb +0 -5
  216. data/test/rails_app/config/environments/development.rb +0 -34
  217. data/test/rails_app/config/environments/production.rb +0 -84
  218. data/test/rails_app/config/environments/test.rb +0 -36
  219. data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  220. data/test/rails_app/config/initializers/devise.rb +0 -178
  221. data/test/rails_app/config/initializers/inflections.rb +0 -2
  222. data/test/rails_app/config/initializers/secret_token.rb +0 -8
  223. data/test/rails_app/config/initializers/session_store.rb +0 -1
  224. data/test/rails_app/config/routes.rb +0 -104
  225. data/test/rails_app/config.ru +0 -4
  226. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -74
  227. data/test/rails_app/db/schema.rb +0 -52
  228. data/test/rails_app/lib/shared_admin.rb +0 -14
  229. data/test/rails_app/lib/shared_user.rb +0 -25
  230. data/test/rails_app/public/404.html +0 -26
  231. data/test/rails_app/public/422.html +0 -26
  232. data/test/rails_app/public/500.html +0 -26
  233. data/test/rails_app/public/favicon.ico +0 -0
  234. data/test/routes_test.rb +0 -250
  235. data/test/support/assertions.rb +0 -40
  236. data/test/support/helpers.rb +0 -91
  237. data/test/support/integration.rb +0 -92
  238. data/test/support/locale/en.yml +0 -4
  239. data/test/support/webrat/integrations/rails.rb +0 -24
  240. data/test/test_helper.rb +0 -34
  241. data/test/test_helpers_test.rb +0 -151
  242. data/test/test_models.rb +0 -26
data/README.md CHANGED
@@ -1,35 +1,65 @@
1
- ![Devise Logo](https://raw.github.com/plataformatec/devise/master/devise.png)
1
+ ![Devise Logo](https://raw.github.com/heartcombo/devise/master/devise.png)
2
2
 
3
- By [Plataformatec](http://plataformatec.com.br/).
4
-
5
- [![Gem Version](https://fury-badge.herokuapp.com/rb/devise.png)](http://badge.fury.io/rb/devise)
6
- [![Build Status](https://api.travis-ci.org/plataformatec/devise.png?branch=master)](http://travis-ci.org/plataformatec/devise)
7
- [![Code Climate](https://codeclimate.com/github/plataformatec/devise.png)](https://codeclimate.com/github/plataformatec/devise)
8
-
9
- This README is [also available in a friendly navigable format](http://devise.plataformatec.com.br/).
3
+ [![Code Climate](https://codeclimate.com/github/heartcombo/devise.svg)](https://codeclimate.com/github/heartcombo/devise)
10
4
 
11
5
  Devise is a flexible authentication solution for Rails based on Warden. It:
12
6
 
13
7
  * Is Rack based;
14
8
  * Is a complete MVC solution based on Rails engines;
15
- * Allows you to have multiple roles (or models/scopes) signed in at the same time;
16
- * Is based on a modularity concept: use just what you really need.
17
-
18
- It's composed of 11 modules:
9
+ * Allows you to have multiple models signed in at the same time;
10
+ * Is based on a modularity concept: use only what you really need.
11
+
12
+ It's composed of 10 modules:
13
+
14
+ * [Database Authenticatable](http://www.rubydoc.info/github/heartcombo/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.
15
+ * [Omniauthable](http://www.rubydoc.info/github/heartcombo/devise/master/Devise/Models/Omniauthable): adds OmniAuth (https://github.com/omniauth/omniauth) support.
16
+ * [Confirmable](http://www.rubydoc.info/github/heartcombo/devise/master/Devise/Models/Confirmable): sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.
17
+ * [Recoverable](http://www.rubydoc.info/github/heartcombo/devise/master/Devise/Models/Recoverable): resets the user password and sends reset instructions.
18
+ * [Registerable](http://www.rubydoc.info/github/heartcombo/devise/master/Devise/Models/Registerable): handles signing up users through a registration process, also allowing them to edit and destroy their account.
19
+ * [Rememberable](http://www.rubydoc.info/github/heartcombo/devise/master/Devise/Models/Rememberable): manages generating and clearing a token for remembering the user from a saved cookie.
20
+ * [Trackable](http://www.rubydoc.info/github/heartcombo/devise/master/Devise/Models/Trackable): tracks sign in count, timestamps and IP address.
21
+ * [Timeoutable](http://www.rubydoc.info/github/heartcombo/devise/master/Devise/Models/Timeoutable): expires sessions that have not been active in a specified period of time.
22
+ * [Validatable](http://www.rubydoc.info/github/heartcombo/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.
23
+ * [Lockable](http://www.rubydoc.info/github/heartcombo/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.
24
+
25
+ ## Table of Contents
26
+
27
+ <!-- TOC depthFrom:1 depthTo:6 withLinks:1 orderedList:0 -->
28
+
29
+ - [Information](#information)
30
+ - [The Devise wiki](#the-devise-wiki)
31
+ - [Bug reports](#bug-reports)
32
+ - [StackOverflow and Mailing List](#stackoverflow-and-mailing-list)
33
+ - [RDocs](#rdocs)
34
+ - [Example applications](#example-applications)
35
+ - [Extensions](#extensions)
36
+ - [Contributing](#contributing)
37
+ - [Starting with Rails?](#starting-with-rails)
38
+ - [Getting started](#getting-started)
39
+ - [Controller filters and helpers](#controller-filters-and-helpers)
40
+ - [Configuring Models](#configuring-models)
41
+ - [Strong Parameters](#strong-parameters)
42
+ - [Configuring views](#configuring-views)
43
+ - [Configuring controllers](#configuring-controllers)
44
+ - [Configuring routes](#configuring-routes)
45
+ - [I18n](#i18n)
46
+ - [Test helpers](#test-helpers)
47
+ - [Controller tests](#controller-tests)
48
+ - [Integration tests](#integration-tests)
49
+ - [OmniAuth](#omniauth)
50
+ - [Configuring multiple models](#configuring-multiple-models)
51
+ - [ActiveJob Integration](#activejob-integration)
52
+ - [Password reset tokens and Rails logs](#password-reset-tokens-and-rails-logs)
53
+ - [Other ORMs](#other-orms)
54
+ - [Rails API mode](#rails-api-mode)
55
+ - [Additional information](#additional-information)
56
+ - [Warden](#warden)
57
+ - [Contributors](#contributors)
58
+ - [License](#license)
59
+
60
+ <!-- /TOC -->
19
61
 
20
- * [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.
21
- * [Token Authenticatable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/TokenAuthenticatable): signs in a user based on an authentication token (also known as "single access token"). The token can be given both through query string or HTTP Basic Authentication.
22
- * [Omniauthable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Omniauthable): adds Omniauth (https://github.com/intridea/omniauth) support;
23
- * [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.
24
- * [Recoverable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Recoverable): resets the user password and sends reset instructions.
25
- * [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.
26
- * [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.
27
- * [Trackable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Trackable): tracks sign in count, timestamps and IP address.
28
- * [Timeoutable](http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/Timeoutable): expires sessions that have no activity in a specified period of time.
29
- * [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.
30
- * [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.
31
62
 
32
- Devise is guaranteed to be thread-safe on YARV. Thread-safety support on JRuby is on progress.
33
63
 
34
64
  ## Information
35
65
 
@@ -37,19 +67,23 @@ Devise is guaranteed to be thread-safe on YARV. Thread-safety support on JRuby i
37
67
 
38
68
  The Devise Wiki has lots of additional information about Devise including many "how-to" articles and answers to the most frequently asked questions. Please browse the Wiki after finishing this README:
39
69
 
40
- https://wiki.github.com/plataformatec/devise
70
+ https://github.com/heartcombo/devise/wiki
41
71
 
42
72
  ### Bug reports
43
73
 
44
74
  If you discover a problem with Devise, we would like to know about it. However, we ask that you please review these guidelines before submitting a bug report:
45
75
 
46
- https://github.com/plataformatec/devise/wiki/Bug-reports
76
+ https://github.com/heartcombo/devise/wiki/Bug-reports
77
+
78
+ If you have discovered a security related bug, please do *NOT* use the GitHub issue tracker. Send an email to heartcombo@googlegroups.com.
79
+
80
+ ### StackOverflow and Mailing List
47
81
 
48
- 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.
82
+ If you have any questions, comments, or concerns, please use StackOverflow instead of the GitHub issue tracker:
49
83
 
50
- ### Mailing list
84
+ http://stackoverflow.com/questions/tagged/devise
51
85
 
52
- If you have any questions, comments, or concerns, please use the Google Group instead of the GitHub issue tracker:
86
+ The deprecated mailing list can still be read on
53
87
 
54
88
  https://groups.google.com/group/plataformatec-devise
55
89
 
@@ -57,7 +91,7 @@ https://groups.google.com/group/plataformatec-devise
57
91
 
58
92
  You can view the Devise documentation in RDoc format here:
59
93
 
60
- http://rubydoc.info/github/plataformatec/devise/master/frames
94
+ http://rubydoc.info/github/heartcombo/devise/master/frames
61
95
 
62
96
  If you need to use Devise with previous versions of Rails, you can always run "gem server" from the command line after you install the gem to access the old documentation.
63
97
 
@@ -65,65 +99,128 @@ If you need to use Devise with previous versions of Rails, you can always run "g
65
99
 
66
100
  There are a few example applications available on GitHub that demonstrate various features of Devise with different versions of Rails. You can view them here:
67
101
 
68
- https://github.com/plataformatec/devise/wiki/Example-Applications
102
+ https://github.com/heartcombo/devise/wiki/Example-Applications
69
103
 
70
104
  ### Extensions
71
105
 
72
106
  Our community has created a number of extensions that add functionality above and beyond what is included with Devise. You can view a list of available extensions and add your own here:
73
107
 
74
- https://github.com/plataformatec/devise/wiki/Extensions
108
+ https://github.com/heartcombo/devise/wiki/Extensions
75
109
 
76
110
  ### Contributing
77
111
 
78
112
  We hope that you will consider contributing to Devise. Please read this short overview for some information about how to get started:
79
113
 
80
- https://github.com/plataformatec/devise/wiki/Contributing
114
+ https://github.com/heartcombo/devise/wiki/Contributing
115
+
116
+ 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`.
117
+ 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`.
118
+
119
+ ### DEVISE_ORM
120
+ Since Devise supports both Mongoid and ActiveRecord, we rely on this variable to run specific code for each ORM.
121
+ The default value of `DEVISE_ORM` is `active_record`. To run the tests for Mongoid, you can pass `mongoid`:
122
+ ```
123
+ DEVISE_ORM=mongoid bin/test
124
+
125
+ ==> Devise.orm = :mongoid
126
+ ```
127
+ When running the tests for Mongoid, you will need to have a MongoDB server (version 2.0 or newer) running on your system.
128
+
129
+ Please note that the command output will show the variable value being used.
130
+
131
+ ### BUNDLE_GEMFILE
132
+ We can use this variable to tell bundler what Gemfile it should use (instead of the one in the current directory).
133
+ Inside the [gemfiles](https://github.com/heartcombo/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 using some of them. If that's the case, you can simulate the same environment using the `BUNDLE_GEMFILE` variable.
134
+ For example, if the tests broke using Ruby 2.4.2 and Rails 4.1, you can do the following:
135
+ ```bash
136
+ rbenv shell 2.4.2 # or rvm use 2.4.2
137
+ BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable bundle install
138
+ BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable bin/test
139
+ ```
140
+
141
+ You can also combine both of them if the tests broke for Mongoid:
142
+ ```bash
143
+ BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable bundle install
144
+ BUNDLE_GEMFILE=gemfiles/Gemfile.rails-4.1-stable DEVISE_ORM=mongoid bin/test
145
+ ```
146
+
147
+ ### Running tests
148
+ Devise uses [Mini Test](https://github.com/seattlerb/minitest) as test framework.
149
+
150
+ * Running all tests:
151
+ ```bash
152
+ bin/test
153
+ ```
154
+
155
+ * Running tests for an specific file:
156
+ ```bash
157
+ bin/test test/models/trackable_test.rb
158
+ ```
81
159
 
82
- 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.
160
+ * Running a specific test given a regex:
161
+ ```bash
162
+ bin/test test/models/trackable_test.rb:16
163
+ ```
83
164
 
84
165
  ## Starting with Rails?
85
166
 
86
- 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:
167
+ 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. Here's a few resources that should help you get started:
87
168
 
88
- * Michael Hartl's online book: http://railstutorial.org/chapters/modeling-and-viewing-users-two#top
89
- * Ryan Bates' Railscast: http://railscasts.com/episodes/250-authentication-from-scratch
169
+ * Michael Hartl's online book: https://www.railstutorial.org/book/modeling_users
170
+ * Ryan Bates' Railscasts: http://railscasts.com/episodes/250-authentication-from-scratch and http://railscasts.com/episodes/250-authentication-from-scratch-revised
171
+ * Codecademy's Ruby on Rails: Authentication and Authorization: https://www.codecademy.com/learn/rails-auth
90
172
 
91
- Once you have solidified your understanding of Rails and authentication mechanisms, we assure you Devise will be very pleasant to work with. :)
173
+ Once you have solidified your understanding of Rails and authentication mechanisms, we assure you Devise will be very pleasant to work with. :smiley:
92
174
 
93
175
  ## Getting started
94
176
 
95
- Devise 3.0 works with Rails 3.2 onwards. You can add it to your Gemfile with:
177
+ Devise 4.0 works with Rails 4.1 onwards. Add the following line to your Gemfile:
96
178
 
97
179
  ```ruby
98
180
  gem 'devise'
99
181
  ```
100
182
 
101
- Run the bundle command to install it.
183
+ Then run `bundle install`
102
184
 
103
- After you install Devise and add it to your Gemfile, you need to run the generator:
185
+ Next, you need to run the generator:
104
186
 
105
187
  ```console
106
- rails generate devise:install
188
+ $ rails generate devise:install
189
+ ```
190
+
191
+ 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`:
192
+
193
+ ```ruby
194
+ config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
107
195
  ```
108
196
 
109
- 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:
197
+ 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.
198
+
199
+
200
+ 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.
110
201
 
111
202
  ```console
112
- rails generate devise MODEL
203
+ $ rails generate devise MODEL
113
204
  ```
114
205
 
115
- 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.
206
+ 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.
116
207
 
117
- 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.
208
+ Then run `rails db:migrate`
209
+
210
+ 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.
118
211
 
119
212
  ### Controller filters and helpers
120
213
 
121
- 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:
214
+ 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'):
122
215
 
123
216
  ```ruby
124
- before_filter :authenticate_user!
217
+ before_action :authenticate_user!
125
218
  ```
126
219
 
220
+ 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`.
221
+
222
+ If your devise model is something other than User, replace "_user" with "_yourmodel". The same logic applies to the instructions below.
223
+
127
224
  To verify if a user is signed in, use the following helper:
128
225
 
129
226
  ```ruby
@@ -142,24 +239,18 @@ You can access the session for this scope:
142
239
  user_session
143
240
  ```
144
241
 
145
- 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:
242
+ 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:
146
243
 
147
244
  ```ruby
148
- root to: "home#index"
245
+ root to: 'home#index'
149
246
  ```
150
247
 
151
- You can also overwrite `after_sign_in_path_for` and `after_sign_out_path_for` to customize your redirect hooks.
152
-
153
- Finally, you need to set up default url options for the mailer in each environment. Here is the configuration for "config/environments/development.rb":
154
-
155
- ```ruby
156
- config.action_mailer.default_url_options = { :host => 'localhost:3000' }
157
- ```
248
+ You can also override `after_sign_in_path_for` and `after_sign_out_path_for` to customize your redirect hooks.
158
249
 
159
- Notice that if your devise model is not called "user" but "member", then the helpers you should use are:
250
+ Notice that if your Devise model is called `Member` instead of `User`, for example, then the helpers available are:
160
251
 
161
252
  ```ruby
162
- before_filter :authenticate_member!
253
+ before_action :authenticate_member!
163
254
 
164
255
  member_signed_in?
165
256
 
@@ -170,44 +261,88 @@ member_session
170
261
 
171
262
  ### Configuring Models
172
263
 
173
- 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:
264
+ 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:
174
265
 
175
266
  ```ruby
176
- devise :database_authenticatable, :registerable, :confirmable, :recoverable, :stretches => 20
267
+ devise :database_authenticatable, :registerable, :confirmable, :recoverable, stretches: 13
177
268
  ```
178
269
 
179
- 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.
270
+ 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`.
180
271
 
181
272
  ### Strong Parameters
182
273
 
274
+ The Parameter Sanitizer API has changed for Devise 4 :warning:
275
+
276
+ *For previous Devise versions see https://github.com/heartcombo/devise/tree/3-stable#strong-parameters*
277
+
183
278
  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.
184
279
 
185
- 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:
280
+ 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:
186
281
 
187
- * `sign_in` (`Devise::SessionsController#new`) - Permits only the authentication keys (like `email`)
282
+ * `sign_in` (`Devise::SessionsController#create`) - Permits only the authentication keys (like `email`)
188
283
  * `sign_up` (`Devise::RegistrationsController#create`) - Permits authentication keys plus `password` and `password_confirmation`
189
284
  * `account_update` (`Devise::RegistrationsController#update`) - Permits authentication keys plus `password`, `password_confirmation` and `current_password`
190
285
 
191
- In case you want to customize the permitted parameters (the lazy way™) you can do with a simple before filter in your `ApplicationController`:
286
+ In case you want to permit additional parameters (the lazy way™), you can do so using a simple before action in your `ApplicationController`:
287
+
288
+ ```ruby
289
+ class ApplicationController < ActionController::Base
290
+ before_action :configure_permitted_parameters, if: :devise_controller?
291
+
292
+ protected
293
+
294
+ def configure_permitted_parameters
295
+ devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
296
+ end
297
+ end
298
+ ```
299
+
300
+ 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:
192
301
 
193
302
  ```ruby
194
303
  class ApplicationController < ActionController::Base
195
- before_filter :configure_permitted_parameters, if: :devise_controller?
304
+ before_action :configure_permitted_parameters, if: :devise_controller?
196
305
 
197
306
  protected
198
307
 
199
308
  def configure_permitted_parameters
200
- devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email) }
309
+ devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, address_attributes: [:country, :state, :city, :area, :postal_code]])
310
+ end
311
+ end
312
+ ```
313
+
314
+ Devise allows you to completely change Devise defaults or invoke custom behavior by passing a block:
315
+
316
+ To permit simple scalar values for username and email, use this
317
+
318
+ ```ruby
319
+ def configure_permitted_parameters
320
+ devise_parameter_sanitizer.permit(:sign_in) do |user_params|
321
+ user_params.permit(:username, :email)
322
+ end
323
+ end
324
+ ```
325
+
326
+ 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:
327
+
328
+ ```ruby
329
+ def configure_permitted_parameters
330
+ devise_parameter_sanitizer.permit(:sign_up) do |user_params|
331
+ user_params.permit({ roles: [] }, :email, :password, :password_confirmation)
201
332
  end
202
333
  end
203
334
  ```
335
+ For the list of permitted scalars, and how to declare permitted keys in nested hashes and arrays, see
336
+
337
+ https://github.com/rails/strong_parameters#nested-parameters
204
338
 
205
- If you have multiple roles, you may want to set up different parameter sanitizer per role. In this case, we recommend inheriting from `Devise::ParameterSanitizer` and add your own logic:
339
+ 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:
206
340
 
207
341
  ```ruby
208
342
  class User::ParameterSanitizer < Devise::ParameterSanitizer
209
- def sign_in
210
- default_params.permit(:username, :email)
343
+ def initialize(*)
344
+ super
345
+ permit(:sign_up, keys: [:username, :email])
211
346
  end
212
347
  end
213
348
  ```
@@ -237,63 +372,113 @@ We built Devise to help you quickly develop an application that uses authenticat
237
372
  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:
238
373
 
239
374
  ```console
240
- rails generate devise:views
375
+ $ rails generate devise:views
241
376
  ```
242
377
 
243
- If you have more than one role in your application (such as "User" and "Admin"), you will notice that Devise uses the same views for all roles. 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".
378
+ 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.
379
+
380
+ 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:
381
+
382
+ ```console
383
+ $ rails generate devise:views users
384
+ ```
244
385
 
245
- 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:
386
+ If you would like to generate only a few sets of views, like the ones for the `registerable` and `confirmable` module,
387
+ you can pass a list of modules to the generator with the `-v` flag.
246
388
 
247
389
  ```console
248
- rails generate devise:views users
390
+ $ rails generate devise:views -v registrations confirmations
249
391
  ```
250
392
 
251
393
  ### Configuring controllers
252
394
 
253
395
  If the customization at the views level is not enough, you can customize each controller by following these steps:
254
396
 
255
- 1. Create your custom controller, for example a `Admins::SessionsController`:
397
+ 1. Create your custom controllers using the generator which requires a scope:
398
+
399
+ ```console
400
+ $ rails generate devise:controllers [scope]
401
+ ```
402
+
403
+ If you specify `users` as the scope, controllers will be created in `app/controllers/users/`.
404
+ And the sessions controller will look like this:
256
405
 
257
406
  ```ruby
258
- class Admins::SessionsController < Devise::SessionsController
407
+ class Users::SessionsController < Devise::SessionsController
408
+ # GET /resource/sign_in
409
+ # def new
410
+ # super
411
+ # end
412
+ ...
259
413
  end
260
414
  ```
261
-
262
- Note that in the above example, the controller needs to be created in the `app/controller/admins/` directory.
415
+ (Use the -c flag to specify a controller, for example: `rails generate devise:controllers users -c=sessions`)
263
416
 
264
417
  2. Tell the router to use this controller:
265
418
 
266
419
  ```ruby
267
- devise_for :admins, :controllers => { :sessions => "admins/sessions" }
420
+ devise_for :users, controllers: { sessions: 'users/sessions' }
268
421
  ```
269
422
 
270
- 3. And since we changed the controller, it won't use the `"devise/sessions"` views, so remember to copy `"devise/sessions"` to `"admin/sessions"`.
423
+ 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`.
424
+
425
+ 4. Finally, change or extend the desired controller actions.
271
426
 
272
- 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.
427
+ You can completely override a controller action:
428
+
429
+ ```ruby
430
+ class Users::SessionsController < Devise::SessionsController
431
+ def create
432
+ # custom sign-in code
433
+ end
434
+ end
435
+ ```
436
+
437
+ Or you can simply add new behavior to it:
438
+
439
+ ```ruby
440
+ class Users::SessionsController < Devise::SessionsController
441
+ def create
442
+ super do |resource|
443
+ BackgroundWorker.trigger(resource)
444
+ end
445
+ end
446
+ end
447
+ ```
448
+
449
+ This is useful for triggering background jobs or logging events during certain actions.
450
+
451
+ 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.
273
452
 
274
453
  ### Configuring routes
275
454
 
276
455
  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:
277
456
 
278
457
  ```ruby
279
- 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' }
458
+ 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' }
280
459
  ```
281
460
 
282
- Be sure to check `devise_for` documentation for details.
461
+ Be sure to check `devise_for` [documentation](http://www.rubydoc.info/github/heartcombo/devise/master/ActionDispatch/Routing/Mapper%3Adevise_for) for details.
283
462
 
284
- 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:
463
+ 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:
285
464
 
286
465
  ```ruby
287
466
  devise_scope :user do
288
- get "sign_in", :to => "devise/sessions#new"
467
+ get 'sign_in', to: 'devise/sessions#new'
289
468
  end
290
469
  ```
291
470
 
292
- 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.
471
+ 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.
472
+
473
+ Please note: You will still need to add `devise_for` in your routes in order to use helper methods such as `current_user`.
474
+
475
+ ```ruby
476
+ devise_for :users, skip: :all
477
+ ```
293
478
 
294
479
  ### I18n
295
480
 
296
- Devise uses flash messages with I18n with the flash keys :notice and :alert. To customize your app, you can set up your locale file:
481
+ 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:
297
482
 
298
483
  ```yaml
299
484
  en:
@@ -329,69 +514,136 @@ en:
329
514
 
330
515
  Take a look at our locale file to check all available messages. You may also be interested in one of the many translations that are available on our wiki:
331
516
 
332
- https://github.com/plataformatec/devise/wiki/I18n
517
+ https://github.com/heartcombo/devise/wiki/I18n
518
+
519
+ Caution: Devise Controllers inherit from ApplicationController. If your app uses multiple locales, you should be sure to set I18n.locale in ApplicationController.
333
520
 
334
521
  ### Test helpers
335
522
 
336
- 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:
523
+ Devise includes some test helpers for controller and integration tests.
524
+ In order to use them, you need to include the respective module in your test
525
+ cases/specs.
526
+
527
+ ### Controller tests
528
+
529
+ Controller tests require that you include `Devise::Test::IntegrationHelpers` on
530
+ your test case or its parent `ActionController::TestCase` superclass.
531
+ For Rails versions prior to 5, include `Devise::Test::ControllerHelpers` instead, since the superclass
532
+ for controller tests was changed to ActionDispatch::IntegrationTest
533
+ (for more details, see the [Integration tests](#integration-tests) section).
534
+
535
+ ```ruby
536
+ class PostsControllerTest < ActionController::TestCase
537
+ include Devise::Test::IntegrationHelpers # Rails >= 5
538
+ end
539
+ ```
337
540
 
338
541
  ```ruby
339
- class ActionController::TestCase
340
- include Devise::TestHelpers
542
+ class PostsControllerTest < ActionController::TestCase
543
+ include Devise::Test::ControllerHelpers # Rails < 5
341
544
  end
342
545
  ```
343
546
 
344
- If you're using RSpec, you can put the following inside a file named `spec/support/devise.rb`:
547
+ If you're using RSpec, you can put the following inside a file named
548
+ `spec/support/devise.rb` or in your `spec/spec_helper.rb` (or
549
+ `spec/rails_helper.rb` if you are using `rspec-rails`):
345
550
 
346
551
  ```ruby
347
552
  RSpec.configure do |config|
348
- config.include Devise::TestHelpers, :type => :controller
553
+ config.include Devise::Test::ControllerHelpers, type: :controller
554
+ config.include Devise::Test::ControllerHelpers, type: :view
349
555
  end
350
556
  ```
351
557
 
352
- Now you are ready to use the `sign_in` and `sign_out` methods. Such methods have the same signature as in controllers:
558
+ Just be sure that this inclusion is made *after* the `require 'rspec/rails'` directive.
559
+
560
+ Now you are ready to use the `sign_in` and `sign_out` methods on your controller
561
+ tests:
562
+
563
+ ```ruby
564
+ sign_in @user
565
+ sign_in @user, scope: :admin
566
+ ```
567
+
568
+ If you are testing Devise internal controllers or a controller that inherits
569
+ from Devise's, you need to tell Devise which mapping should be used before a
570
+ request. This is necessary because Devise gets this information from the router,
571
+ but since controller tests do not pass through the router, it needs to be stated
572
+ explicitly. For example, if you are testing the user scope, simply use:
353
573
 
354
574
  ```ruby
355
- sign_in :user, @user # sign_in(scope, resource)
356
- sign_in @user # sign_in(resource)
575
+ test 'GET new' do
576
+ # Mimic the router behavior of setting the Devise scope through the env.
577
+ @request.env['devise.mapping'] = Devise.mappings[:user]
357
578
 
358
- sign_out :user # sign_out(scope)
359
- sign_out @user # sign_out(resource)
579
+ # Use the sign_in helper to sign in a fixture `User` record.
580
+ sign_in users(:alice)
581
+
582
+ get :new
583
+
584
+ # assert something
585
+ end
360
586
  ```
361
587
 
362
- There are two things that is important to keep in mind:
588
+ ### Integration tests
589
+
590
+ Integration test helpers are available by including the
591
+ `Devise::Test::IntegrationHelpers` module.
363
592
 
364
- 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;
593
+ ```ruby
594
+ class PostsTests < ActionDispatch::IntegrationTest
595
+ include Devise::Test::IntegrationHelpers
596
+ end
597
+ ```
365
598
 
366
- 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:
599
+ Now you can use the following `sign_in` and `sign_out` methods in your integration
600
+ tests:
367
601
 
368
- ```ruby
369
- @request.env["devise.mapping"] = Devise.mappings[:user]
370
- get :new
371
- ```
602
+ ```ruby
603
+ sign_in users(:bob)
604
+ sign_in users(:bob), scope: :admin
372
605
 
373
- ### Omniauth
606
+ sign_out :user
607
+ ```
374
608
 
375
- 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
+ RSpec users can include the `IntegrationHelpers` module on their `:feature` specs.
376
610
 
377
611
  ```ruby
378
- config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
612
+ RSpec.configure do |config|
613
+ config.include Devise::Test::IntegrationHelpers, type: :feature
614
+ end
379
615
  ```
380
616
 
381
- You can read more about Omniauth support in the wiki:
617
+ Unlike controller tests, integration tests do not need to supply the
618
+ `devise.mapping` `env` value, as the mapping can be inferred by the routes that
619
+ are executed in your tests.
382
620
 
383
- * https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview
621
+ You can read more about testing your Rails 3 - Rails 4 controllers with RSpec in the wiki:
622
+
623
+ * https://github.com/heartcombo/devise/wiki/How-To:-Test-controllers-with-Rails-(and-RSpec)
624
+
625
+ ### OmniAuth
626
+
627
+ Devise comes with OmniAuth support out of the box to authenticate with other providers. To use it, simply specify your OmniAuth configuration in `config/initializers/devise.rb`:
628
+
629
+ ```ruby
630
+ config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo'
631
+ ```
632
+
633
+ You can read more about OmniAuth support in the wiki:
634
+
635
+ * https://github.com/heartcombo/devise/wiki/OmniAuth:-Overview
384
636
 
385
637
  ### Configuring multiple models
386
638
 
387
- Devise allows you to set up as many roles as you want. For example, you may have a User model and also want an Admin model with just authentication and timeoutable features. If so, just follow these steps:
639
+ Devise allows you to set up as many Devise models as you want. If you want to have an Admin model with just authentication and timeout features, in addition to the User model above, just run:
388
640
 
389
641
  ```ruby
390
642
  # Create a migration with the required fields
391
643
  create_table :admins do |t|
392
644
  t.string :email
393
645
  t.string :encrypted_password
394
- t.timestamps
646
+ t.timestamps null: false
395
647
  end
396
648
 
397
649
  # Inside your Admin model
@@ -401,7 +653,7 @@ devise :database_authenticatable, :timeoutable
401
653
  devise_for :admins
402
654
 
403
655
  # Inside your protected controller
404
- before_filter :authenticate_admin!
656
+ before_action :authenticate_admin!
405
657
 
406
658
  # Inside your controllers and views
407
659
  admin_signed_in?
@@ -409,44 +661,84 @@ current_admin
409
661
  admin_session
410
662
  ```
411
663
 
412
- On the other hand, you can simply run the generator!
664
+ Alternatively, you can simply run the Devise generator.
665
+
666
+ Keep in mind that those models will have completely different routes. They **do not** and **cannot** share the same controller for sign in, sign out and so on. In case you want to have different roles sharing the same actions, we recommend that you use a role-based approach, by either providing a role column or using a dedicated gem for authorization.
667
+
668
+ ### ActiveJob Integration
669
+
670
+ If you are using Rails 4.2 and ActiveJob to deliver ActionMailer messages in the
671
+ background through a queuing back-end, you can send Devise emails through your
672
+ existing queue by overriding the `send_devise_notification` method in your model.
673
+
674
+ ```ruby
675
+ def send_devise_notification(notification, *args)
676
+ devise_mailer.send(notification, self, *args).deliver_later
677
+ end
678
+ ```
679
+
680
+ ### Password reset tokens and Rails logs
681
+
682
+ If you enable the [Recoverable](http://rubydoc.info/github/heartcombo/devise/master/Devise/Models/Recoverable) module, note that a stolen password reset token could give an attacker access to your application. Devise takes effort to generate random, secure tokens, and stores only token digests in the database, never plaintext. However the default logging behavior in Rails can cause plaintext tokens to leak into log files:
683
+
684
+ 1. Action Mailer logs the entire contents of all outgoing emails to the DEBUG level. Password reset tokens delivered to users in email will be leaked.
685
+ 2. Active Job logs all arguments to every enqueued job at the INFO level. If you configure Devise to use `deliver_later` to send password reset emails, password reset tokens will be leaked.
686
+
687
+ Rails sets the production logger level to INFO by default. Consider changing your production logger level to WARN if you wish to prevent tokens from being leaked into your logs. In `config/environments/production.rb`:
688
+
689
+ ```ruby
690
+ config.log_level = :warn
691
+ ```
692
+
413
693
 
414
694
  ### Other ORMs
415
695
 
416
- Devise supports ActiveRecord (default) and Mongoid. To choose other ORM, you just need to require it in the initializer file.
696
+ Devise supports ActiveRecord (default) and Mongoid. To select another ORM, simply require it in the initializer file.
417
697
 
418
- ## Additional information
698
+ ### Rails API Mode
699
+
700
+ Rails 5+ has a built-in [API Mode](https://edgeguides.rubyonrails.org/api_app.html) which optimizes Rails for use as an API (only). Devise is _somewhat_ able to handle applications that are built in this mode without additional modifications in the sense that it should not raise exceptions and the like. But some issues may still arise during `development`/`testing`, as we still don't know the full extent of this compatibility. (For more information, see [issue #4947](https://github.com/heartcombo/devise/issues/4947/))
419
701
 
420
- ### Heroku
702
+ #### Supported Authentication Strategies
703
+ API-only applications don't support browser-based authentication via cookies, which is devise's default. Yet, devise can still provide authentication out of the box in those cases with the `http_authenticatable` strategy, which uses HTTP Basic Auth and authenticates the user on each request. (For more info, see this wiki article for [How To: Use HTTP Basic Authentication](https://github.com/heartcombo/devise/wiki/How-To:-Use-HTTP-Basic-Authentication))
421
704
 
422
- Using devise on Heroku with Ruby on Rails 3.1 requires setting:
705
+ The devise default for HTTP Auth is disabled, so it will need to be enabled in the devise initializer for the database strategy:
423
706
 
424
707
  ```ruby
425
- config.assets.initialize_on_precompile = false
708
+ config.http_authenticatable = [:database]
426
709
  ```
427
710
 
428
- Read more about the potential issues at http://guides.rubyonrails.org/asset_pipeline.html
711
+ This restriction does not limit you from implementing custom warden strategies, either in your application or via gem-based extensions for devise.
712
+ A common authentication strategy for APIs is token-based authentication. For more information on extending devise to support this type of authentication and others, see the wiki article for [Simple Token Authentication Examples and alternatives](https://github.com/heartcombo/devise/wiki/How-To:-Simple-Token-Authentication-Example#alternatives) or this blog post on [Custom authentication methods with Devise](http://blog.plataformatec.com.br/2019/01/custom-authentication-methods-with-devise/).
713
+
714
+ #### Testing
715
+ API Mode changes the order of the middleware stack, and this can cause problems for `Devise::Test::IntegrationHelpers`. This problem usually surfaces as an ```undefined method `[]=' for nil:NilClass``` error when using integration test helpers, such as `#sign_in`. The solution is simply to reorder the middlewares by adding the following to test.rb:
716
+
717
+ ```ruby
718
+ Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Cookies
719
+ Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Session::CookieStore
720
+ ```
721
+
722
+ For a deeper understanding of this, review [this issue](https://github.com/heartcombo/devise/issues/4696).
723
+
724
+ Additionally be mindful that without views supported, some email-based flows from Confirmable, Recoverable and Lockable are not supported directly at this time.
725
+
726
+ ## Additional information
429
727
 
430
728
  ### Warden
431
729
 
432
730
  Devise is based on Warden, which is a general Rack authentication framework created by Daniel Neighman. We encourage you to read more about Warden here:
433
731
 
434
- https://github.com/hassox/warden
732
+ https://github.com/wardencommunity/warden
435
733
 
436
734
  ### Contributors
437
735
 
438
736
  We have a long list of valued contributors. Check them all at:
439
737
 
440
- https://github.com/plataformatec/devise/contributors
441
-
442
- ### Maintainers
443
-
444
- * José Valim (https://github.com/josevalim)
445
- * Carlos Antônio da Silva (https://github.com/carlosantoniodasilva)
446
- * Rodrigo Flores (https://github.com/rodrigoflores)
738
+ https://github.com/heartcombo/devise/graphs/contributors
447
739
 
448
740
  ## License
449
741
 
450
- MIT License. Copyright 2009-2013 Plataformatec. http://plataformatec.com.br
742
+ MIT License. Copyright 2020 Rafael França, Leonardo Tegon, Carlos Antônio da Silva. Copyright 2009-2019 Plataformatec.
451
743
 
452
- You are not granted rights or licenses to the trademarks of the Plataformatec, including without limitation the Devise name or logo.
744
+ The Devise logo is licensed under [Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License](https://creativecommons.org/licenses/by-nc-nd/4.0/).