Phoenix Auth Has never been so simple
I already written how to add social authentication in phoenix
But for email password authenitcation Its amazingly simple now.
open mix.exs and add deps.
{:phx_gen_auth, “~> 0.6.0”}
Then run,
mix phx.gen.auth Accounts User users
mix ecto.migrate
and thats it. We have full fledge authentication system.
run the server and try it in the browser
http://localhost:4000/users/log_in
you should see login page.
Bonus:
To guard a routes we can pass scope through the require_authenticated_user
plug. It is already in our route file generate by phx_gen_auth
pipe_through [:browser, :require_authenticated_user]
To guard a specific action in controller
import TailwindWeb.UserAuth, only: [require_authenticated_user: 2]plug :require_authenticated_user when action in [:my_rooms, :new, :edit, :delete]