lib/google_auth_set.ex

# TODO: ask eric about naming
defmodule PlugParadoxAuth.GoogleAuthSet do
  import Plug.Conn

  def init(options) do
    options
  end

  def call(conn, _opts) do
    %{params: %{"code" => code}} = conn
    {:ok, token} = ElixirAuthGoogle.get_token(code, conn)
    # TODO make sure this is a paradox profile
    {:ok, profile} = ElixirAuthGoogle.get_user_profile(token.access_token)
    IO.puts "blurrrp"
    IO.inspect profile
    IO.inspect profile.email

    url = conn |> fetch_session |> get_session(:google_auth_success_url)

    # this might not be an issue if I use a newer version of phoenix / plug
    # https://github.com/elixir-plug/plug/issues/1017
    conn
    |> fetch_session
    |> put_session(:google_user, profile.email)
    |> put_resp_header("location", url)
    |> send_resp(302, "text/html")
  end
end