1

I am new to AngularJS.

When user login with email-id then I will be getting the json response from backend as below with dynamic session-id of 12 digits

JSON Response

{
      "json": {
        "response": {
          "statuscode": "0",
          "statusmessage": "Success",
          "user": [
            {
              "sessionid": "56756",
              "groupdetails": {
                "group-id": 216,
                "group-code": "ABC",
                "group-name": "School BSK"
              },
              "memberdetails": {
                "member-id": 22,
                "member-name": "Arya"
              }
            },
            {
              "sessionid": "123456789012",
              "groupdetails": {
                "group-id": 215,
                "group-code": "XYZ",
                "group-code": "College ABC"
              },
              "memberdetails": {
                "member-id": 223,
                "member-name": "Arya1"
              }
            }
          ]
        }
      }
    }

I need to fetch the value from response and store each session id separately with different variable name. But the problem is the user can have any number of accounts. Each account will have a different session id.

I don't know how to fetch the value dynamically and store all the session id separately and use further.

The response I get from the backend using the below code.

Login.controller.js

(function () {
    'use strict';

    var app = angular
        .module('app');


    app.controller('LoginController', LoginController);

    LoginController.$inject = ['$location', 'AuthenticationService', 'FlashService'];
    function LoginController($location, AuthenticationService, FlashService) {
        var vm = this;

        vm.login = login;

        (function initController() {
            // reset login status
            AuthenticationService.ClearCredentials();
        })();


        function login() {
            vm.dataLoading = true;

            if (vm.username.indexOf('@') != -1) {
        AuthenticationService.Login(vm.username, vm.password, function(response) {
            if (response.json.response.statuscode == 0 && response.json.response.statusmessage=='Success') {
                AuthenticationService.SetCredentials(vm.username, vm.password);
                $location.path('/');
                console.log(response);
                if (typeof (Storage) !== "undefined") {

                    var userdata = response;

                    localStorage.setItem('userdata', userdata);

                    }
                    else {
                        alert("Sorry, your browser does not support web storage...");
                    }
                } else {
                        FlashService.Error(response.json.response.statusmessage);
                        vm.dataLoading = false;
                    }
                });
                } else {
                    AuthenticationService.adminLogin(vm.username, vm.password, function(response) {
                        if (response.json.response.statuscode == 0) {
                            AuthenticationService.SetCredentials(vm.username, vm.password);
                            $location.path('/admin');
                            console.log(response);
                        } else {
                            FlashService.Error(response.json.response.statusmessage);
                            vm.dataLoading = false;
                        }
                    });
                }
            }
    }

  }) ();

My response is inside AuthenticationService.Login(vm.username, vm.password, function(response)

2
  • Not familiar with angular, but just an idea on how to store the values; you could just have a variable/cookie array and story any keys in there? Commented May 18, 2016 at 8:47
  • Try github.com/grevory/angular-local-storage. see the documentation for configuration. Commented May 18, 2016 at 9:08

1 Answer 1

1

The easiest way to implement local temp storage for you variables is to use caching for your $http requests:

$http({
cache: true,
 url:url}).then(function(rs){});

Angular's native caching works fine but can't store in a local sqlite db, for that you need a library:

http://jmdobry.github.io/angular-cache/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.