sanals

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

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.

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement