Skip to content
Merged
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
23 changes: 15 additions & 8 deletions lib/algora/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ defmodule Algora.Accounts do
{:ok, Repo.preload(user, :identities, force: true)}
end

def last_context(%{last_context: nil} = user) do
def last_context(%User{last_context: nil} = user) do
orgs = Organizations.get_user_orgs(user)

last_debit_query =
Expand All @@ -378,15 +378,17 @@ defmodule Algora.Accounts do
where: t.type == :debit,
where: u.id in ^Enum.map(orgs, & &1.id),
order_by: [desc: t.succeeded_at],
limit: 1
limit: 1,
select_merge: %{user: u}
)

last_bounty_query =
from(b in Bounty,
join: c in assoc(b, :creator),
where: c.id in ^Enum.map(orgs, & &1.id),
order_by: [desc: b.created_at],
limit: 1
limit: 1,
select_merge: %{creator: c}
)

last_sponsored_on_behalf_of =
Expand All @@ -396,13 +398,18 @@ defmodule Algora.Accounts do
true -> nil
end

case last_sponsored_on_behalf_of do
%{type: :organization} -> last_sponsored_on_behalf_of.handle
_ -> default_context()
end
last_context =
case last_sponsored_on_behalf_of do
%{type: :organization} -> last_sponsored_on_behalf_of.handle
_ -> default_context()
end

update_settings(user, %{last_context: last_context})

last_context
end

def last_context(%{last_context: last_context}), do: last_context
def last_context(%User{last_context: last_context}), do: last_context

def default_context, do: "personal"

Expand Down
13 changes: 9 additions & 4 deletions lib/algora_web/controllers/context_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ defmodule AlgoraWeb.ContextController do
alias AlgoraWeb.UserAuth

def set(conn, %{"context" => context}) do
{:ok, _updated_user} =
Accounts.update_settings(conn.assigns.current_user, %{last_context: context})
# TODO: validate context is accessible by user

conn = put_session(conn, :last_context, context)
conn =
case Accounts.update_settings(conn.assigns.current_user, %{last_context: context}) do
{:ok, user} -> assign(conn, :current_user, user)
{:error, _} -> conn
end

redirect(conn, to: UserAuth.signed_in_path(conn))
conn
|> put_session(:last_context, context)
|> redirect(to: UserAuth.signed_in_path_from_context(context))
end
end
10 changes: 6 additions & 4 deletions lib/algora_web/controllers/oauth_callback_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ defmodule AlgoraWeb.OAuthCallbackController do
|> render(:success)

:redirect ->
conn
|> put_flash(:info, welcome_message(user))
|> AlgoraWeb.UserAuth.put_current_user(user)
|> redirect(to: data[:return_to] || AlgoraWeb.UserAuth.signed_in_path(conn))
conn =
conn
|> put_flash(:info, welcome_message(user))
|> AlgoraWeb.UserAuth.put_current_user(user)

redirect(conn, to: data[:return_to] || AlgoraWeb.UserAuth.signed_in_path(conn))
end
else
{:error, reason} ->
Expand Down
13 changes: 11 additions & 2 deletions lib/algora_web/controllers/user_auth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ defmodule AlgoraWeb.UserAuth do
conn
|> renew_session()
|> put_session(:user_id, user.id)
|> put_session(:last_context, user.last_context)
|> put_session(:last_context, Accounts.last_context(user))
|> put_session(:live_socket_id, "users_sessions:#{user.id}")
end

Expand Down Expand Up @@ -194,7 +194,16 @@ defmodule AlgoraWeb.UserAuth do
end

def signed_in_path(conn) do
signed_in_path_from_context(get_session(conn, :last_context) || Accounts.default_context())
cond do
last_context = get_session(conn, :last_context) ->
signed_in_path_from_context(last_context)

user = conn.assigns[:current_user] ->
signed_in_path(user)

true ->
signed_in_path_from_context(Accounts.default_context())
end
end

defp login_code_ttl, do: 3600
Expand Down