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

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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews