Checking if the user is signed in from front end code Question
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?
1 answer
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
}
0 comment threads