Restricting access via sessions

We have some tests in place to make sure that our code works well enough, but we haven't done anything to actually secure the creation of new polls in our application. We want to make sure that the user is actually logged in - and if they're not, we need to be able to redirect them to the root page with a message explaining the situation. For this, we're going to write a Plug module so that we can reuse this code anywhere we need it. We'll call this VerifyUserSession, as that is the intended use case of the plug. Let's create lib/vocial_web/verify_user_session.ex and give it a starting skeleton, shown as follows:

defmodule VocialWeb.VerifyUserSession do  def init(opts), do: opts  def call(conn, _opts) do conn ...

Get Phoenix Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.