Skip to content

Commit 03bc311

Browse files
Proof README
1 parent 66b932f commit 03bc311

File tree

4 files changed

+58
-24
lines changed

4 files changed

+58
-24
lines changed

README.md

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ end
855855
# app/mailers/user_mailer.rb
856856
class UserMailer < ApplicationMailer
857857

858-
def confirmation(user)
858+
def confirmation(user, confirmation_token)
859859
...
860860
mail to: @user.confirmable_email, subject: "Confirmation Instructions"
861861
end
@@ -1195,10 +1195,12 @@ module Authentication
11951195
...
11961196
end
11971197
...
1198+
private
1199+
...
11981200
def store_location
11991201
session[:user_return_to] = request.original_url if request.get? && request.local?
12001202
end
1201-
...
1203+
12021204
end
12031205
```
12041206

@@ -1461,7 +1463,7 @@ end
14611463
6. Update account page.
14621464

14631465
```html+ruby
1464-
<!-- app/views/users/edit.html.erb -->
1466+
<!-- app/views/users/edit.html.erb -->
14651467
...
14661468
<h2>Current Logins</h2>
14671469
<% if @active_sessions.any? %>
@@ -1512,8 +1514,6 @@ class ActiveSessionsController < ApplicationController
15121514
end
15131515

15141516
def destroy_all
1515-
current_user
1516-
15171517
current_user.active_sessions.destroy_all
15181518
reset_session
15191519

@@ -1566,6 +1566,7 @@ end
15661566
```
15671567

15681568
```html+ruby
1569+
<!-- app/views/active_sessions/_active_session.html.erb -->
15691570
<tr>
15701571
<td><%= active_session.user_agent %></td>
15711572
<td><%= active_session.ip_address %></td>
@@ -1619,22 +1620,28 @@ class MoveRememberTokenFromUsersToActiveSessions < ActiveRecord::Migration[6.1]
16191620
end
16201621
```
16211622

1623+
2. Run migration.
1624+
1625+
```bash
1626+
rails db:migrate
1627+
```
1628+
16221629
> **What's Going On Here?**
16231630
>
16241631
> - We add `null: false` to ensure this column always has a value.
16251632
> - We add a [unique index](https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html#method-i-index) to ensure this column has unique data.
16261633
1627-
2. Update User Model.
1634+
3. Update User Model.
16281635

16291636
```diff
16301637
class User < ApplicationRecord
16311638
...
1632-
- has_secure_password
1639+
- has_secure_token :remember_token
16331640
...
16341641
end
16351642
```
16361643

1637-
3. Update Active Session Model.
1644+
4. Update Active Session Model.
16381645

16391646
```ruby
16401647
# app/models/active_session.rb
@@ -1649,7 +1656,7 @@ end
16491656
> - 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.
16501657
> - Note that we remove this from the `user` model.
16511658
1652-
4. Refactor the Authentication Concern.
1659+
5. Refactor the Authentication Concern.
16531660

16541661
```ruby
16551662
# app/controllers/concerns/authentication.rb
@@ -1663,7 +1670,7 @@ module Authentication
16631670
active_session
16641671
end
16651672

1666-
def forget(user)
1673+
def forget_active_session
16671674
cookies.delete :remember_token
16681675
end
16691676
...
@@ -1687,11 +1694,11 @@ end
16871694
> **What's Going On Here?**
16881695
>
16891696
> - 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.
16911698
> - 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.
16921699
> - 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).
16931700
1694-
5. Refactor the Sessions Controller.
1701+
6. Refactor the Sessions Controller.
16951702

16961703
```ruby
16971704
# app/controllers/sessions_controller.rb
@@ -1710,9 +1717,39 @@ class SessionsController < ApplicationController
17101717
...
17111718
end
17121719
end
1720+
1721+
def destroy
1722+
forget_active_session
1723+
...
1724+
end
17131725
end
17141726
```
17151727

17161728
> **What's Going On Here?**
17171729
>
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.
1732+
1733+
7. Refactor Active Sessions Controller
1734+
1735+
```ruby
1736+
# app/controllers/active_sessions_controller.rb
1737+
class ActiveSessionsController < ApplicationController
1738+
...
1739+
def destroy
1740+
...
1741+
if current_user
1742+
...
1743+
else
1744+
forget_active_session
1745+
...
1746+
end
1747+
end
1748+
1749+
def destroy_all
1750+
forget_active_session
1751+
current_user.active_sessions.destroy_all
1752+
...
1753+
end
1754+
end
1755+
```

app/controllers/active_sessions_controller.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@ class ActiveSessionsController < ApplicationController
22
before_action :authenticate_user!
33

44
def destroy
5-
user = current_user
65
@active_session = current_user.active_sessions.find(params[:id])
76

87
@active_session.destroy
98

109
if current_user
1110
redirect_to account_path, notice: "Session deleted."
1211
else
13-
forget(user)
12+
forget_active_session
1413
reset_session
1514
redirect_to root_path, notice: "Signed out."
1615
end
1716
end
1817

1918
def destroy_all
20-
current_user
21-
22-
forget(current_user)
19+
forget_active_session
2320
current_user.active_sessions.destroy_all
2421
reset_session
2522

app/controllers/concerns/authentication.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def login(user)
2020
active_session
2121
end
2222

23-
def forget(user)
23+
def forget_active_session
2424
cookies.delete :remember_token
2525
end
2626

@@ -38,10 +38,6 @@ def remember(active_session)
3838
cookies.permanent.encrypted[:remember_token] = active_session.remember_token
3939
end
4040

41-
def store_location
42-
session[:user_return_to] = request.original_url if request.get? && request.local?
43-
end
44-
4541
private
4642

4743
def current_user
@@ -55,4 +51,8 @@ def current_user
5551
def user_signed_in?
5652
Current.user.present?
5753
end
54+
55+
def store_location
56+
session[:user_return_to] = request.original_url if request.get? && request.local?
57+
end
5858
end

app/controllers/sessions_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def create
2020
end
2121

2222
def destroy
23-
forget(current_user)
23+
forget_active_session
2424
logout
2525
redirect_to root_path, notice: "Signed out."
2626
end

0 commit comments

Comments
 (0)