Search⌘ K
AI Features

Protect Routes with Plugs

Explore how to use plugs in Phoenix to protect web routes by managing authenticated user sessions. Learn to fetch the current user, authenticate login credentials, and handle redirections to secure your application effectively.

The authentication service integrates with the Phoenix stack to provide infrastructure for session management including plugs that we can use in the router to control access to our routes.

Defining the authentication service

The authentication service is defined in the file pento/lib/pento_web/controllers/user_auth.ex. We could open up the code base, but instead, let’s do a quick review in IEx to see what the public API looks like. Fire up the IEx in the terminal below with iex -S mix, and key this in:

Elixir
exports PentoWeb.UserAuth

And, we should see ...

Elixir
fetch_current_user/2
log_in_user/2
log_in_user/3
log_out_user/1
redirect_if_user_is_authenticated/2
require_authenticated_user/2
Terminal 1
Terminal
Loading...

All of ...