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'));
    console.log("Logged in.");
    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?

  • \$\begingroup\$ Doesn't the same thing apply to processes? I have applications working as described and I'm looking for a more clean/"correct" way of doing that... Isn't that and the code review concept the same thing :S ? \$\endgroup\$ – José Neves Aug 1 '14 at 13:02
  • 3
    \$\begingroup\$ The difference is that we review Code, not designs. The workaround? Let us review the implementation of the design in code. \$\endgroup\$ – Pimgd Aug 1 '14 at 13:05
  • \$\begingroup\$ Check if it's according now, pls. \$\endgroup\$ – José Neves Aug 1 '14 at 13:28
  • \$\begingroup\$ What do you have against rendering on the server side ? \$\endgroup\$ – konijn Aug 5 '14 at 18:22
  • \$\begingroup\$ Rendering server side implies no separation between the front and the back end. \$\endgroup\$ – José Neves Aug 6 '14 at 9:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.