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\$ Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving the cleanliness of existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! \$\endgroup\$ – Heslacher Aug 1 '14 at 12:07
  • \$\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
  • \$\begingroup\$ As this site is called Code Review ... No \$\endgroup\$ – Heslacher Aug 1 '14 at 13:04
  • 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

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.