johnnyicon

johnnyicon

LiveView using basic `Map` as session and not `Plug.Conn`

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!

Marked As Solved

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

Last Post!

johnnyicon

johnnyicon

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement