greyhwndz

greyhwndz

Hello everyone!

Can somebody help me understanding the following…

Why is it that:

mydata = %{id: user.id, email: user.email, username: user.username}
IO.inspect mydata
jwt = %{data: mydata, sub: user.id}

results in

iex(1)> %{email: "maria@hello.com", id: 4, username: "maria"}

while:

mydata = %{id: user.id, email: user.email, username: user.username}
jwt = %{data: mydata, sub: user.id}
IO.inspect mydata

results in

iex(1)> {:ok,
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.B0QRhfQku78gSaxGoC8sxcKVL1wScNCAQGU_lzV4sQ8",
 %{}}

Showing Posts 33 to 24

greyhwndz

greyhwndz OP

I guess @NobbZ I was referring to somewhat like a typesafety advantage that is :slight_smile:

NobbZ

NobbZ

It is basically the same as an imaginary user when is_struct(user, User) -> ….

I’m not sure what you otherwise mean by " offering the same advantage as somewhat like ensuring that the input is of the same User struct".

greyhwndz

greyhwndz OP

yes @NobbZ thank you so much!
Is user = %User{} -> offering the same advantage as somewhat like ensuring that the input is of the same User struct?

NobbZ

NobbZ

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

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
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))
...

?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

greyhwndz

greyhwndz OP

So with my case it should be

token_with_default_plus_custom_claims = MyApp.Token.generate_and_sign!(jwt)

?

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)
greyhwndz

greyhwndz OP

Joken 2.0 @benwilson512

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews