Logged in user details on all php pages

I have created a login api that redirects to dashboard which works fine!
and this has user logged in api that also works fine

Problem : I need the loggedin user details to be available on all pages because the data queried on the pages depends on the user details ie id, enrollement etc

Currently: For development, I added a dataview element to mimic and sort users detals based on the id that I manually add which works

I need the dataview id to be dynamically inserted as the user logs in on all pages,
I tried using session manager but I really failed
Kindly guide me on what I should do
Thanks

The key security step is to add server-side security restrict to any Server Connect API calls you make. That way data can never be accessed by anyone who shouldn't get to it.

in addition to page restriction, on the client-side, you can run an SC call to retrieve the user details using a query based on their Security Provider identity

Are you just starting out or is NodeJS an option? I find page security one of the (many) areas that NodeJS is easier to use.

Some more info on page restriction for PHP:
https://docs.wappler.io/t/security-restricting-access-to-your-page/2853

and for NodeJS:
https://docs.wappler.io/t/applying-security-to-your-nodejs-pages/21682

thanks but my problem is not about security for now. My users are lecturers and each lecturer teaches different course units meaning after login the information queried on the pages is specific to that lecturer ie units taught , students taught , etc. what I need is the ability for that lectuers details such as Institution id, lecturer Id, school Id, Department Id , to be available on all the different pages because the information to be queried ont these pages is based on the lectuer infomation


in that image I manually put the lecturer id to be 14
in the data view
so it queries there info

See the data View Setup

I need the data view to pick the id automatically after login
on all different pages

based on the Docs I think Sessions could work but I have failed to make them work any ideas??

Sessions won't work. If user closes the browser tab it could lose the session.

What I do, not sure if there is another way, but it works great.

I create a single query (serverconnect) to get user details and add it to my header include file so it is available on every page of the dashboard. This gives me access to all of my user's data.

You can just put the database query step in the globals. It will be loaded on every page where you have a server action added.

1 Like

Thanks Brad let me explore this option aswell
sounds interesting

Ohh okay let me look at that too thanks Teodor

I have followed this and I see it available but how exactly do I set in this case that session value in globals to be equal to the logged in lecturer??

I have tried inserting that user loggedin api on a page with header that i use as page include on all pages which after login some times works well but later loses the identity of the user so it keeps getting on and off How do I fix it

HOW DO I FIX IT TO BE SURE THAT THE SYSTEM DOESN'T LOOSE THE LOGGED IN OR QUERIED USER FROM THE API as I browse through different pages


Here is page include structure


And Here is one of the front end pages for course units that have to be queried based on the user loggedin id/ details that should be passed on by the page include (header) since it contains the api for logged in user
but some times it works and other times it fails
How do I fix it

well does the "Remember" option on the login step have a value? This will ensure a login cookie is created.


Yes The remeber option Is set to 1 but the system still looses the user and in this case the user data can not be queried because every thing depends on their ID that is obtained from the Login API

Please explain this in more details. What exactly is happening?
What data and how exactly are you sending to the pages? What is the server action response? When is your data "lost"?

It looks like something is not correctly set, but i can't tell that as your screenshots are not really helpful.

This is the procedure that I would take (in your case 'customers' would read 'user' and 'email', 'username'):

  1. Create a Security Provider:

  1. Create a Security Identity in Globals

  1. Create a 'customer_logged_in' API. Start with a condition that the 'customer_identity' exists (i.e. the customer has logged in). If so, then show the customer details as per single query where the condition is the customer.id == 'customer_identity'.

  1. On the frontend, you can use Server Connect to connect to 'customer_logged_in' on any page you wish.

For more, view this video:

Thank you So Much Ben for the advice!
I have done all those steps and am doing this in PHP
1 I have perfectly working security provider and identity in globals
2. I have working login API and userlogged in API
3. login step and getting the logged in user work very fine SOMETIMES other times Login works and querying logged in user fails.

THE IDEA
My users are lecturers each teaching different units, giving different course works etc and their id is linked to these different tables in the database
So on login their Lecturer id is used to filter data ie courseunits whose lecturer is == to loggedin lecturer and this info is displayed.

On all my pages the lecture id is important to query info since its used as a filter parameter using that dataview element.

PROBLEM
Login API works very fine But userloggedin can work like on the dashboard right after login then when a user redirects may be to course units page, the system completely loses the logged in userId regardless of the userloggedin API being present on that page
Then may be I choose to logout and login again it works on dashboard, maybe redirect to course uniti page and still works fine (yet failed in the prvious attempt) , then may be on redirecting to assignments the loggedin user id now is lost so the whose system and all pages loose the content because the dont have a userid to query.

THEN on logout and relogin some times the loggedin user may be queried and things work or still after login the id is lost so no data is queried and the cycle repeats its self.


This image shows successful login and lecturerid used properly and so their active course units are queried
But on redirecting to the assignments page the Id was lost and nothing was shown see next image please


in the same login where i took the previous course image, I redirected to assignments and the lecturer id was lost so no assignment is queried

I tried putting the userlogged in API on all pages using server connect but the same problem occured
SO I used BRAD's advice to use a header with the userloggedin API so that I put the header as a php require include on all pages which I did and so some times it works and queries the info based in the lecturer id and other times it fails.

I should also inform that the Failure or situation of losing the logged in lecturer does NOT hapen on a specific page, today it can fail at the dashboard, nextime it fails after redirecting to assignmemnts the othertime they all work fine and may be fails when you come back to the dashboard

and when the user ID is lost all pages are empty because their infomation is filtered basing on the current loggedin lecturer ID

Something is probably not set up properly on your pages. I can't tell what is wrong by looking at your screenshots, the issue is either in the user details server action or the way you set up the login.
Please provide a link to a page where the issues happen with some login details that at least we can check.