store data in Session using AngularJS -


i learning angularjs creating small application.

i storing user information in sessionstorage @ time of successful login.

logincontroller.js

app.controller("logincntrl", function ($scope, $location, angularservice) {      $scope.login = function () {         var username = $scope.username;         var password = $scope.password;          var getdata = angularservice.logininformation(username,password);         getdata.then(function (msg) {             if (msg.data == '') {                 alert('no data');             } else {                 sessionstorage.username = msg.datausername;                 $location.path('/members');             }          }, function (error) {             alert('error in login');         });     }; }); 

on successful login, redirecting user '/memberlist' , in memberlistcntrl controller checking if sessionstorage empty or not. if empty assume user trying access site without login , sending user login screen

code in memberlistcntrl

app.controller("memberlistcntrl", function ($scope, $location, allmembers) {      if (sessionstorage.length == 0)     {         $location.path('/login');         return;     }      $scope.memberslist = allmembers.data;      $scope.createmember = function (path) {         $location.path(path);     }; }); 

my questions,

  1. is right way check if user logged in or not?
  2. is sessionstorage right object store data in "session" can access through out pages or site?
  3. is there better way accomplish same?

my questions,

  • is right way check if user logged in or not?
  • is sessionstorage right object store data in "session" can access through out pages or site?

well, depends on use-case. sessionstorage gets cleared when page session ends. page session lasts long browser open , survives on page reloads , restores. opening page in new tab or window cause new session initiated.

so, if want user logout on opening tab, can continue using sessionstorgae. however, thats not case in of sites(do logout stackoverflow on opening question in 'another tab' ?).

  • is there better other way accomplish same?

yes. can use localstorage or cookies. stackoverflow keeps information in cookie named usr.

if use chrome, open console(f12).

go resources > cookies , delete usr. refresh page , you'd logged out.

gmail, facebook uses similar technique.

now angularjs provides $cookies service get/set or remove cookies.

the reason why cookies preferred on localstorage objects track login status is: cookies passed server each request/response cycle , hence available @ server side. if dont required cookie particular request, can throw status 401(unauthorized) message.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -