- Published
- Author
- Aditya VishwakarmaSystem Analyst
Rails provides a https://api.rubyonrails.org/classes/ActiveRecord/TokenFor.html#method-i-generate_token_for|generates_token_for method on Active Record models, which can generate unique tokens for records — including support for expiration. This is useful for scenarios like creating unique unsubscribe links in emails. You can later retrieve a record using the generated token with the corresponding https://api.rubyonrails.org/classes/ActiveRecord/TokenFor/RelationMethods.html#method-i-find_by_token_for|find_by_token_for method.
The Rails Authentication generator utilizes this feature in its password reset flow. It uses https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html#method-i-has_secure_password|has_secure_password, which by default enables password reset functionality for the user model. When you call the
By default, https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html#method-i-has_secure_password|has_secure_password uses the password attribute, but you can customize this by specifying a different attribute via the
#rubyonrails #ror #authentication #token #password
The Rails Authentication generator utilizes this feature in its password reset flow. It uses https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html#method-i-has_secure_password|has_secure_password, which by default enables password reset functionality for the user model. When you call the
password_reset_token instance method on a user, it internally uses generates_token_for to generate a unique token. You can then find the user record later using find_by_password_reset_token.By default, https://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html#method-i-has_secure_password|has_secure_password uses the password attribute, but you can customize this by specifying a different attribute via the
attribute option. If you want to disable password reset functionality, you can pass reset_token: false to has_secure_password (it is enabled by default). Similarly, you can disable password confirmation validations by passing validations: false (validations are enabled by default).#rubyonrails #ror #authentication #token #password