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

(feat) return path to original page #120

Draft
wants to merge 2 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
2 changes: 1 addition & 1 deletion lib/algora_web/components/layouts/live.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<%= if @current_user do %>
<.sidebar_account_dropdown id="mobile-account-dropdown" current_user={@current_user} />
<% else %>
<a class="group outline-none w-full" target="_self" href="/auth/login">
<a class="group outline-none w-full" href={~p"/auth/login?#{[return_to: @uri.path]}">
<div class="flex text-center font-sans justify-center items-center shrink-0 duration-150 select-none group-focus:outline-none group-disabled:opacity-75 group-disabled:pointer-events-none transition bg-indigo-700 hover:bg-indigo-600 disabled:opacity-50 h-10 px-2.5 text-base font-semibold rounded mx-3">
<div class="justify-center flex w-full items-center gap-x-1">
<span class="mx-auto grow self-center truncate px-1 text-gray-100 text-base font-semibold">
Expand Down
2 changes: 1 addition & 1 deletion lib/algora_web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
</div>
<% else %>
<.link
navigate="/auth/login"
href={~p"/auth/login?#{[return_to: @uri.path]}"}
class="rounded-lg bg-gray-50 hover:bg-gray-200 py-1 px-2 text-sm font-semibold leading-6 text-gray-950 active:text-gray-950/80"
>
<span class="relative font-semibold text-sm">Login</span>
Expand Down
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() // stores the return to path in the session
|> redirect(to: ~p"/auth/login")
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/algora_web/controllers/user_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ defmodule AlgoraWeb.UserAuth do
defp redirect_require_login(socket) do
socket
|> LiveView.put_flash(:error, "Please sign in")
|> LiveView.put_session(:user_return_to, socket.assigns.uri.path)
|> LiveView.redirect(to: ~p"/auth/login")
end

Expand Down
7 changes: 5 additions & 2 deletions lib/algora_web/live/sign_in_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule AlgoraWeb.SignInLive do
Algora TV
</h2>
<a
href={Algora.Github.authorize_url()}
href={Algora.Github.authorize_url(@return_to)}
class="mt-8 w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-purple-600 hover:bg-purple-600 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-purple-400"
>
Sign in with GitHub
Expand All @@ -20,6 +20,9 @@ defmodule AlgoraWeb.SignInLive do
end

def mount(_params, _session, socket) do
{:ok, socket}
{:ok,
socket
|> assign(:page_title, "Sign in")
|> assign(:return_to, socket.assigns.uri.query_params["return_to"])}
end
end