Calling server routes through react?


#1

I am currently working on a react app on osjs and have a route set up to make an api call. The route works perfectly but when trying to use the route through react and axios the calls get a 404 error. Any tips on how to solve this?


#2

If it’s a 404 then it’s a path issue.

I need more context to help you out. Some code examples would be great.


#3

Yes it was a path issue and it has been solved but I also have another issue is it possible to save a variable in a process in such a way that the variable is created when the user logs in, and when the user logs out that the variable is cleared. I tried using the settings variable but im not sure how to clear the setting variable once the user logs out.


#4

I’m not exactly sure what you mean by “when the user logs in” with “variable in a process”.

Do you mean some kind of global variable ? If not, then I’m not really sure what you would need with this.


#5

What im trying to create is an application that creates a one time password and once the password is verified the user gets to see the applications content because the users password has been verified and the one time password does not show again until the user logs out. Once they log out and log back in they get shown the one time password again because they have not been verified but if they were to just close the window and open it back up it without logging out the password does not show in the window. I am trying to get this done by making a variable that does not get undefined when the window is closed and the password is verified but the variable only becomes undefined when the user logs out from osjs or the users password hasnt been verified. Once the user logs back in the variable is created again. My question is the way im planning to solve the problem possible in osjs. If this explanation does not make sense I will try elaborate on it again.
Sorry its long and their might be some mistakes.


#6

You can use core.on('osjs/core:logged-out', () => {}) to capture a logout signal, however, since this happens during teardown and is a side-effect this will not allow you to use the settings APIs.

So I think you can solve it in the following way:

Example implementation:

osjs.register(applicationName, (core, args, options, metadata) => {
  const hasOtp = localStorage.getItem('MY_OTP')

  core.on('osjs/core:logged-out', () => {
    localStorage.removeItem('MY_OTP')
  })
});