Closed zhulinpinyu closed 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.
➜ 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
@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
@yordis is correct, you're referencing a function that does not exist. Thanks for helping out @yordis ❤️
@yordis thanks you much. ❤️
def resource_from_claims(claims) do
user = Accounts.get_user!(claims["sub"])
{:ok, user}
end
Lads, could you add it to documentation?
@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}
@yordis if you'd like to open a PR to update the docs with that snippet go for it 👍
@doomspork done
{:guardian, "~> 1.0-beta"}