sanals
Guardian.resource_from_claims/1 function always returns nil instead of user
Hi,
I am new to elixir. I have a web app in elixir which is not done by me. I am finding it difficult to understand the code. When ever I try to access an api which requires authentication, it always return “no authorization”.
I am not able to call any function that is included within plug MyApiWebPlug.Auth when action in [update_profile, :get_myprofile, .......]
All the function fails and response with no authorization . since that’s the message in halt_request
After signin in, I got a token and I am passing the token as authorization along with all the api calls.
but they all fails at this line MyApi.Guardian.resource_from_claims. It returns nil
if it helps, I am posting the Plug.Conn module.
defmodule MyApiWeb.Plug.Auth do
@moduledoc """
This Plug performs authentication.
"""
import Plug.Conn
def init(default), do: default
def call(conn, _) do
case Enum.find(conn.req_headers, &elem(&1, 0) == "authorization") do
{_, token} ->
case MyApi.Guardian.decode_and_verify(token, %{"typ" => "access"}) do
{:ok, claims} ->
case Guardian.DB.on_verify(claims, token) do
{:ok, {claims, _}} ->
user =
claims
|> MyApi.Guardian.resource_from_claims
|> elem(1)
if user == nil,
do: halt_request(conn),
else: conn |> assign(:user, user)
{:error, _} -> halt_request(conn)
end
{:error, _} -> halt_request(conn)
end
nil -> halt_request(conn)
end
end
def halt_request(conn) do
conn
|> put_resp_content_type("application/json")
|> send_resp(403, Poison.encode!(%{message: "no authorization"}))
|> halt
end
end
First Post!
kokolegorille
It is the same code as in your previous question…
The problem is probably not with the application, but with the way You pass the authorization token.
You could put some log to see what kind of token You receive.
There is not enough information to debug your problem.








