You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -1619,22 +1620,28 @@ class MoveRememberTokenFromUsersToActiveSessions < ActiveRecord::Migration[6.1]
1619
1620
end
1620
1621
```
1621
1622
1623
+
2. Run migration.
1624
+
1625
+
```bash
1626
+
rails db:migrate
1627
+
```
1628
+
1622
1629
> **What's Going On Here?**
1623
1630
>
1624
1631
> - We add `null: false` to ensure this column always has a value.
1625
1632
> - We add a [unique index](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html#method-i-index) to ensure this column has unique data.
1626
1633
1627
-
2. Update User Model.
1634
+
3. Update User Model.
1628
1635
1629
1636
```diff
1630
1637
class User < ApplicationRecord
1631
1638
...
1632
-
-has_secure_password
1639
+
-has_secure_token :remember_token
1633
1640
...
1634
1641
end
1635
1642
```
1636
1643
1637
-
3. Update Active Session Model.
1644
+
4. Update Active Session Model.
1638
1645
1639
1646
```ruby
1640
1647
# app/models/active_session.rb
@@ -1649,7 +1656,7 @@ end
1649
1656
> - We call [has_secure_token](https://api.rubyonrails.org/classes/ActiveRecord/SecureToken/ClassMethods.html#method-i-has_secure_token) on the `remember_token`. This ensures that the value for this column will be set when the record is created. This value will be used later to securely identify the user.
1650
1657
> - Note that we remove this from the `user` model.
1651
1658
1652
-
4. Refactor the Authentication Concern.
1659
+
5. Refactor the Authentication Concern.
1653
1660
1654
1661
```ruby
1655
1662
# app/controllers/concerns/authentication.rb
@@ -1663,7 +1670,7 @@ module Authentication
1663
1670
active_session
1664
1671
end
1665
1672
1666
-
defforget(user)
1673
+
defforget_active_session
1667
1674
cookies.delete :remember_token
1668
1675
end
1669
1676
...
@@ -1687,11 +1694,11 @@ end
1687
1694
> **What's Going On Here?**
1688
1695
>
1689
1696
> - The `login` method now returns the `active_session`. This will be used later when calling `SessionsController#create`.
1690
-
> - The `forget` method simply deletes the `cookie`. We don't need to call `active_session.regenerate_remember_token` since the `active_session` will be deleted, and therefor cannot be referenced again.
1697
+
> - The `forget` method has been renamed to `forget_active_session` and no longer takes any arguments. This method simply deletes the `cookie`. We don't need to call `active_session.regenerate_remember_token` since the `active_session` will be deleted, and therefor cannot be referenced again.
1691
1698
> - The `remember` method now accepts an `active_session` and not a `user`. We do not need to call `active_session.regenerate_remember_token` since a new `active_session` record will be created each time a user logs in. Note that we now save `active_session.remember_token` to the cookie.
1692
1699
> - The `current_user` method now finds the `active_session` record if the `remember_token` is present and returns the user via the [safe navigation operator](https://ruby-doc.org/core-2.6/doc/syntax/calling_methods_rdoc.html#label-Safe+navigation+operator).
1693
1700
1694
-
5. Refactor the Sessions Controller.
1701
+
6. Refactor the Sessions Controller.
1695
1702
1696
1703
```ruby
1697
1704
# app/controllers/sessions_controller.rb
@@ -1710,9 +1717,39 @@ class SessionsController < ApplicationController
1710
1717
...
1711
1718
end
1712
1719
end
1720
+
1721
+
defdestroy
1722
+
forget_active_session
1723
+
...
1724
+
end
1713
1725
end
1714
1726
```
1715
1727
1716
1728
> **What's Going On Here?**
1717
1729
>
1718
-
> - Since the `login` method now returns an `active_session`, we can take that value and pass it to `remember`.
1730
+
> - Since the `login` method now returns an `active_session`, we can take that value and pass it to `remember`.
1731
+
> - We replace `forget(current_user)` with `forget_active_session` to reflect changes to the method name and structure.
0 commit comments