Store data for session with an expiration date - javascript

I am building a mobile application in Angular (Ionic 5 to be precise) and I need a way to keep session data during my users workflow.
I considered using the sessionStorage for that, but one important thing is that my user session should expire automatically after 5 minutes. So I would like to store data and reset those data if the user finish the workflow or reset them if the user didn't finished the workflow within 5 minutes.
What is the best way to do this?

Store the expiry time along with the data in sessionStorage and when reading the key, check the time to make sure it is still valid for usage.
See this article for reference.
I can't include a Stack Snippet here because it fails on security privileges (editing the localStorage etc.), so here's the link to try this on GitHub.

You can use the user reaction events(click, keypress, ...etc) to refresh your session and made any control you want ! something like (click)="refreshSession()". All your controls will be made inside the refreshSession()

Related

Is there a way to cache JSON on client side for limited time in order to avoid making 2 GET requests to the API if the user visits the same page twice

Imagine this scenario - A user visits their profile page on URL /user/username and the component that loads on this URL has to make 1 GET request to the API in order to obtain the information about the user with that username. So far so good, however, if the user visits another URL and then decides to come back to the profile page with URL /user/username, the component makes a new GET request for the same information that it got earlier which leads to 2 drawbacks - the information doesn't appear instantly as the component has to wait for the GET request and I'm making a second call to the API.
This is why I am wondering if it's possible to somehow cache that information so that when the user visits his profile page again, the component wouldn't have to make a second GET request. Also this cached information should be able to expire after a certain amount of time like an hour so that it is never inaccurate.
Is this achievable and worth it?
I personally would first try the http-caching solution as mentioned by "Sudhakar RS", but next I would try session storage. There is a vue-sessionstorage plugin as well. Of course your data then would be valid for the session. Should your session be longer than the hour you mention you would have to manually handle that as part of the data you save with a timestamp. There is a sizelimit of I think 5MB for all session storage data. If you need more then I would go with what "Hung Nguyen" suggested.
You can use browser Local Storage or WebSql or IndexedDB to store data on browser but be careful, they has limit size.
You should cache data on server side instead client side, by this way you will have more privilege to manage your data (caching size, flush cache, ...)

Change ASP.NET session timeout programmatically

Here is a new requirement that I need help with. Our users request that 2 minutes before the session timeout, warn them. (i can use a global javascript to check on every page since once a page is loaded, the session reset and by default, another 20 minutes is extended). at the 18th minute, a javascript popup shows up, asking the user "You have two minutes left before being logged off. Do you want to extend the session"?
Up to here, all is fine. But then once they hit "Extend it", then what? I don't want to refresh the page because the data they've already entered will be lost. Is Ajax needed? If so, what is the programmatic way to extend the current session? (not modifying web.config just to be clear)
Also, say they are talking to someone and did not see the javascript confirmation during the 2 minute. Is there anyway to "hold" the session, till the user decides to do something?
Thanks
I was recently working on a similar problem. With ASP.Net every call back to the sever resets the session timeout period. So a Ajax call is going to be your best bet.
As for holding the session, are you actually storing anything in the Session object that needs to be maintained? Or when you say session do you mean the period that the user is authenticated for? If it is truly Session and you are not storing data then it shouldn't matter id it expires. You may want to take a look ar the below link.
Forms authentication timeout vs sessionState timeout

How to maintain state of an app after a refresh, AngularJS?

I'm trying to make a basic social networking application following Write Modern Web Apps with the MEAN Stack book.
The end result should be: https://mean-sample.herokuapp.com/
I got through to getting user accounts set up, having a user log in and create a personalized post. But as soon as I refresh, the user gets logged out.
What am I doing wrong? And how do I fix this?
In the client side we cant maintain the session, we need the server support to it. There are many ways to maintain the session
1 Token based, for each request to the server, the server will check whether token exists or not.
2 We can store in the localstorage while refresh the rootscope will be, at that time we can take from local store and populate the page objects.
Maintaining in server side is secured and advisable.
use localstorage, it will help you in maintaining the session and also store user details temporarily.
Refer this for more details
https://github.com/grevory/angular-local-storage

How to ensure something definitely stays in localstorage despite size limit

I'm writing an app in Angular JS and plan to use local storage for keeping the logged in user details instead of using cookies.
The trouble is that I may want to use local storage at a later date to store other things, like caching search results and other data. I know that an exception is thrown if the browser storage limit is hit and that's not a problem for other data, but if the storage is full and the user logs in then it would fail if the account details couldn't be stored.
Is there an obvious solution here, other than to write a bunch of code that prioritizes what I should delete if the limit is hit and then deleting something less important and trying again?

Save Form Data for when user returns

Here is my problem...
We have a very large Form with many inputs and check-boxes, the problem happen when the user's pc disconnects he then need to restart the form.
After doing many Google searches I've found a few solution but i have no exp using any of the following and would like to know which solution is better used.
Save a session with post variables so that when user returns his data would be saved. (problem is with session destroyed when browser leaves page.)
Save the post variable to a temporary table, and if host name of user is there to populate the form to continue where he left off. (Probably the simplest way)
Session Storage and Local Storage, Both of these seemed like a good alternative but haven't seen any examples or any docs on how this can be used to populate forms.(No Exp with this.)
I'm thinking of using the second option and just wiping that table after 1 hour but would like to know which is better in terms of what is more widely used for this solution.
Thanks
you can send the data with the onBlur on the textfields with ajax post to a php file which writes them into the session or a coockie or a database.
Session ends when the Browser(not the tab) has been closed. Coockie must be aktivated by the user. So i think the temporary table is the best solution for you.

Categories