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
Forget user when deleting an associated active session
When deleting an active session I want to ensure the associated session is forgotten so that I can ensure my account it safe.
Issues
------
- Closes#78
Copy file name to clipboardexpand all lines: README.md
+121-1
Original file line number
Diff line number
Diff line change
@@ -1595,4 +1595,124 @@ end
1595
1595
1596
1596
> **What's Going On Here?**
1597
1597
>
1598
-
> - This is a very subtle change, but we've added a [safe navigation operator](https://ruby-doc.org/core-2.6/doc/syntax/calling_methods_rdoc.html#label-Safe+navigation+operator) via the `&.user` call. This is because `ActiveSession.find_by(id: session[:current_active_session_id])` can now return `nil` since we're able to delete other `active_session` records.
1598
+
> - This is a very subtle change, but we've added a [safe navigation operator](https://ruby-doc.org/core-2.6/doc/syntax/calling_methods_rdoc.html#label-Safe+navigation+operator) via the `&.user` call. This is because `ActiveSession.find_by(id: session[:current_active_session_id])` can now return `nil` since we're able to delete other `active_session` records.
1599
+
1600
+
## Step 21: Refactor Remember Logic
1601
+
1602
+
Since we're now associating our sessions with an `active_session` and not a `user`, we'll want to remove the `remember_token` token from the `users` table and onto the `active_sessions`.
1603
+
1604
+
1. Move remember_token column from users to active_sessions table.
1605
+
1606
+
```bash
1607
+
rails g migration move_remember_token_from_users_to_active_sessions
> - We add `null: false` to ensure this column always has a value.
1625
+
> - 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
+
1627
+
2. Update User Model.
1628
+
1629
+
```diff
1630
+
class User < ApplicationRecord
1631
+
...
1632
+
- has_secure_password
1633
+
...
1634
+
end
1635
+
```
1636
+
1637
+
3. Update Active Session Model.
1638
+
1639
+
```ruby
1640
+
# app/models/active_session.rb
1641
+
classActiveSession < ApplicationRecord
1642
+
...
1643
+
has_secure_token :remember_token
1644
+
end
1645
+
```
1646
+
1647
+
> **What's Going On Here?**
1648
+
>
1649
+
> - 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
+
> - Note that we remove this from the `user` model.
> - 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.
1691
+
> - 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
+
> - 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
+
1694
+
5. Refactor the Sessions Controller.
1695
+
1696
+
```ruby
1697
+
# app/controllers/sessions_controller.rb
1698
+
classSessionsController < ApplicationController
1699
+
defcreate
1700
+
...
1701
+
if@user
1702
+
if@user.unconfirmed?
1703
+
...
1704
+
else
1705
+
...
1706
+
active_session = login @user
1707
+
remember(active_session) if params[:user][:remember_me] =="1"
1708
+
end
1709
+
else
1710
+
...
1711
+
end
1712
+
end
1713
+
end
1714
+
```
1715
+
1716
+
> **What's Going On Here?**
1717
+
>
1718
+
> - Since the `login` method now returns an `active_session`, we can take that value and pass it to `remember`.
0 commit comments