Skip to content
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

fix: return to original page after login (#101) #110

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/algora_web/controllers/oauth_login_controller.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule AlgoraWeb.OAuthLoginController do
use AlgoraWeb, :controller
require Logger
import AlgoraWeb.UserAuth, only: [maybe_store_return_to: 1]

def new(conn, %{"provider" => "restream"} = params) do
if conn.assigns.current_user do
Expand All @@ -11,7 +12,9 @@ defmodule AlgoraWeb.OAuthLoginController do
|> put_session(:restream_state, state)
|> redirect(external: Algora.Restream.authorize_url(state))
else
conn |> redirect(to: ~p"/auth/login")
conn
|> maybe_store_return_to()
|> redirect(to: ~p"/auth/login")
end
end
end
6 changes: 4 additions & 2 deletions lib/algora_web/controllers/redirect_controller.ex
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
defmodule AlgoraWeb.RedirectController do
use AlgoraWeb, :controller

import AlgoraWeb.UserAuth, only: [fetch_current_user: 2]
import AlgoraWeb.UserAuth, only: [fetch_current_user: 2, maybe_store_return_to: 1]

plug :fetch_current_user

def redirect_authenticated(conn, _) do
if conn.assigns.current_user do
AlgoraWeb.UserAuth.redirect_if_user_is_authenticated(conn, [])
else
redirect(conn, to: ~p"/auth/login")
conn
|> maybe_store_return_to()
|> redirect(to: ~p"/auth/login")
end
end
end
17 changes: 11 additions & 6 deletions lib/algora_web/controllers/user_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,15 @@ defmodule AlgoraWeb.UserAuth do
end

defp redirect_require_login(socket) do
socket
|> LiveView.put_flash(:error, "Please sign in")
|> LiveView.redirect(to: ~p"/auth/login")
end
conn = LiveView.get_connect_info(socket, :conn)
%{request_path: request_path, query_string: query_string} = conn

return_to = if query_string == "", do: request_path, else: request_path <> "?" <> query_string

socket
|> LiveView.put_flash(:error, "Please sign in")
|> LiveView.redirect(to: ~p"/auth/login?return_to=#{URI.encode_www_form(return_to)}")
end

@doc """
Logs the user in.
Expand Down Expand Up @@ -157,13 +162,13 @@ defmodule AlgoraWeb.UserAuth do
end
end

defp maybe_store_return_to(%{method: "GET"} = conn) do
def maybe_store_return_to(%{method: "GET"} = conn) do
%{request_path: request_path, query_string: query_string} = conn
return_to = if query_string == "", do: request_path, else: request_path <> "?" <> query_string
put_session(conn, :user_return_to, return_to)
end

defp maybe_store_return_to(conn), do: conn
def maybe_store_return_to(conn), do: conn

def signed_in_path(_conn), do: "/"
end
6 changes: 5 additions & 1 deletion lib/algora_web/live/show_live/show.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule AlgoraWeb.ShowLive.Show do
use AlgoraWeb, :live_view
import AlgoraWeb.UserAuth, only: [maybe_store_return_to: 1]

alias Algora.{Shows, Library, Events}
alias Algora.Accounts
Expand Down Expand Up @@ -344,9 +345,12 @@ defmodule AlgoraWeb.ShowLive.Show do

show = Shows.get_show_by_fields!(slug: slug)

conn = get_connect_info(socket, :conn)

cond do
current_user == nil ->
socket
conn
|> maybe_store_return_to()
|> redirect(to: ~p"/auth/login")

current_user.id != show.user_id && !Accounts.admin?(current_user) ->
Expand Down