If you have an interface served by a NodeJS API, what is the most clean way to propagate status changes to the interface?
For example, I have a page that has 2 statuses, user logged in and guest. How would you send to the interface that information, without rendering the page server side? Same thing applies to permissions.
I have been working with 2 solutions, have a flag in the cookie (login = true/false) and then validate that server side on the page or resource request or render the page server side... but I don't think that neither of this is "clean". The first one because is insecure and "lose" and the last one because interface should be independent.
How are you doing this?
The user logs in and in that function I create a cookie that is sent in response:
var authData = new Array();
authData[0] = loginData.userid;
(...)
var cookie = [];
cookie[0] = { 'hash': etree.findtext('./hash'), 'login': 'true', 'customerid': loginData.customerid };
response.cookie('systemAuth', JSON.stringify(cookie), { maxAge: 86400000, path: '/', domain: url });
utils.sendResponse(__filename, __line, request, response, 201, "text/plain", "Logged");
After that if, in my interface, if I want to check if the user is logged in or if is just a guest, I have to parse the cookie and check the value of that cookie.login:
var cookie = JSON.parse($.cookie('systemAuth'));
if(cookie[0]['login'])
console.log("Logged in.");
else
console.log("Show login options...");
That's the first option.
My second is to use ejs to render the page server side:
<% if(user.loginid){ %>
logged in
<% }else{ %>
<a href='#/login'>Login</a>
<% } %>
How can I clean this up?