Pass in session data to a live view when instigated via a live route?

I’m not sure if this is helpful, but what I didn’t realise when I wrote the question is that I can do the following in my router.ex:

live "/tasks", TaskLive.Index, session: [ :guardian_default_token ]

:session is a list of keys that may exist in the Plug session (when the page is initially rendered). If they do exist, then the key/value pair is copied to the LiveView session (as a parameter in the mount function call). There’s more information in the documentation

In my case, I’m using Guardian. So once the user authenticates, I’m able to pass in the guardian_default_token session variable - which is stored as a secure cookie.

But to get the token stored as a session variable in the first place, I think it needs to be a part of a post back to the server? So I’m not sure that you can do this without a redirect? Please take the thought with a grain of salt - it’s only a guess

2 Likes