Well, usually you would lay it out as a pipeline, because as you are doing it you’re not passing the conn
you changed unless you re-assing it with conn = ...
def create(conn, %{"session" => session_params}) do
case AetherWars.Session.login(session_params, AetherWars.Repo) do
{:ok, user} ->
conn
|> put_session(:current_user, user)
|> put_session(:bearer, create_token(user.id))
|> put_flash(:info, "Logged in")
|> redirect(to: "/")
{:error, message} ->
conn
|> put_flash(:info, message)
|> render("new.html")
end
end
So the last redirect()
or render()
will receive that changed conn
If you do
put_session(conn, :something, something)
render conn, "something.html"
You’re saying render
with this conn
object that wasn’t changed.
If you do
put_session(conn, :something, something)
|> render "something.html"
You’re passing through the |>
the previous element to the render
function. It’s still render/2
, but you omit the first parameter as it’s being passed through the pipe (|>
)
On the last line of the “pipe” you could then pass the conn that you’ve mutated before, so that it can decide the redirect.
So perhaps something like (it’s just illustrative):
def login_function(.....) do
conn
|> put_session(:bearer, create_token(user.id))
|> put_flash(:info, "Logged in")
|> decide_redirect()
end
def decide_redirect(conn) do
case get_session(conn, :on_payment) do
nil -> redirect(conn, to: "/")
url -> redirect(conn, to: url)
end
end
Regarding transforming data on your view templates, don’t do it. The template (view layer) should only be concerned with presenting things. While the controller is concerned in transforming (or calling modules that transform) the data and then calling the appropriate template.