-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show logout template after logging out
- Loading branch information
1 parent
f20b2c6
commit fa49344
Showing
8 changed files
with
72 additions
and
30 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
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,17 @@ | ||
defmodule LivebookWeb.AuthHelpers do | ||
use LivebookWeb, :controller | ||
|
||
def redirect_to(conn) do | ||
conn | ||
|> then(fn conn -> | ||
if redirect_to = get_session(conn, :redirect_to) do | ||
conn | ||
|> delete_session(:redirect_to) | ||
|> redirect(to: redirect_to) | ||
else | ||
redirect(conn, to: ~p"/") | ||
end | ||
end) | ||
|> halt() | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule LivebookWeb.UserAuthController do | ||
use LivebookWeb, :controller | ||
alias LivebookWeb.AuthHelpers | ||
|
||
def logout(conn, _params) do | ||
if get_session(conn, :user_id) do | ||
conn | ||
|> configure_session(renew: true) | ||
|> clear_session() | ||
|> render("logout.html") | ||
else | ||
AuthHelpers.redirect_to(conn) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule LivebookWeb.UserAuthHTML do | ||
use LivebookWeb, :html | ||
|
||
embed_templates "user_auth_html/*" | ||
end |
File renamed without changes.
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
29 changes: 29 additions & 0 deletions
29
test/livebook_web/controllers/user_auth_controller_test.exs
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,29 @@ | ||
defmodule LivebookWeb.UserAuthControllerTest do | ||
use LivebookWeb.ConnCase, async: true | ||
|
||
describe "GET /logout" do | ||
test "renders logout template when logged in", %{conn: conn} do | ||
conn = login_user(conn) | ||
|
||
conn = get(conn, ~p"/logout") | ||
|
||
assert html_response(conn, 200) =~ "You have been logged out" | ||
end | ||
|
||
test "redirects when already logged out", %{conn: conn} do | ||
conn = logout_user(conn) | ||
|
||
conn = get(conn, ~p"/logout") | ||
|
||
assert redirected_to(conn) == ~p"/" | ||
end | ||
|
||
defp login_user(conn) do | ||
Phoenix.ConnTest.init_test_session(conn, %{user_id: 1}) | ||
end | ||
|
||
defp logout_user(conn) do | ||
Phoenix.ConnTest.init_test_session(conn, %{}) | ||
end | ||
end | ||
end |