I created a plug
to check if the user already created a profile, and redirect them to the /profiles/new
page with a flash if they not yet have one:
Plug.CheckProfile
case profiles do
nil ->
conn |> put_flash(:info, "No profile found.") |> redirect(to: "/profiles/new") |> halt()
profiles ->
assign(conn, :profiles, profiles)
end
but I read somewhere that put_flash
is normally for the controller,
what is the proper way of doing this?
Is there an alternative for plug
?
A controller is just another plug. This isn't a super-common use case -- you're allowed some leeway in how to implement the behavior. A plug/middleware seems like a clean solution. At its heart, it isn't all that different from the conditional redirects you see that vary depending on whether a user is logged in or not.