I have a session key :identifiedas
that may or may not exist for the user. I want it so that if you go to the login page while this key exists, the server redirects you to the homepage.
I could use an if
to solve this, but it seems like a bad idea compared to having a pattern in another function clause, if it's possible.
def login(conn, %{"username" => username, "password" => password}) do
if Plug.Conn.get_session(conn, :identifiedas) do
conn
|> Flash.put(:notice, "You are already logged in.")
|> redirect(to: "/")
else
# Actually try to login. Elided from example.
end
end
Preferrably I'd like it to be:
def login(%Conn{:something -> %{:identifiedas => _}, _fields) do
conn
|> Flash.put(:notice, "You are already logged in.")
|> redirect(to: "/")
end
def login(conn, %{"username" => username, "password" => password}) do
# Elided
end
Yes. You can use this pattern:
def index(conn = %Plug.Conn{private: %{plug_session: %{identifiedas: _}}}, _params) do
# ...
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With