0

Firebase/auth docs explain well how to login via magic email link. Issue that I have is that magic email link is opened in a new tab/window which means that after a successful authentication I have two tabs open. What is the best practice to close new/old window/tab if there is already one open?

radulle
  • 1,437
  • 13
  • 19

1 Answers1

2

One way is to use localStorage API to check for this condition. See this question on how to use it: JavaScript: sharing data between tabs

From UX standpoint: while closing a duplicate tab seems like a good idea on a first glance, as a user I'd not be too happy about losing a tab with it's back history. I've seen at least one website that applied a whole-page overlay saying "this is open already in another tab" to disable interaction.

loa_in_
  • 1,030
  • 9
  • 20
  • I would not close the old tab but the new one. I was thinking of using localStorage API but had two edge cases why I was not sure about it: 1) what happens if user first closes manually the tab and than opens the new link? Than I would have to listen to `beforeunload` event and clear the key. 2) What if I already have two or more tabs open and I was to implement the strategy from the point 1). Than I would have to add onchange listener to localStorage, and it quickly blows out. I was kind of hoping for some straightforward/simple solution. – radulle Oct 04 '21 at 18:19
  • Instead of clearing the key, you can periodically set a value to current timestamp. Kind of. It's not a trivial problem to solve. Some websites delegate to service workers. They are shared across all tabs in a domain. That serves as a good guarantee as well. – loa_in_ Oct 04 '21 at 18:52