Skip to Main Content
Programming Phoenix
book

Programming Phoenix

by Chris McCord, Bruce Tate, Jose Valim
April 2016
Beginner to intermediate content levelBeginner to intermediate
300 pages
6h 58m
English
Pragmatic Bookshelf
Content preview from Programming Phoenix

Writing an Authentication Plug

The authentication process works in two stages. First, we’ll store the user ID in the session every time a new user registers or a user logs in. Second, we’ll check if there’s a new user in the session and store it in conn.assigns for every incoming request, so it can be accessed in our controllers and views. Let’s start with the second part because it’s a little easier to follow.

Create a file called web/controllers/auth.ex that looks like this:

 defmodule​ Rumbl.Auth ​do
 import​ Plug.Conn
 
 def​ init(opts) ​do
  Keyword.fetch!(opts, ​:repo​)
 end
 
 def​ call(conn, repo) ​do
  user_id = get_session(conn, ​:user_id​)
  user = user_id && repo.get(Rumbl.User, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Elm

Programming Elm

Jeremy Fairbank
Real-Time Phoenix

Real-Time Phoenix

Stephen Bussey

Publisher Resources

ISBN: 9781680501926Errata Page