Hello!
So, I started playing with liveviews for a new project. And I have a couple of questions, and I would love if someone can point me in the right direction.
I need the user to login (I have a login service) My question is how to authorize the “socket”, I know that when using websockets we have the connect/2 callback and I can make some auth logic there, but the liveview should be treated as a websocket?
Or I need to add auth information to the session like a normal webpage?
When using reactjs in the frontend, I make use of JWT stored in localStorage
There are a few discussions on the forums covering this and I still think the best model is yet to emerge. The most common approach seems to be to authenticate using the traditional approach and pass the credentials into the liveview via router config (there have been a couple of changes to make this easier in recent liveview releases, but the concepts are covered by @Softknobs comment here: LiveView authentication failed: how to redirect to login page?)
If the liveview is configured in a protected router pipeline you should be good to go. Token exchange with the browser to confirm it is a valid websocket connection is handled by LiveView for you.
I hope that doesn’t make you more lost! TLDR; authenticate using old fashioned session controllers, pass user_id into liveview, ensure liveviews are in protected router pipeline.