<commit_before>defmodule EspiDni.AuthController do
  use EspiDni.Web, :controller
  alias EspiDni.SlackAuthHandler
  alias EspiDni.GoogleAuthHandler
  plug Ueberauth

  def request(conn, _params) do
    render(conn, "request.html", callback_url: Helpers.callback_url(conn))
  end

  def delete(conn, _params) do
    conn
    |> put_flash(:info, "You have been logged out!")
    |> configure_session(drop: true)
    |> redirect(to: "/")
  end

  def callback(%{assigns: %{ueberauth_failure: _fails}} = conn, _params) do
    conn
    |> put_flash(:error, "Failed to authenticate.")
    |> redirect(to: "/")
  end

  def callback(%{assigns: %{ueberauth_auth: auth}} = conn, %{"provider" => "slack"} = _params) do
    case SlackAuthHandler.init_from_auth(auth) do
      {:ok, team, user} ->
        conn
        |> put_flash(:info, "Successfully authenticated.")
        |> put_session(:current_team, team)
        |> put_session(:current_user, user)
        |> start_bot
        |> redirect(to: "/")
      {:error, reason} ->
        conn
        |> put_flash(:error, reason)
        |> redirect(to: "/")
    end
  end

  def callback(%{assigns: %{ueberauth_auth: auth}} = conn, %{"provider" => "google"} = _params) do
    team = get_session(conn, :current_team)

    case GoogleAuthHandler.update_from_auth(team, auth) do
      {:ok, team} ->
        conn
        |> put_flash(:info, "Team updated successfully.")
        |> redirect(to: "/")
      {:error, changeset} ->
        conn
        |> put_flash(:info, "A problem occurred")
        |> redirect(to: "/")
    end
  end

  defp start_bot(conn) do
    team = get_session(conn, :current_team)
    EspiDni.BotSupervisor.start_bot(team.slack_token)
    conn
  end

end
<commit_msg>Redirect oauth successes to setup page<commit_after>defmodule EspiDni.AuthController do
  use EspiDni.Web, :controller
  alias EspiDni.SlackAuthHandler
  alias EspiDni.GoogleAuthHandler
  plug Ueberauth

  def request(conn, _params) do
    render(conn, "request.html", callback_url: Helpers.callback_url(conn))
  end

  def delete(conn, _params) do
    conn
    |> put_flash(:info, "You have been logged out!")
    |> configure_session(drop: true)
    |> redirect(to: "/")
  end

  def callback(%{assigns: %{ueberauth_failure: _fails}} = conn, _params) do
    conn
    |> put_flash(:error, "Failed to authenticate.")
    |> redirect(to: "/")
  end

  def callback(%{assigns: %{ueberauth_auth: auth}} = conn, %{"provider" => "slack"} = _params) do
    case SlackAuthHandler.init_from_auth(auth) do
      {:ok, team, user} ->
        conn
        |> put_flash(:info, "Successfully authenticated.")
        |> put_session(:current_team, team)
        |> put_session(:current_user, user)
        |> start_bot
        |> redirect(to: setup_path(conn, :index))
      {:error, reason} ->
        conn
        |> put_flash(:error, reason)
        |> redirect(to: "/")
    end
  end

  def callback(%{assigns: %{ueberauth_auth: auth}} = conn, %{"provider" => "google"} = _params) do
    team = get_session(conn, :current_team)

    case GoogleAuthHandler.update_from_auth(team, auth) do
      {:ok, team} ->
        conn
        |> put_flash(:info, "Team updated successfully.")
        |> put_session(:current_team, team)
        |> redirect(to: setup_path(conn, :index))
      {:error, changeset} ->
        conn
        |> put_flash(:info, "A problem occurred")
        |> redirect(to: "/")
    end
  end

  defp start_bot(conn) do
    team = get_session(conn, :current_team)
    EspiDni.BotSupervisor.start_bot(team.slack_token)
    conn
  end

end

{'fraction_non_alphanumeric': 0.14090019569471623, 'fraction_numerical': 0.0, 'mean_word_length': 3.0429378531073445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2345479', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1175, 'n_words': 265}