lib/google_auth_check.ex

defmodule PlugParadoxAuth.GoogleAuthCheck do
  import Plug.Conn

  def init(options) do
    # initialize options
    options
  end

  def call(conn, _opts) do
    # remove
    clear_session(conn)

    cond do
      get_session(conn, :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