<commit_before>defmodule Goncord.TokenController do
  use Goncord.Web, :controller
  require Logger

  alias Goncord.User

  def create(conn, %{"login" => login, "password" => password}) do
    user = Repo.get_by(User, login: login)
    create(conn, user, password)
  end

  def create(conn, %{"email" => email, "password" => password}) do
    user = Repo.get_by(User, email: email)
    create(conn, user, password)
  end

  defp create(conn, user, password) do
    case User.check_hashed_password(user, password) do
      true ->
        case Guardian.encode_and_sign(user, :token, %{}) do
          {:ok, jwt, _full_claims} ->
            conn
            |> put_status(:created)
            |> render("show.json", jwt: jwt, user: user)
          {:error, reason} ->
            Logger.error("Error in #{__MODULE__}.create. Tried to issue JWT: #{inspect(reason)}")
            send_resp(conn, :internal_server_error, "")
        end
      _ ->
        conn
        |> put_status(:unprocessable_entity)
        |> render("error.json", message: "Проверьте правильность логина, email или пароля")
    end
  end
end<commit_msg>Added logout, validate view. Added unauthenticated hook.<commit_after>defmodule Goncord.TokenController do
  use Goncord.Web, :controller
  require Logger

  alias Goncord.User

  def create(conn, %{"login" => login, "password" => password}) do
    user = Repo.get_by(User, login: login)
    create(conn, user, password)
  end

  def create(conn, %{"email" => email, "password" => password}) do
    user = Repo.get_by(User, email: email)
    create(conn, user, password)
  end

  defp create(conn, user, password) do
    case User.check_hashed_password(user, password) do
      true ->
        case Guardian.encode_and_sign(user, :token, %{}) do
          {:ok, jwt, _full_claims} ->
            conn
            |> put_status(:created)
            |> render("show.json", jwt: jwt, user: user)
          {:error, reason} ->
            Logger.error("Error in #{__MODULE__}.create. Tried to issue JWT: #{inspect(reason)}")
            send_resp(conn, :internal_server_error, "")
        end
      _ ->
        conn
        |> put_status(:unprocessable_entity)
        |> render("error.json", message: "Проверьте правильность логина, email или пароля")
    end
  end

  def delete(conn, _params) do
    jwt = Guardian.Plug.current_token(conn)
    {:ok, claims} = Guardian.Plug.claims(conn)

    case Guardian.revoke!(jwt, claims, nil) do
      :ok -> send_resp(conn, :no_content, "")
      {:error, reason} ->
        Logger.error("Error in #{__MODULE__}.delete. Tried to revoke JWT: #{inspect(reason)}")
        send_resp(conn, :internal_server_error, "")
    end
  end

  def unauthenticated(conn, _params) do
    conn
    |> put_status(:unauthorized)
    |> put_view(Goncord.TokenView)
    |> render("error.json", message: "Требуется аутентификация")
  end

  def validate(conn, _params) do
    user = Guardian.Plug.current_resource(conn)
    conn
    |> put_status(:ok)
    |> put_view(Goncord.UserView)
    |> render("user.json", user: user)
  end
end
{'fraction_non_alphanumeric': 0.13380967846703476, 'fraction_numerical': 0.0, 'mean_word_length': 2.8987341772151898, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, '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': '2870323', 'n_tokens_mistral': 1029, 'n_tokens_neox': 1002, 'n_words': 241}