njwest

njwest

Note: The real question here is probably “how to access window cookies within a JS SPA”, but can’t hurt to ask from this perspective…

So I am serving a VueJS app built to priv/static/ within a Phoenix 1.4 --no-webpack server, and I am serving the VueJS app from its generated index.html (not index.html.eex) via the PageController like so:

defmodule ...PageController do

  def index(conn, _params) do
    conn
    |> put_resp_header("content-type", "text/html; charset=utf-8")
    |> Plug.Conn.send_file(200, Application.app_dir(:tips, **"priv/static/index.html"**))
    |> halt()
  end

I am using UeberAuth + the UeberAuth CAS strategy to authenticate access to index.html where the VueJS app lives. This puts some user information into a neat session cookie available in my window.

In order to interface with my Phoenix backend, I am making internal HTTP requests to API endpoints in my router.ex that return JSON – since I have the cookie in my window, though, I imagine there is a better way to get my user’s info than:

// js
axios.get('/path/to/get_session').then(...)

# ex
def get_session(conn, _params) do
    session = fetch_session(conn)
    # get user from :plug_session within session and render it to JSON
end

Since I’m using CAS , I’m avoiding writing a JWT layer on top of this as I’m using CAS as a barrier to index.html, not within the JS SPA itself, and I just feel like There’s Got to Be a Better Way :stuck_out_tongue:

Any input would be greatly appreciated, thanks for reading!

Showing Posts 1 to 4

kokolegorille

kokolegorille

I think the usual way to pass data between html and js is to add data attribute to an element. At least if You don’t use the full SPA, Webpack etc.

I don’t understand why You need an index.html, and not an eex template. If You add eex extension, and pass no parameters, it is supposed to be equivalent to a pure html file. If You hide data attributes inside html, it’s easy for javascript to read them.

njwest

njwest OP

Thanks for the response Koko!

My question was about making requests authenticated with cookies to internal endpoints from a Vue SPA served within a Phoenix app rather than in an independent client – not communication between the HTML and JS, but between the SPA and the backend

Was wondering if anyone else has cookie strategies for JS SPAs (React / Vue) that differ from the internal requests of the kind I’m doing above, but seems these internal requests are performing well

axelson

axelson

Scenic Core Team

Can’t the Phoenix server simply read the cookie that is sent with the request? Am I missing something?

njwest

njwest OP

Yeah that’s what I’m doing, was just curious if I was missing out on any other strategies

— All posts loaded —

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

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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews