Maintaining the current logged in user.
How do we maintain the current user, like in liveviews we fetch it from the session.
60 Replies
You can use the
load_from_session
plug in your router, which should be documented
and then you can use the live session
For instance, we do this in ash_hq around our liveviews
so i will add the on_mount one?
in my liveviews?
Have you done this kind of thing with liveview before?
It might be worth reading their documentation on this stuff too
The
load_from_session
plug is documented in the getting started guide for ash_authentication_phoenix, and will make a current_user
assign available
the on_mount
is something you write yourself
but just adding ash_authentication_live_session
will set the current_user
assign
So the {AshHqWeb.LiveUserAuth, :live_user_optional}
is something I wrote for AshHq
specificallyyes i have but at that time i had to write a custom live helper to fetch the current user from session and assign it to the socket and called that function in every mount of liveview.
Ah, yeah so thats been updated
and now you can use
on_mount
hooks like the one I mentioned
so ash_authentication_live_session
will set current_user
assign, and then you can add an on_mount
hook to do things like require that the user is there for certain routes
i.e
This is what it looks like in AshHq
That makes sure that the assigns I want are always there (current_user
) and also handles requiring a user for some routesWhat i understood from on_mount live user required one is making sure that the current user is present.
Correct
This example is from the getting started guide for ash authentication phoenix:
Now i understood it. I am sorry. I couldn't find it in the docs. 😅
no problem 👍
If i want the relationships of user to be loaded then?
Put that in your
on_mount
hook
and use Accounts.load(user, [:relationships, ...])
so that i have to do through plug.
You can do it in the
on_mount
hook like I showed above, just add logic to load relationships
Like thatyeah i got it 😄
or i can add another on_mount just to load the relations
yep!
That would be a good way to do it
in list we provide the relations that we need to load, right?
is it different from the Ash.Query.load?
They are exactly the same 👍
MyApp.Accounts.load!(socket.assigns, :current_user, [:teams])
if i do this it gives me errorThat isn't how that works
the first argument to
load!
is the ash record
and the second argument is the stuff you want to load
MyApp.Accounts.load!(socket.assigns.current_user, [:teams])
oh, i picked the one you wrote above in the
on_mount
hookOh
sorry 🙂
no, don't be.
I've fixed the example
its just i am new 😅
Doesn't help when I give you bad code samples 😆
it still gives me the error
* No read action exists for Dummy.Accounts.Team when: loading relationship teams
I have this defined in Team resource:
Can I see the whole resource?
You sure you've saved it and recompiled and everything?
That looks right to me
yes, i have auto save enabled
and you restarted your server?
Sorry, just being thorough because that looks right to me
Yup
same error
Can I see your code where you're loading it?
yes gimme a sec
added this is auth plug
Ah
That is fine, but I think the issue is probably on your join resource
Dummy.Accounts.TeamJoinedUser
also needs defaults [:read, :create, :update, :destroy]
yeah that was the issue
Am i missing something here?
no function clause matching in Plug.Conn.assign/3
It is giving me this error.That shouldn't be calling
Plug.Conn.assign
it should be from Phoenix.Component
I believe
did you try to put the on_mount
in a plug?nope
can I see the whole module
on_mount {Dummy.AuthPlug, :load_assocs_current_user}
I didn't even know you could do thisreally?
Yeah, I only ever did them in the live_session
I got to know about them recently myself tho.
anyway, can I see
AuthPlug
?
Because it looks like you did something like import Plug
and thats not what you want to dowhen i implemented
mix phx.gen.auth
it gave an example there for this.
Yeah, so because you put that in your
AuthPlug
, when you say assign
its calling the imported Plug.Conn.assign
But there is a different assign that you are supposed to use with liveview sockets
Phoenix.Component.assign
For example, the one we use in ash_hq
See how that does import Phoenix.Component
(not use AshAuthentication.Plug, otp_app: :dummy_app
)yes i got it
that was the issue.
👍
oh so i shouldn't use the ash auth plug here?
Put it in its own module like I show above
two User Auths?
one for controller requests and the other one for Socket ones?
and can we do nested relationship loaded using
load/3
?yes, you can
load(foo: [bar: [baz: :buz]])
They are just two different modules for doing two different thignsnoted
if you want to combine them you can
so just like we do in preload
but you just have to make sure you're calling the right functions 😆
😂
Lets resolve this one since we got through the main issue. Feel free to open more.
Sure. Thank you