-
Notifications
You must be signed in to change notification settings - Fork 451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug fix: show logout template after logging out #2913
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the benefit of moving this to another controller? We now need to create a separate module to share functionality, causing more indirection. I'd prefer to keep them together without a AuthHelpers. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried that in the first place. But they have different requirements. AuthController's actions requires livebook/lib/livebook_web/controllers/auth_controller.ex Lines 9 to 15 in fa49344
UserAuthContoller.logout does not. After trying to battle that, I ended up with that solution: two different controllers. I actually liked it because I have different concerns. AuthContoller is about "admin authentication". UserAuthController is about "instance/user-identity" based authentication. We have a similar approach with two related plugs. We have AuthPlug that's more related to "admin authentication" and there's UserPlug that's more related to "instance/user-identity" based authentication. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can do action based plugs in Phoenix controllers, I believe it would be:
Or something like that. :) But I see your points about keeping them separate because they deal with different concerns, and I agree. Here are my suggestions:
I think the suggestions above keep your proposals for code organization and remove my concerns, wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree. Done in e3ed654. ✅ |
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 |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FTR
authentication().mode != :disabled
was intentional, the reasoning is that currently we have no way to sign out from Livebook after you authenticate withLIVEBOOK_PASSWORD
. I'm fine either way though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think they have to be different things because when using an identity provider, you still need to log in as admin. We may also want to add a "Logout" button to the menu of deployed apps, which will also trigger this. So having the button doing different things at different stages could be confusing, so I think tying it exclusively to identity for now is the right call.