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)