What is the best approach to work with data while using token based authentication

102 views Asked by At

I am building an sample application that lets user store comments. I've created the registration and login process. When the user registers, his details are stored in a MySQL database and a token is returned to the browser. Now he can access the Profile page.

When an existing user logs in he is redirected to the profile page. The profile page is accessible only when a user registers or logs in.

After logging in, I want to show all his comments if he has already added them.

My frontend is in Angular and backend use Laravel. For authentication I use Satellizer.

I want to know, what is the best approach while playing with data, considering the fact that the user will add, edit his comments. Should I use localstorage and store data in a key value pair or should I create a json file which gets updated everytime the user adds a comment or makes a change.

I wanted to know what is the most efficient way to deal with data from server so that the application is fast even when it scales to a 10000 users and lot of data for each user.

Thanks

2

There are 2 answers

0
Andrew Khan On

You should be updating it on the server when changes are made rather than only relying on localstorage. You can use localstorage to cache, but it should only be for immutable data, it shouldn't really be used for data that is going to change.

So in this case you'll be adding and updating new comments via your API (ideally a RESTful one!). Once you've made a change, you could store the comments locally and only update them when the user makes a new comment, however you'll quickly run into issues where the data is invalid on different clients. (i.e. if you update the comments on a different computer, the other computer won't be aware).

Alternatively, you could cache the comments and then simply ping the server to find out if new comments have been added. This could be using a HEAD request for example to check the last modified date on your comments resource.

6
Tzook Bar Noy On

You can store comments data locally on user browser, but you should properly manage it.

I don't how much load your server will have and if the time invested now worths it.

  • You can fetch comments and store them locally
  • User adds a comment, then you update locally and send a request to the server
    • You need to track the request response, if requests fail so notify user and remove comments from local.
    • if request was successful so you can continue on your way.

** facebook uses this "success first" approach user does an action, and he see it happens instantly, in the background it could take few seconds, only if it fails they will notify you.

** look at their commenting process, when you comment, it appears instantly, no loading... but in the BG the load happens.