title: How to require login in Elixir Phoenix application?
body: <p>I'm using the <a href="https://github.com/scrogson/oauth2_example" rel="nofollow">OAuth2 example</a> (with Google) for authenticating my Phoenix web application.  Though my site requires the user always be logged in or authenticated.  There's no user registration or anonymous access.</p>
  
  <p>I've got the OAuth2 part working using Google as the provider.  Though I'm not sure how to require that the user is redirected to the login page if they're not logged in.</p>
  
  <p><strong>Can anyone provide a working code example?</strong></p>
  
  <p>Here's my <code>web\router.ex</code>:</p>
  
  <pre><code>defmodule MyApp.Router do
    use MyApp.Web, :router
  
    pipeline :browser do
      plug :accepts, ["html"]
      plug :fetch_session
      plug :fetch_flash
      plug :protect_from_forgery
      plug :put_secure_browser_headers
      plug :assign_current_user
    end
  
    pipeline :api do
      plug :accepts, ["json"]
    end
  
    scope "/", MyApp do
      pipe_through :browser # Use the default browser stack
      get "/users", UserController, :index
      get "/users/:id", UserController, :show
      get "/", PageController, :index
    end
  
    scope "/auth", MyApp do
      pipe_through :browser # Use the default browser stack
      get "/:provider", AuthController, :index
      get "/:provider/callback", AuthController, :callback
      delete "/logout", AuthController, :delete
    end
  
    # Other scopes may use custom stacks.
    # scope "/api", MyApp do
    #   pipe_through :api
    # end
  
    # Fetch the current user from the session and add it to `conn.assigns`. This
    # will allow you to have access to the current user in your views with
    # `@current_user`.
    defp assign_current_user(conn, _) do
      assign(conn, :current_user, get_session(conn, :current_user))
    end
  end
  </code></pre>
  
  <hr>
  
  <p><strong>UPDATE 05/23/2016</strong></p>
  
  <p>Using part of the selected answer below, I got this working by modifying my <code>web\router.ex</code>:</p>
  
  <pre><code>defmodule MyApp.Router do
    use MyApp.Web, :router
  
    pipeline :browser do
      plug :accepts, ["html"]
      plug :fetch_session
      plug :fetch_flash
      plug :protect_from_forgery
      plug :put_secure_browser_headers
      plug :assign_current_user
    end
  
    pipeline :authenticated do
      plug :authenticate
    end
  
    pipeline :api do
      plug :accepts, ["json"]
    end
  
    scope "/", MyApp do
      pipe_through [:browser, :authenticated] # Use the default browser stack
  
      get "/", PageController, :index
      resources "/users", UserController, only: [:index, :show, :new, :create]
    end
  
    scope "/auth", MyApp do
      pipe_through :browser # Use the default browser stack
  
      get "/:provider", AuthController, :index
      get "/:provider/callback", AuthController, :callback
      delete "/logout", AuthController, :delete
    end
  
    # Other scopes may use custom stacks.
    # scope "/api", MyApp do
    #   pipe_through :api
    # end
  
    # Fetch the current user from the session and add it to `conn.assigns`. This
    # will allow you to have access to the current user in your views with
    # `@current_user`.
    defp assign_current_user(conn, _params) do
      assign(conn, :current_user, get_session(conn, :current_user))
    end
  
    defp authenticate(conn, _params) do
      if conn.assigns.current_user do
        conn
      else
        conn
        |&gt; put_flash(:error, "You must be logged in.")
        |&gt; redirect(to: "/auth/google")
        |&gt; halt()
      end
    end
  end
  </code></pre>