How to conditionally show content if user is logged in

I’m going really well with Wappler but am struggling with documentation being older than Wappler and some bits not being there at all.

I have a nav menu which is in an include file. I want to only show the ‘Log out’ menu item if the visitor is logged in. I’ve added the Conditional Region component but I just can’t see what needs to go in the ‘Condition’ field.

Can you give me a steer in the right direction?

1 Like

Hello ,
Please watch this tutorial you will find your answer in this tutorial

2 Likes

Please check the documentation section, it’s explained how to create a logout button on your page: Logout - Create a Working User Logout Button
As for the condition value - select the identity binding returned from your user details action. Click your button, select dynamic attributes - show: and bind identity binding.
If it has a value, the button will be displayed.

1 Like

Thanks for your replies. I’ve already got everything working, including the log out link, that’s not the problem. I want to put in a conditional so that ‘Log out’ in the nav menu is only shown when they are logged in. It’s pointless having it in the menu when they’re not logged in. I just can’t find the info to get this bit finished.

Please see my reply above:

1 Like

Thanks Teodor. I missed that in your reply, sorry. I’ll follow that now.

Hmm, I’m really trying to achieve this without asking questions but I’m just not able to.

Can you give me some steps to achieve the ability to hide some output if a user is logged in?

I’ve followed the instructions for using the security enforcer and I have the login page working and have also set other pages to be restricted to logged in users. I haven’t used a user details action (I can’t even find this in Wappler) so am at a loss how to proceed.

Sorry.

Do you have a server action returning any of the logged user data? Or anything related to user?

Aah, I don’t think so. All I have at the moment is the facility to log someone in and then show a page which requires being logged in. And I’ve made a logout feature. That’s it.

You can check the logged user session in this case.
When your users log in, a session is being created. Its name is security provider step name + Id. If your security provider step (in server action) is called “userSecurity” the session will be called:

userSecurityId

In order to check this, in app connect structure add session manager, and add this session there.
Then use the dynamic attributes - show - and select the session as a condition.

Brilliant, thanks Teodor. I think I’ve been fixated on building the site without touching code so overlooked some simple coding which I would have normally done anyway. I added a simple

if(isset($_SESSION[‘siteSecurityId’])) {
OUTPUT
}

and it’s working as it should.

You can do this without PHP - just by using dynamic attributes in Wappler.

Thanks again. Is there a tutorial for that? I’ve added a $_SESSION in the Globals section of the server actions and have added the dynamic attribute for ‘hide’ but can’t see how I put in the expression.

You don’t need to add anything in server connect.
The session is being created automatically on log in!

Check my explanation from the last post please:

1 Like

@Teodor Can you explain how I add the session within Session Manager as you describe please?
Everything I enter is forced to lowercase and the binding doesn’t seem to pick up that I’m logged in.

Thanks in advance.

The session created in the server action (by log in step) is a server session.
The sessions in Session Managee in App Connect is a browser session.

You cannot pick sessions created in the server action with the session manager in app connect, as these are different things.

In order to use the session of the logged user, you can use php code:

Otherwise use a query which provides user details, and if it doesn’t return any, hide the log in button.

2 Likes

Thanks for clarifying.

I find this bit a little confusing. If I knew the user id to query by, then wouldn’t I already know that the user is logged in, or am I missing something here?

@niall_obrien,

What I did was create a user details query to retrieve the details of the logged in user.

I then put in on my SPA index page, so it is called when the page is loaded.

I then referred to it on the SPA child pages by adding it to the show/hide dynamic attributes or server connect data displayed on the page.

It is important to note that with SPAs, any queries on the index page are not currently available in the data picker, so you would need to manually add it in.

So for example on my SPA index page, I have

<dmx-serverconnect id="sc_get_user_details" url="dmxConnect/api/tables/webu_users/public/list/get_user_details.php"></dmx-serverconnect>

On my SPA child page, I have

<p>Hello {{sc_get_user_details.data.query1[0].webu_firstname}} !!</p>

To only show the content on the SPA child page if logged in, you would add a Dynamic Attribute>Show with the {{sc_get_user_details.data.query1[0].webu_firstname}} as the When.

<p dmx-show="{{sc_get_user_details.data.query1[0].webu_firstname}}">Hello {{sc_get_user_details.data.query1[0].webu_firstname}} !!</p>
3 Likes

Check the docs as well:

2 Likes