From 39b71e2565fe2b4c31b57bfac61317a0313d5bcd Mon Sep 17 00:00:00 2001 From: Tomas Pinkas Date: Wed, 20 May 2026 20:32:58 +0200 Subject: [PATCH] fix: handle stale login redirect contexts --- lib/algora_web/controllers/user_auth.ex | 20 ++++++++++++++++++- .../algora_web/controllers/user_auth_test.exs | 17 ++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/algora_web/controllers/user_auth.ex b/lib/algora_web/controllers/user_auth.ex index e3f00b465..110330213 100644 --- a/lib/algora_web/controllers/user_auth.ex +++ b/lib/algora_web/controllers/user_auth.ex @@ -241,7 +241,25 @@ defmodule AlgoraWeb.UserAuth do def signed_in_path_from_context(org_handle), do: ~p"/#{org_handle}/dashboard" def signed_in_path(%User{} = user) do - signed_in_path_from_context(Accounts.last_context(user)) + case Accounts.last_context(user) do + "personal" -> + signed_in_path_from_context("personal") + + "preview/" <> _ = preview_context -> + signed_in_path_from_context(preview_context) + + context -> + case Accounts.get_user_by_handle(context) do + %User{type: :organization, handle: handle} -> + signed_in_path_from_context(handle) + + %User{type: :individual} -> + signed_in_path_from_context("personal") + + nil -> + signed_in_path_from_context(Accounts.default_context()) + end + end end def signed_in_path(conn) do diff --git a/test/algora_web/controllers/user_auth_test.exs b/test/algora_web/controllers/user_auth_test.exs index a52af647e..987d4c56b 100644 --- a/test/algora_web/controllers/user_auth_test.exs +++ b/test/algora_web/controllers/user_auth_test.exs @@ -1,6 +1,8 @@ defmodule AlgoraWeb.UserAuthTest do use AlgoraWeb.ConnCase + import Algora.Factory + alias AlgoraWeb.UserAuth describe "verify_login_code/2" do @@ -143,4 +145,19 @@ defmodule AlgoraWeb.UserAuthTest do assert result == id end end + + describe "signed_in_path/1" do + test "falls back to /home when last_context points to a missing handle" do + user = insert!(:user, handle: "zhravan", last_context: "shravan20") + + assert UserAuth.signed_in_path(user) == "/home" + end + + test "routes org contexts to the org dashboard" do + org = insert!(:organization, handle: "acme") + user = insert!(:user, last_context: org.handle) + + assert UserAuth.signed_in_path(user) == "/acme/dashboard" + end + end end