coen.bakker

coen.bakker

How to store 'return to' URL when navigating from LiveView to another page (LiveView/regular)?

I have a live session called :home that consists of four LiveViews. From the LiveViews of this live session the user can navigate to a set of ‘secondary’ pages (LiveViews and regular views). Whenever a user does so, this ‘secondary’ page should show a back button that returns the user to the LiveView that they navigated from. If the user loads a ‘secondary’ page directly from the browser’s location bar, the back button is still visible and returns the user to one of the :home LiveViews by default.

I went into this problem thinking I would build it quickly, but cannot seem to find an elegant way to solve it. I can compare the navigation history (History API) with the URL’s of the :home live session, but I wanted to try to store the URL of the LiveView before redirecting the user to the requested page. Because the user is redirected from a LiveView, rather than a regular view, I fail to see how I can add the URL to the session data of the redirected-to page. There is no redirect(socket, to: "/path", session: %{return_to: "/path"}).

Edit:
So my question is: how can I pass data from a LiveView to a navigated-to LiveView/regular page?

Marked As Solved

RudManusachi

RudManusachi

First thing comes to my mind is to pass “return_to” as a query parameter and have a plug that would read that param and put the value into an “assign” for conn (for “dead” views). And attach an on_mount hook that does the same but for live_sessions.

Also Liked

eahanson

eahanson

Make sure to validate the return_to value to mitigate phishing attacks: Unchecked Redirect | Hacker101

Also, you might want to remove it from the URL so that when people bookmark or share the current URL, it doesn’t send them somewhere unexpected after visiting the URL.

ericmason

ericmason

This has the benefit of the button going to the right place when the user has multiple tabs open on your app, which is not the case if you store the path in the session.

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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 41989 114
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement