defmodule AuthService.Guardian do
  use Guardian, otp_app: :auth_service
  alias AuthService.Accounts
  def subject_for_token(%{id: id}, _claims) do
    # You can use any value for the subject of your token but
    # it should be useful in retrieving the resource later, see
    # how it being used on `resource_from_claims/1` function.
    # A unique `id` is a good subject, a non-unique email address
    # is a poor subject.
    sub = to_string(id)
    {:ok, sub}
  end
  def subject_for_token(_, _) do
    {:error, :reason_for_error}
  end

  def resource_from_claims(%{"sub" => id}) do
    # Here we'll look up our resource from the claims, the subject can be
    # found in the `"sub"` key. In above `subject_for_token/2` we returned
    # the resource id so here we'll rely on that to look it up.
    user = Accounts.get_user!(id)
    {:ok,  user}
  end
  def resource_from_claims(_claims) do
    {:error, :reason_for_error}
  end
  def fetch_secret do
    Application.get_env(:auth_service, AuthServiceWeb.Guardian)
      |> Keyword.get(:secret_key_file)
      |> JOSE.JWK.from_pem_file
end
end
