Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Comments on Checking if the user is signed in from front end code

Parent

Checking if the user is signed in from front end code Question

+1
−0

When writing front end code for QPixel, is there a preferred way to check whether the user is signed in?

The back end code has access to this information, and constructs the page accordingly. Some page elements are present or not based on whether the user is signed in.

The front end code could check for the presence of an element that is only present when the user is signed in, but this seems brittle - any changes to the naming or presence of elements in future could break the check and make it look like the user is not signed in when they are.

Is there a variable passed to the front end specifically for the purpose of checking whether the user is signed in? If there is not yet, would it be useful to create one? Is there a particular place in the HTML structure that would be best to place such a variable? Is there a convention for this?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

Post
+1
−0

When you say front-end code, do you mean views or JavaScript? The answer is different.

Views are the client-side code, but are generated server-side so you have access to things like user_signed_in? and current_user.

JavaScript has the QPixel JS API that you can use:

const user = await QPixel.user();
if (!!user.error) {
  // not signed in or some error - handle it here,
  // else use the user object for whatever you need
}
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

I meant views making the value available to JavaScript (1 comment)
I meant views making the value available to JavaScript
trichoplax‭ wrote over 1 year ago

Thanks for the double explanation. I found QPixel.user() previously but I noticed that the first time it is called it makes an AJAX request, which seems redundant if the user is not signed in. So the reason I asked this question is because I was wondering if functions like QPixel.user() and QPixel.preferences() could check whether the user is signed in first, and only if they are signed in make the AJAX request.

I was wondering if the views could add the value of user_signed_in somewhere in the finished HTML, perhaps as a data attribute on the body. Then JavaScript functions that only need to run when the user is signed in could check that data attribute first.

I guess the benefit of this would be small except for users on a slow connection, but is there any drawback?