leifmetcalf

leifmetcalf

Mixing authenticated and unauthenticated LiveViews

Is there a way to let authenticated users live_navigate from an authenticated route to an unauthenticated route without a doing a page reload? I have some standard routes

live "/posts", PostLive.Index, :index
live "/posts/new", PostLive.Form, :new
live "/posts/:id", PostLive.Show, :show
live "/posts/:id/edit", PostLive.Form, :edit

I would like to make /posts and /posts/:id accessible to all users, and /posts/new and /posts/:id/edit accessible only to authenticated users. I can add the routes to the :current_user and :require_authenticated_user, but then authenticated users would have to do a page reload when navigating from e.g. /posts/new to /posts.

First Post!

olivermt

olivermt

Live sessions always require a reload, but you can do your auth check manually in mount instead of in a hook if youbreally want to avoid reloading.

As in only assign the user (allowing current user to be nil) and implement the security aspects based on the live action in mount.

Easy to foot gun yourself and forget and very repetetive so maybe worth asking yourself if it is worth it.

Last Post!

leifmetcalf

leifmetcalf

:grinning: I was worried about security, but if it just means the user isn’t redirected sometimes when I forget to add the on_mount handler to a LiveView, then that’s not too bad.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New

We're in Beta

About us Mission Statement