Skip to content

Commit 39b71e2

Browse files
fix: handle stale login redirect contexts
1 parent 6144a32 commit 39b71e2

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

lib/algora_web/controllers/user_auth.ex

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,25 @@ defmodule AlgoraWeb.UserAuth do
241241
def signed_in_path_from_context(org_handle), do: ~p"/#{org_handle}/dashboard"
242242

243243
def signed_in_path(%User{} = user) do
244-
signed_in_path_from_context(Accounts.last_context(user))
244+
case Accounts.last_context(user) do
245+
"personal" ->
246+
signed_in_path_from_context("personal")
247+
248+
"preview/" <> _ = preview_context ->
249+
signed_in_path_from_context(preview_context)
250+
251+
context ->
252+
case Accounts.get_user_by_handle(context) do
253+
%User{type: :organization, handle: handle} ->
254+
signed_in_path_from_context(handle)
255+
256+
%User{type: :individual} ->
257+
signed_in_path_from_context("personal")
258+
259+
nil ->
260+
signed_in_path_from_context(Accounts.default_context())
261+
end
262+
end
245263
end
246264

247265
def signed_in_path(conn) do

test/algora_web/controllers/user_auth_test.exs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule AlgoraWeb.UserAuthTest do
22
use AlgoraWeb.ConnCase
33

4+
import Algora.Factory
5+
46
alias AlgoraWeb.UserAuth
57

68
describe "verify_login_code/2" do
@@ -143,4 +145,19 @@ defmodule AlgoraWeb.UserAuthTest do
143145
assert result == id
144146
end
145147
end
148+
149+
describe "signed_in_path/1" do
150+
test "falls back to /home when last_context points to a missing handle" do
151+
user = insert!(:user, handle: "zhravan", last_context: "shravan20")
152+
153+
assert UserAuth.signed_in_path(user) == "/home"
154+
end
155+
156+
test "routes org contexts to the org dashboard" do
157+
org = insert!(:organization, handle: "acme")
158+
user = insert!(:user, last_context: org.handle)
159+
160+
assert UserAuth.signed_in_path(user) == "/acme/dashboard"
161+
end
162+
end
146163
end

0 commit comments

Comments
 (0)