ueberauth / guardian

Elixir Authentication
MIT License
3.43k stars 382 forks source link

No secret key configured for JWT #460

Closed arunvelsriram closed 6 years ago

arunvelsriram commented 6 years ago

I followed the steps under https://github.com/ueberauth/guardian#installation and ended up in the below error:

** (RuntimeError) No secret key configured for JWT
    (guardian) lib/guardian/token/jwt.ex:319: Guardian.Token.Jwt.fetch_secret/2
    (guardian) lib/guardian/token/jwt.ex:163: Guardian.Token.Jwt.create_token/3
    (guardian) lib/guardian.ex:754: Guardian.returning_tuple/1
    (guardian) lib/guardian.ex:581: Guardian.encode_and_sign/4

config.exs

config :playground, Playground.Guardian,
  issuer: "playground",
  secret_key: "iZYJkEAaViic3E24ihM6n587JYXBkXdYSKHZkxfe2s9HoyZ0GNW9p4u7nJv6IdtN"

guardian.ex

defmodule Playground.Guardian do
  use Guardian, otp_app: :my_app

  def subject_for_token(resource, _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(resource.id)
    {:ok, sub}
  end

  def subject_for_token(_, _) do
    {:error, :reason_for_error}
  end

  def resource_from_claims(claims) 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.
    id = claims["sub"]
    resource = Playground.get_resource_by_id(id)
    {:ok,  resource}
  end

  def resource_from_claims(_claims) do
    {:error, :reason_for_error}
  end
end
scrogson commented 6 years ago

Looks like you copy/pasted the whole example without changing your otp_app option to your app name.

arunvelsriram commented 6 years ago

Oops, my bad. Thanks!