ayanc97

ayanc97

Hi, I am complete new to handling cookies and storing tokens in order to send request from phoenix template html page to api endpoint, authenticated by auth plug (via ajax), so I need a way to store the auth token to send them along with every request, but if I normally store it in cookie as key value pair, it can be read by every one, I am not much versed with cookie , I came to know about various flags to make the cookies secure, but I tried this one
put_resp_cookie("auth_token", "some value", [same_site: "strict", secure: true, http_only: true])
but i see nothing is being stored, I think I am getting it wrong , please suggest me how I can send ajax request using auth token in header from my phoenix html pages( had it been any front end framework, i could have many options, but currently i need to implement it using normal html phoenix page and js/ajax) .

Showing Posts 1 to 6

Exadra37

Exadra37

If you use the http_only flag the browser will not let any Javascript access the cookie, therefore you JS code will not be able to read the auth_token to use in an Ajax call.

Read more about securing cookies here:

Now, if you need to use this auth_token to call a thrid-party API, then the only secure approach is to delegate that task to your Phoenix backend that will then return the response to the browser via a socket event.

ayanc97

ayanc97 OP

but how will i have socket between a normal js file that dont have access to anything other than DOM, Can you please elaborate, and FYI I am sending the ajax request to my same site, just I am calling the API endpoint (callable from Mobile app/ frontend framework) from Normal html templates in phoenix via javascript instead.

Exadra37

Exadra37

You can use Phoenix Channels:

Just send a request to the backend via a Phoenix Channel to get a new auth_token with some seconds worth of time to expire, and then do the Ajax request to the API endpoint.

Any data you need to get from the same API endpoints you may want to do it internally from your backend.

So, just request the data via a Phoenix Channel and then update your DOM when you get a response, as per examples in the above link to Phoenix Channels docs. Maybe instead you prefer to just use Phoenix LiveView if you decide to go down this route.


If you want to go old school by using a normal AJAX request, then your security will be weaker, because you cannot secure secrets in the client side. As you already know in the browser all it takes is to use F12 to get the auth_token or it can be stolen from another JS script, but even in your mobile app the auth_token to query the API can be extracted, despite the code being in binary format(the APK) on the client side.

KristerV

KristerV

Just to clarify. Sockets (Phoenix Channels) and a request (fetch/ajax) are two different things. Most likely what you need is a simple request.

Make sure you check out the official Phoenix tutorial on sessions: https://phoenixframework.readme.io/v0.13.0/docs/sessions

There they use put_session and get_session to save/read session variables.

Does that answer your question?

ayanc97

ayanc97 OP

hi, is it the case that , plug.session key-value pairs are not visible to anyone right? so I wont be able to access the value using javascript, even if i store it, because I cannot access sesssion value from js, so any way around?

KristerV

KristerV

yes. session variables are server-only information.

— All posts loaded —

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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews