ueberauth / guardian

Elixir Authentication
MIT License
3.44k stars 381 forks source link

use 1.0-beta Got undefined function find_me_a_resource #414

Closed zhulinpinyu closed 7 years ago

zhulinpinyu commented 7 years ago
defmodule MyApp.Guardian do
  use Guardian, otp_app: :my_app

  def subject_for_token(resource, _claims) do
    {:ok, to_string(resource.id)}
  end

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

  def resource_from_claims(claims) do
    {:ok, find_me_a_resource(claims["sub"])}
  end
  def resource_from_claims(_claims) do
    {:error, :reason_for_error}
  end
end

{:guardian, "~> 1.0-beta"}

yordis commented 7 years ago

@zhulinpinyu could you put the stack trace? With that people outside of guardian team could take a look faster because we do not know the code base but reading the stack trace we can figure out faster.

zhulinpinyu commented 7 years ago
➜  demoJWT mix phx.server
Compiling 17 files (.ex)

== Compilation error in file lib/demoJWT/guardian.ex ==
** (CompileError) lib/demoJWT/guardian.ex:13: undefined function find_me_a_resource/1
    (stdlib) lists.erl:1338: :lists.foreach/2
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
yordis commented 7 years ago

@zhulinpinyu ahhhhhh I see what is the problem

find_me_a_resource is not a function from Guardian, the point is that you replaced find_me_a_resource with some function from your logic, which could take what comes from claims["sub"] and return the resource back. You could do the business logic in that function as well but I wouldn't.

 def resource_from_claims(claims) do
    resource = MyApp.get_resource_from_claims(claims["sub"])
    # Do whatever you want but you have to return `{:ok, resource}`
    {:ok,  resource}
  end
doomspork commented 7 years ago

@yordis is correct, you're referencing a function that does not exist. Thanks for helping out @yordis ❤️

zhulinpinyu commented 7 years ago

@yordis thanks you much. ❤️

def resource_from_claims(claims) do
  user = Accounts.get_user!(claims["sub"])
  {:ok, user}
end
alec-c4 commented 6 years ago

Lads, could you add it to documentation?

yordis commented 6 years ago

@doomspork I would use this snippet

def resource_from_claims(claims) do
    resource = MyApp.get_resource_from_claims(claims["sub"])
    # Do whatever you want but you have to return `{:ok, resource}`
    {:ok,  resource}
  end

At least they can relate MyApp and the breaking down of {:ok, resource}

doomspork commented 6 years ago

@yordis if you'd like to open a PR to update the docs with that snippet go for it 👍

yordis commented 6 years ago

@doomspork done