lib/google_auth_check.ex

defmodule PlugParadoxAuth.GoogleAuthCheck do
  import Plug.Conn

  def init(options) do
    options
  end

  def call(conn, _opts) do
    google_user = conn |> fetch_session |> get_session(:google_user)

    cond do
      google_user -> conn
      conn.request_path == "/auth/google/callback" -> conn
      true -> handle_redirect(conn)
    end
  end

  def handle_redirect(conn) do
    conn
    |> put_session(:google_auth_success_url, conn.request_path)
    |> put_resp_header("location", ElixirAuthGoogle.generate_oauth_url(conn))
    |> send_resp(302, "text/html")
  end
end