0

I have a single webpage that uses an API to query backend for data, which is returned and plotted in a few figures. Backend is Node.js with Express.

The appearance of the figures is controlled through a set of JS functions that are loaded when the page initially loads.

After the user has been on the page for 30 seconds, I want to ask them to login or register by displaying a modal over the page.

If the login is successful, I'd like to close the modal and have new functionality available to the user, including changing the behavior of the original JS functions that were loaded when the user first arrived. But, I'd like to do this without reloading a new page with a separate set of JS functions.

I'm a relative newbie at this and have been having a hard time figuring out the right way to accomplish this.

I thought maybe there was a way to update the original JS function file by submitting an API get request and using the response to overwrite replace/overwrite the non-authenticated version. Then user would have access to new functionality without having reloaded the page.

But, I can't seem to find anything that would support this as the correct approach, or even whether this would be possible.

Really need help on which direction to go.

casemax
  • 1
  • 1
  • 2
  • Redefining those functions on successful signup/login would be one option - [can-you-alter-a-javascript-function-after-declaring-it](https://stackoverflow.com/questions/2136522/can-you-alter-a-javascript-function-after-declaring-it) – Ryan Wilson Feb 16 '23 at 13:52
  • Thanks for this feedback. I had seen similar potential to redefine a JS function. But, is this the "right" way to address the problem? It seems like a fairly standard use case, and I'm concerned I'm pursuing an approach which may be more challenging than it needs to be? – casemax Feb 16 '23 at 16:26
  • I don't see any other solution currently, other than using differently named functions or passing some kind of boolean or doing a boolean check in your functions to see if the user has logged in/signed up and execute one block of code if not or the other if so, but this opens it up to manipulation from a user to set that boolean or what not to a value to bypass your check. By loading new function definitions on successful login it would at least prevent the user from manipulating the script file or DOM to allow for it to bypass your check. – Ryan Wilson Feb 16 '23 at 16:56
  • Or you could run the functions server side and do one thing if they are logged in else do another. – Ryan Wilson Feb 16 '23 at 17:24

0 Answers0