-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow user to manually destroy specific sessions
When viewing my active sessions I want to be able to destroy a specific session so that I can keep my account secure. Issues ------ - Closes #70
- Loading branch information
1 parent
8005f1b
commit e68248c
Showing
11 changed files
with
226 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the styles related to the active_sessions controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: https://sass-lang.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class ActiveSessionsController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
def destroy | ||
@active_session = current_user.active_sessions.find(params[:id]) | ||
|
||
@active_session.destroy | ||
|
||
if current_user | ||
redirect_to account_path, notice: "Session deleted." | ||
else | ||
reset_session | ||
redirect_to root_path, notice: "Signed out." | ||
end | ||
end | ||
|
||
def destroy_all | ||
current_user | ||
|
||
current_user.active_sessions.destroy_all | ||
reset_session | ||
|
||
redirect_to root_path, notice: "Signed out." | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ActiveSessionsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
<td><%= active_session.user_agent %></td> | ||
<td><%= active_session.ip_address %></td> | ||
<td><%= active_session.created_at %></td> | ||
<tr> | ||
<td><%= active_session.user_agent %></td> | ||
<td><%= active_session.ip_address %></td> | ||
<td><%= active_session.created_at %></td> | ||
<td><%= button_to "Sign Out", active_session_path(active_session), method: :delete %></td> | ||
</tr> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
require "test_helper" | ||
|
||
class ActiveSessionsControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@confirmed_user = User.create!(email: "confirmed_user@example.com", password: "password", password_confirmation: "password", confirmed_at: Time.current) | ||
end | ||
|
||
test "should destroy all active sessions" do | ||
login @confirmed_user | ||
@confirmed_user.active_sessions.create! | ||
|
||
assert_difference("ActiveSession.count", -2) do | ||
delete destroy_all_active_sessions_path | ||
end | ||
|
||
assert_redirected_to root_path | ||
assert_nil current_user | ||
assert_not_nil flash[:notice] | ||
end | ||
|
||
test "should destroy another session" do | ||
login @confirmed_user | ||
@confirmed_user.active_sessions.create! | ||
|
||
assert_difference("ActiveSession.count", -1) do | ||
delete active_session_path(@confirmed_user.active_sessions.last) | ||
end | ||
|
||
assert_redirected_to account_path | ||
assert_not_nil current_user | ||
assert_not_nil flash[:notice] | ||
end | ||
|
||
test "should destroy current session" do | ||
login @confirmed_user | ||
|
||
assert_difference("ActiveSession.count", -1) do | ||
delete active_session_path(@confirmed_user.active_sessions.last) | ||
end | ||
|
||
assert_redirected_to root_path | ||
assert_nil current_user | ||
assert_not_nil flash[:notice] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters