Skip to content

Commit 7d80ccb

Browse files
[skip ci] Description added for ActiveRecord::Base.generates_token_for method in 7.1 release note
[skip ci] Added PR reference [skip ci] review changes updated [skip ci] review changes updated
1 parent fea9ad3 commit 7d80ccb

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

guides/source/7_1_release_notes.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,37 @@ User.normalize_value_for(:phone, "+1 (555) 867-5309") # => "5558675309"
8888

8989
### Add `ActiveRecord::Base.generates_token_for`
9090

91-
TODO: Add description https://github.com/rails/rails/pull/44189
91+
A new [method `generates_token_for`](https://github.com/rails/rails/pull/44189) has been introduced
92+
to `ActiveRecord::Base`. This feature allows you to generate tokens that can embed data from a record.
93+
These tokens are particularly useful for tasks like password resets.
94+
95+
With `generates_token_for`, tokens can be designed to reflect record state, making it possible to embed
96+
specific record data within the token itself. When utilizing the token to retrieve the associated record,
97+
a comparison is performed between the data in the token and the current data in the record. If the two
98+
sets of data do not match, the token is considered invalid, similar to an expired token.
99+
100+
Here's an example of how this feature can be used:
101+
102+
```ruby
103+
class User < ActiveRecord::Base
104+
has_secure_password
105+
106+
generates_token_for :password_reset, expires_in: 15.minutes do
107+
# A password's BCrypt salt changes when the password is updated.
108+
# By embedding (part of) the salt in a token, the token will
109+
# expire when the password is updated.
110+
BCrypt::Password.new(password_digest).salt[-10..]
111+
end
112+
end
113+
114+
user = User.first
115+
token = user.generate_token_for(:password_reset)
116+
117+
User.find_by_token_for(:password_reset, token) # => user
118+
119+
user.update!(password: "new password")
120+
User.find_by_token_for(:password_reset, token) # => nil
121+
```
92122

93123
### Add `perform_all_later` to enqueue multiple jobs at once
94124

0 commit comments

Comments
 (0)