javascript - using auth header to prevent access to page -
on login action i'm storing inside localstorage username
, token
logged user afterwards i'm creating auth. headers post request.
update: more specific on this. have userservice
succ. put , retrieve users data localstorage, have username
, sessionid
, , islogged
.
my question is: having auth. header, info need logged user inside localstorage, how can write event handler check if user logged in before each route change.
should on app.js init app , inject userservice? if yes how.
(function () { "use strict"; var app = angular.module("myapp", ["common.services", "ui.router", "ui.mask", "userservice", "ui.bootstrap"]); ....
how can use stored auth header in order access/deny access specific pages?
update 2: app.js
(function () { "use strict"; var app = angular.module("myapp", ["common.services", "ui.router", "ui.mask", "userservice", "ui.bootstrap"]); app.config(["$stateprovider", "$urlrouterprovider", function ($stateprovider, $urlrouterprovider) { $urlrouterprovider.otherwise("/"); $stateprovider .state("index", { url: "/", templateurl: "app/index.html", }) // restricted .state("home", { url: "/home", templateurl: "app/home/home.html", controller: "homecontroller vm" }) }] ); }());
i think right approach problem use resolve property in route, user can't navigate pages if isn't logged in , once logged in can inject user object controller
for example navigate home page must logged in
.when("/home", { templateurl: "homeview.html", controller: "homecontroller", resolve: { user: function(authenticationservice){ return authenticationservice.getuser(); } } }) app.controller("homecontroller", function ($scope, user) { $scope.user = user; });
good example: https://www.sitepoint.com/implementing-authentication-angular-applications/
Comments
Post a Comment