johnnyicon

johnnyicon

Hi all, working with LiveView and the code generated by phx.gen.live. I’m also working with code generated by phx_gen_auth for my authentication.

I’m trying to use the functions defined in UserAuth from phx_gen_auth to manage authentication on a LiveView. I looked through the code of UserAuth and saw that it expects sesions to be of type Plug.Conn.
However, it seems that LiveView is using a basic Map. In particular, I looked at the parameters of the function
def mount(_params, session, socket) of a LiveView and inspected the session parameter.

Should it be using Plug.Conn?

If not, how would I convert a Map to Plug.Conn so I can use the functions defined in UserAuth?

Cheers!

Showing Posts 1 to 3

voughtdq

voughtdq

What do you need to do?

In your router (router.ex) you should have something like this:

  scope "/", PayBillWeb do
    pipe_through [:browser, :require_authenticated_user]

    get "/users/settings", UserSettingsController, :edit
    put "/users/settings/update_password", UserSettingsController, :update_password
    put "/users/settings/update_email", UserSettingsController, :update_email
    get "/users/settings/confirm_email/:token", UserSettingsController, :confirm_email

    live "/sequences", SequenceLive.Index, :index
    live "/sequences/new", SequenceLive.Index, :new
    live "/sequences/:id/edit", SequenceLive.Index, :edit

    live "/sequences/:id", SequenceLive.Show, :show
    live "/sequences/:id/show/edit", SequenceLive.Show, :edit
  end

Here if someone tries to go to /sequences they will be redirected to the login controller if they aren’t already authenticated.

At this point if you’re inside a live view, you don’t need to use any of the UserAuth functions. The UserAuth functions are supposed to be used over HTTP because they are for setting cookies and renewing sessions.

Instead, when mounting, you get a map, like you mentioned, in the session argument. Here, you can pull out the user_token so you can find the user in the database:

  def mount(_params, %{"user_token" => user_token}, socket) do
    user = user_token && Accounts.get_user_by_session_token(user_token)
    ...
  end

Then you can do what you need to do as far as authorization is concerned.

johnnyicon

johnnyicon OP

I’m wanting to get access to the current user. To do that, I’m calling UserAuth.fetch_current_user(session, {}) within mount() When the code runs, the following error occurs:

Request: GET /contacts
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in Plug.Conn.get_session/1
        (plug 1.10.4) lib/plug/conn.ex:1566: Plug.Conn.get_session(%{"_csrf_token" => "8QpsoNqphY4LSSZlcWRAgjjo", "live_socket_id" => "users_sessions:R8ARynE4Xr0R9XczxEJ7LYyPZlU0_K_Isj38GqlOCsU=", "user_token" => <<71, 192, 17, 202, 113, 56, 94, 189, 17, 245, 119, 51, 196, 66, 123, 45, 140, 143, 102, 85, 52, 252, 175, 200, 178, 61, 252, 26, 169, 78, 10, 197>>})
        (plug 1.10.4) lib/plug/conn.ex:1553: Plug.Conn.get_session/2
        (app 0.1.0) lib/app_web/controllers/user_auth.ex:101: AppWeb.UserAuth.ensure_user_token/1
        (app 0.1.0) lib/app_web/controllers/user_auth.ex:95: AppWeb.UserAuth.fetch_current_user/2
        lib/app_web/live/contact_live/index.ex:13: AppWeb.ContactLive.Index.mount/3
voughtdq

voughtdq

Got it. So this code here:

  def mount(_params, %{"user_token" => user_token}, socket) do
    user = user_token && Accounts.get_user_by_session_token(user_token)
    ...
  end

Is actually a direct copy from UserAuth repurposed for the live view mount. If you want to create something that is more general purpose you might do this:

def get_current_user(%{"user_token" => user_token} = _session) do
  user_token && Accounts.get_user_by_session_token(user_token)
end

Then you can do this in any of your mounts:

  def mount(_params, session, socket) do
    user = get_current_user(session)
  end
— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
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
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

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
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews