How to conditionally show content if user is logged in

Thanks, there’s no way I would’ve known this. Have you run into many other “gotyas” while creating a SPA with Wappler?

1 Like

@niall_obrien,

Just small things that are specific to SPAs. What I would recommend is searching the community with the phrase “SPA” almost anything I have encountered has been posted, but there is always the possibility of something new that has not been considered.

1 Like

Thanks @Teodor - so what I’m doing at the moment is running a userDetails server action to see if they’re logged in or not, and it works great, except for one thing; when they’re not logged in I obviously get a 401 error in the console. I’ve tried handling it via the Unauthorized Dynamic Event, however, it’s not like I can select a “do nothing” action (I don’t want to do anything if it returns 401). Besides, no matter what I action I pick, the error is still thrown in the console. Eg. If I alert the error (works fine), the exception is still thrown in the browser.

How can I handle this?

So what is it actually doing then?

Well, that’s what is it supposed to do, when there’s no user logged in, the action returns status 401. What is it supposed to return when no user is logged in?

In this case, I have a register page. I’m doing the userDetails check so that if the user is already logged in, I redirect away from the register page. If they’re not logged in, ie. 401, then I want to do nothing (and leave the page render as normal).

Yes, I expect it to do that, but I would no longer expect it to throw an exception in the console if I handle the Unauthorized event (even if its Action is left empty). Ideally I’d like some way of suppressing the exception error in the console.
image

That’s not possible, the server doesn’t know what you are doing on the front end. It’s PHP code that runs on the server, returns the status and after that the page is rendered in your browser.

So, what’s happening on status 401 now? Isn’t it staying on your page?

Yeah, the rendering is fine thanks. Any other suggestions on how I can accomplish the same thing without throwing exceptions etc.?

In the server action you can check if the user session has a value or not, with a condition step (so without using the security provider step).
This way the response will always be 200.

1 Like

Unfortunately that’s not ideal as I use the 401 response to show/hide the page content too.
What I mean is that if the user is already logged in and they visit /register, if I just do a redirect, the register page content is visible for a split-second, so I use the 401 to hide the content and then redirect. If they’re not logged in, then it will show the content (but obviously won’t redirect).
Maybe I’m going about it all wrong, I’m not sure.

You can use Set Response step and set whatever status you like in server connect/condition step.

1 Like

How do I do this exactly, set the condition to $_SESSION["siteSecurityId"]?

Add your session name under GLOBALS > SESSION.
Then use the session as a condition expression.

2 Likes