defmodule Addict.Plugs.Authenticated do
  @moduledoc """
  Authenticated plug can be used to filter actions for users that are
  authenticated.
  """
  import Plug.Conn
  use Phoenix.Controller

  def init(options) do
    options
  end


  @doc """
  Call represents the use of the plug itself.

  When called, it will assign `current_user` to `conn`, so it is
  possible to always retrieve the user via `conn.assigns.current_user`.

  In case the user is not logged in, it will redirect the request to
  the Application :addict :not_logged_in_url page. If none is defined, it will
  redirect to `/error`.
  """
  def call(conn, _) do
    conn = fetch_session(conn)

    if is_logged_in(get_session(conn, :current_user)) do
      assign(conn, :current_user, get_session(conn, :current_user))
    else
      conn |> redirect(to: not_logged_in_url) |> halt
    end
  end

  def is_logged_in(user_session) do
    case user_session do
      nil -> false
      _   -> true
    end
  end

  def not_logged_in_url do
    Application.get_env(:addict, :not_logged_in_url, "/error")
  end

end

defmodule Addict.Plugs.Authenticated do
  @moduledoc """
  Authenticated plug can be used to filter actions for users that are
  authenticated.
  """
  import Plug.Conn

  def init(options) do
    options
  end


  @doc """
  Call represents the use of the plug itself.

  When called, it will assign `current_user` to `conn`, so it is
  possible to always retrieve the user via `conn.assigns.current_user`.

  In case the user is not logged in, it will redirect the request to
  the Application :addict :not_logged_in_url page. If none is defined, it will
  redirect to `/error`.
  """
  def call(conn, _) do
    conn = fetch_session(conn)

    if is_logged_in(get_session(conn, :current_user)) do
      assign(conn, :current_user, get_session(conn, :current_user))
    else
      conn |> Phoenix.Controller.redirect(to: not_logged_in_url) |> halt
    end
  end

  def is_logged_in(user_session) do
    case user_session do
      nil -> false
      _   -> true
    end
  end

  def not_logged_in_url do
    Application.get_env(:addict, :not_logged_in_url, "/error")
  end

end

Call redirect method directly from Phoenix.Controller module
Call redirect method directly from Phoenix.Controller module

By calling "use" we were injecting the whole Controller code inside the plug,
which was not the objective.
trenpixster/addict,jjt/addict,mrluc/addict,trenpixster/addict,breakoutbit/addict,pap/addict,altenwald/addict,suranyami/addict,esbullington/addict