benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You’re passing in nil as the payload.

2
Post #21
greyhwndz

greyhwndz OP

You mean jwt is not the payload? This is the one aspect I find it hard to understand with Joken even after going thru its documentation. I am using 2.0

0
Post #22
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

What version of Joken are you on?

2
Post #23
greyhwndz

greyhwndz OP

Joken 2.0 @benwilson512

0
Post #24
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Right, so your issue is that Joken — Joken v2.6.2 isn’t really built to be called explicitly, the docs call for you to have a MyApp.Token module that handles this stuff. The first arg to Joken — Joken v2.6.2 is the token configuration. If you look at some examples you’ll see that if you want to encode stuff like the user id and email that should be done as so:

extra_claims = %{"user_id" => "some_id"}
token_with_default_plus_custom_claims = MyApp.Token.generate_and_sign!(extra_claims)
2
Post #25
greyhwndz

greyhwndz OP

So with my case it should be

token_with_default_plus_custom_claims = MyApp.Token.generate_and_sign!(jwt)

?

0
Post #26
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Yes, after you create that module in accordance with the Joken docs.

2
Post #27
greyhwndz

greyhwndz OP

I actually already have gone thru the documentation but what I can’t seem to do is the conversion of

%{data: %{id: user.id, email: user.email, username: user.username}, sub: user.id}

into a format that I can easily convert into

def token_config do
    default_claims()
    |> add_claim("some_claim", nil, &(&1 == "some value"))
  end

should I add each claim one by one lilke

...
|> add_claim("id", nil, &(&1 == user.id))
|> add_claim("email", nil, &(&1 == user.email))
|> add_claim("username", nil, &(&1 == user.username))
...

?

0
Post #28
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I’m not a JWT power user or anything but can’t you just do

  def render("token.json", %{user: user}) do
    data = %{id: user.id, email: user.email, username: user.username}
    jwt = %{data: data, sub: user.id}
    token = MyAppWeb.Token.generate_and_sign!(jwt)
    %{token: token}
  end
3
Post #29
NobbZ

NobbZ

The user case as the most generic clause has to come last, alternatively you could make it user = %User{} or something similar.

2
Post #30