mauric

mauric

Hello you all!

I’m developing an app with Phoenix + Absinthe with React on the front-end. I’m using Guardian to generate the JWT token and with the login mutation I return the token to the front, then I save the token into a cookie. Using Apollo, I include that token into the header and on the server I wrote a Context to check the value and authenticate the user.

I want to improve this solution in terms of where to store the token or even use another solution.

The solution is to store the token on a httpOnly cookie set up by the server ? How to do that ?

Thanks!

Showing Posts 1 to 10

autodidaddict

autodidaddict

Author of Real World Event Sourcing

The following may not be applicable because I don’t know all the context for your use case… but one thing that’s worth remembering about signed JWTs is that they are self-validating. If you trust the signer of the token, then you can trust the token. This means that your Absinthe API doesn’t need to generate tokens at all (or keep them in sessions). It can simply accept a bearer token in the Authorization header of all requests and then you crack that open when you build your context and add a user, scopes, etc.

This is essentially what I’m doing for my project. I don’t know if there are hidden dangers here, but I like this because it means that the front end can generate and sign tokens at will and the back-end can validate those tokens, and nobody needs a session anywhere.

Exadra37

Exadra37

httpOnly cookies cannot be accessed from Javascript, therefore you React app will not be able to use them.

I never coded in React, but if possible store the token returned from the backend in the some React internal state.

Exadra37

Exadra37

I work in API security and never heard this assertions that JWT tokens are self-validating. Do you mind to elaborate more on this?

autodidaddict

autodidaddict

Author of Real World Event Sourcing

If you sign a JWT with a private key, and include the corresponding public key inside the JWT in the iss field, then you know that the JWT was signed by whoever claims to have signed it (assuming you’re validating the signature). You can essentially use the issuer field like an API key - if you trust the issuer (e.g. your web UI) then you can simply accept the contents at face value and assume that the user (sub) in the JWT is correct. You can use aud to validate that the token is going to a specific URL and you can use the expiration field to make time-limited bearer tokens, etc.

This is why I said I don’t know if what I’m suggesting is appropriate for your use case - some scenarios don’t allow for this kind of security pattern.

brettbeatty

brettbeatty

httpOnly cookies cannot be accessed from javascript, therefore you React app will not be able to use them

You can configure Apollo to include HttpOnly cookies when making HTTP requests. Where the option is passed to the browser API the cookies don’t need read by Javascript.

Exadra37

Exadra37

Now I know waht you are talking about :slight_smile: This is called signing the JWT with the RS256 asymmetric signing algorithm.

Are you saying that the JWT token will be signed with a private key stored in the browser and then the backend will validate it by using it’s public key? Or do you mean by Web UI the backend responsible to deliver it to the browser?

derek-zhou

derek-zhou

The JWT is signed at the web backend and stored in the client side (Usually as a cookie) as an opaque data. It is also tamper proof.
Two different server-side applications can share the same key to form a trusted domain, so one application can trust the JWT from another application. With Guardian it is the sharing of the Guardian key.

WestKeys

WestKeys

Sorry, auth tokens are generated on the front end? How does that work?

autodidaddict

autodidaddict

Author of Real World Event Sourcing

I was (perhaps incorrectly so) assuming that there were two Phoenix apps - a “front-end/UI” app and the Absinthe app, which is the GraphQL API. The flow I imagined was something like:

Phoenix/LiveView -> OAuth redir -> Phoenix/LiveView -> JWT -> Absinthe

This is obviously not a one-size-fits-all scenario and probably has more steps in it than most people need. But the general idea was that the UI web app would sign JWTs with its key which could then be verified by the Absinthe app via bearer token auth.

Exadra37

Exadra37

Thanks for the the explanation, and I already understood this flow, but by the way it was phrased I was left with the impression that would be the web app on the browser signing them.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews