angularjs - how to protect my routes using webtoken angular js and node js -
my routing tree
app.config(['$routeprovider', '$httpprovider', function ($routeprovider, $httpprovider) { $routeprovider .when("/",{ templateurl:"build/views/home.html" }) .when("/login",{ templateurl: "build/views/login.html" }). otherwise({ redirectto: '???' }); }]);
i want protect home.html .... accessible when login success
app.controller('loginctrl', function ($scope, $http, $window,$location) { $scope.user = {username: '', password: ''}; $scope.isauthenticated = false; $scope.welcome = ''; $scope.message = ''; $scope.submit = function () { $http .post('http://localhost:8080/authenticate', $scope.user) .success(function (data, status, headers, config) { $window.sessionstorage.token = data.token; $scope.isauthenticated = true; var encodedprofile = data.token.split('.')[1]; var profile = json.parse(url_base64_decode(encodedprofile)); $scope.welcome = 'welcome ' + profile.first_name + ' ' + profile.last_name; $location.path('/'); }) .error(function (data, status, headers, config) { // erase token if user fails log in delete $window.sessionstorage.token; $scope.isauthenticated = false; $location.path('/login'); // handle login errors here $scope.error = 'error: invalid user or password'; $scope.welcome = ''; }); };
now how make interceptor , protect home.html direct access typing url, localhost:8000/#/ -- should not accessible if logout window.
please ask if more code needed side solve problem you.... posted controller , routes.
Comments
Post a Comment