user2999165
user2999165

Reputation: 45

How to use multiple different localStorage in a single domain?

I have a website in which there are 3 different folders to be accessed by 3 different users. say if you accessed www.thissite.com/folder1/index.html it will check if user is logged and redirect to login page if not.

I used localStorage to do so since i don't want to deal with cookies and session because of IOS issues.

I have this code that assigns a default object to localStorage. It work fine for single folder. but when i try to use it on other folders it's going directly to the lading page of the folder and not calling login page.

How can i use the similar code with localStorage for different folder? How can i make 3 unique localStorage for each folder so when accessed will still be protected and ask for login?

Here's my code:

index.html

             if(localStorage['data']){
             var data=JSON.parse(localStorage['data']);
             if(data.logedIn==1){

             }else{
              location.href="http://example.com/test/login.html";
             }
            }
            var folder1 = "http://example.com/test/";

            var defaultInfo=[
             {folder:'folder1',count:1,pageLoadCount:1},
            ],
            data=localStorage['data']?      JSON.parse(localStorage['data']):defaultInfo;//load

            console.log(data);

            if(data.pageLoadCount==0){
             location.href="http://example.com/test/login.html";
            }

            data.pageLoadCount++;
            data.count=dir1.pageLoadCount;

            localStorage['data']=JSON.stringify(data);

login.html

            <script type="text/javascript">
            function test() {
            var userName = document.Form1.id.value;
            var password = document.Form1.pass.value;
            if ((userName=="moulinex") && (password=="abc123")) {

                window.open("http://example.com/test/index.html");

                } else {
                alert("Invalid ID or Password!")
                }
            }

            </script>

i place this on the below of login page

    <script>
            localStorage['data']=JSON.stringify({logedIn:1})
            </script>

Thanks a lot in advance. All/any answer is very much apprecaited.

Upvotes: 0

Views: 1063

Answers (1)

Steven Lambert
Steven Lambert

Reputation: 5891

LocalsStorage is name/value dictionary. If you just use the data name, you will override any previous values in it. Instead, use three different names (one for each folder) and set their data that way:

localStorage['folder1'] = JSON.strinfify({logedIn:1});
localStorage['folder2'] = JSON.strinfify({logedIn:0});
localStorage['folder3'] = JSON.strinfify({logedIn:1});

Alternatively, you could just create one object into localStorage and populate the different folders that way.

localSotrage['data'] = JSON.stringify({
                          folder1: {logedIn: 1}, 
                          folder2: {logedIn: 0}, 
                          folder3: {logedIn: 1}
                       });

Upvotes: 1

Related Questions