About protected pages #1403
Replies: 1 comment 1 reply
|
Yes, you can factor this out, with two caveats about how SQLPage executes files: there is no include that shares the parent page's variable context, and What works is a shared -- commons/auth_guard.sql : pass the session table name, get back the user id (or redirect)
set id_user = (
select case :user_type
when 'company' then (select id_company from session_company where session_token = sqlpage.cookie('session_token'))
when 'manager' then (select id_manager from session_manager where session_token = sqlpage.cookie('session_token'))
when 'worker' then (select id_worker from session_worker where session_token = sqlpage.cookie('session_token'))
end
);
select 'redirect' as component, '/landingpage' as link where :id_user is null;and on each protected page: exec auth_guard(user_type => 'company');
-- from here :id_user is set, or the request has been redirected awayWhy the guard must emit the redirect itself: On your COALESCE sketch: the idea is right but the syntax needs fixing. For pages shared by several user types, call the same guard without filtering on type, then branch on which table matched: set user_kind = (
select case
when exists (select 1 from session_company where session_token = sqlpage.cookie('session_token')) then 'company'
when exists (select 1 from session_manager where session_token = sqlpage.cookie('session_token')) then 'manager'
when exists (select 1 from session_worker where session_token = sqlpage.cookie('session_token')) then 'worker'
end
);
select 'redirect' as component, '/landingpage' as link where :user_kind is null;On your edit: SET variables are per-request server-side state; there is no supported way to declare a variable on one page and read it from another. Cross-request state belongs in the database (your session tables) plus a browser cookie, which is exactly what you already have. If some day all four user tables merged into one |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi again everyone,
I have a new question, but first some context :
The app I am making has four types of users : administrators, companies, managers and workers (sometimes called technical workers). Most pages are only accessible to one type of user, though some are shared between the companies and managers, as well as handlers and pages where the user is asked if they are sure about deleting some things.
Each user must log in to use the app, and I am storing session tokens in four separate tokens (one per user type) :
For each user, there is a dedicated table where the hashed password, username, etc.. are stored.
Each protected page begins with some variations of :
Where I check the user is allowed to be here, redirected to login if he isn't and the id of the user is gathered before the header is displayed. (depending on the page, the token is compared to the matching session table) (there are also different headers for each user type)
So, everything works as it is now, but I am wondering if it would be possible to tuck some part of it away to only have the code once and just have to call it.
I don't know if I can protect a page while allowing multiple types of users to have access to it (because pages accessible to multiple user types are not protected for now), I am maybe thinking of using something like :
You will have noticed the line :
I use it for a lot of different things, in pages shared between user types it helps determine where to redirect depending who the user is. So I do need it.
Please tell me if it is possible to simplify the code, I am aware that this is not the best at all, but I do not know of any better way to do it yet. I can't really make a minimal reproducible example, as it works as it is.
Thank you in advance for any help you can bring
EDIT : regarding the variable id_user, I am trying to find a way to declare varibles that I could access from all pages (it can be by calling the page where they are declared). I am trying to use the dynamic component for this without success, is this a job for a cookie ?
EDIT 2 : I found and have tried the way pages are protected in the User Management example, using only
to handle the protection. It seems to work for now and is more compact. Please tell me if this is a bad idea
All reactions