amacedev

amacedev

Just curious if the basic auth framework generated by phx.gen.auth can be ‘easily’ converted to work with a JSON api.

Showing Posts 1 to 10

al2o3cr

al2o3cr

Can you expand on what you mean by “work with a JSON API”? I can think of two scenarios that meet that description and they have different answers for your question.

If you mean “connected to a JS frontend via JSON-formatted requests”, the conversion wouldn’t need to change that much. The biggest change might be with the details of where authentication data is in the request; best-practice with JSON APIs would be a bearer token, versus the cookie-based session used by default.

If you mean “as an authentication system for a JSON API called by other programs” (either via HTTP Basic or API key), the generated code would be a lot less useful.

amacedev

amacedev OP

Ah yeah sorry I can see how that would be confusing. I mean the former - connected to a JS frontend. I’ve read in a few different places how JWT’s are apparently subjectively worse than sessions. Thoughts on that? Why wouldn’t the cookie-based session be good practice?

thojanssens1

thojanssens1

JWT is a token that allows you to maintain user sessions. So what do you mean that they are worse than sessions?

derek-zhou

derek-zhou

Cookie is designed to work without the JS frontend. By default, cookie session in phoneix is http only (not accessible by the js) and unencrypted (but signed so is tamper-proof). If you log in via conventional http that it is fine; if you want to have fancy login in javascript driven frontend, then it is not as easy to work with.

A bearer token is an opaque token, whereas a JWT token in the cookie session contains trusted data. With a bearer token you keep all session data server side in a ETS table or something, so it is safer and easier to work with in js.

thojanssens1

thojanssens1

Could you elaborate on this please? I think whether we work with cookies or not it doesn’t matter? As long as the server has an appropriate success/failure response for its login service? I guess you see something I don’t see:)

phx.gen.auth stores it in DB. Storing the sessions in ETS wouldn’t allow having long lasting sessions (e.g. 60 days default for phx.gen.auth when remember-me is checked) without a huge cost in memory? Anyway it might be an option indeed but interesting that you mentioned that one as an example instead of some relational DB.

derek-zhou

derek-zhou

Without a concrete design I cannot comment anymore. You probably have some requirements that we don’t know, so no one else can design it for you. If you have a plan, but want a second opinion, then please post your plan first.

MarthinL

MarthinL

I’d like to latch on to this question if I may.

My plan is to have my users sign up and log in using standard phoenix generated html. Essentially a customised version of the pages generated by:

mix pix.gen.auth Accounts User users

While they are logged in, users will interact on the site through a Vue Single Page Application which fetches and updates data using an API written in phoenix served from the same site.

What I’ve managed to do so far is to

  • start with a phx.new project (i.e. with ecto and html),
  • Vue-enabled it at the hand of Caleb Weeks’ blog post and Steven Pallen’s project,
  • added authentication using phx.gen.auth and
  • added a basic API added with phx.gen.json.

What I now need to figure out, with your help, is:

  • where to find the token on the vue client side,
  • how to get the token into the API call (I’m using axios),
  • how to retrieve- and
  • check the token again on the API side to limit API calls to only those arriving with valid and current tokens and also to retrieve “session” data linked to the token in the database (cached in ETS), which never the client code never get to see.

I’m aware that somewhere between none, some or even all of this is already taken care of by the generated code and/or various third-party libraries like Guardian. But I’m given to the idea that the best framework is no framework and want to ensure that I understand what the code I am using does and how.

So far, I am loving Elixir a lot, for that reason (I’ve come to Elixir from Erlang and PHP) but I am still very new to it.

al2o3cr

al2o3cr

General comment: a typical API is not going to have any stateful behavior that would require a session.

MarthinL

MarthinL

I know, session data sounds like the “stateful behaviour” which RESTfull APIs are not to get hung up with, which is why I put session in quotes. My application isn’t typical and I’m well aware of the implications of keeping per-user or even per-session information in an API server. We’re not talking about a real session which used to refer to a process or server execution context staying inthe memory of a specific instance of a server which meant that future interactions needed to go back to the same instance which might be too busy now or for a different instance to reconstruct the execution context from the database just to continue execution of the next request. That was the MO of IIS and Apache which lead to stateless or RESTfull servers gaining popularity. What I call a “session” is more like data specific to the user for which the token which got issued when they logged in. There is a whole system of routing, load balancing and data distribution behind the API server to deal with horizontal scaling and reduce overheads.

derek-zhou

derek-zhou

If you want to touch the token from client side js, then it cannot be in the http-only cookie session as in the default. I’d suggest to put it in the localStorage, with potential XSS implication applied.

Once you have your hand on the token you can put it in the Authorization: Bearer header in your fetch call, it is straight forward to retrieve it and look up server side data from it.

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
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
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
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

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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews