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

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