Reputation: 57
I am writing nodejs and angularjs application, and I want to create a simple authorization, that only logged-in users are allowed to see some pages.
I read about jwt (json web token) and my user now got the "token",but what should I do now ? all the routing are in the angular routing. How can I validate the routing there?
My nodeJS code :
server.use(express.static(__dirname + '/public/app'));
server.use(bodyParser.json());
My angularJS :
var app = angular.module('app').config(function($routeProvider){
$routeProvider.when('/main',{
templateUrl: 'templates/main.html',
controller: 'MainCtrl'
});
$routeProvider.when('/onlyLogged',{
templateUrl:'/templates/onlyLogged.html',
controller: 'NewCtrl
});
I want my server side to identify when the user moves to onlyLogged , and if the user is not authorized to redirect him to 401 page.
thank you !
Upvotes: 0
Views: 73
Reputation: 18065
you would need to pass that token back to the user/client/angular and store it in some service/local storage
and then pass the token to server upon each request...
this is one such sample implementation
this part of code stores it in cookie store
var success = function (data) {
var token = data.token;
api.init(token);
$cookieStore.put('token', token);
$location.path('/');
};
this is where you are passing it back with each request
angular.module('dashboardApp').factory('api', function ($http, $cookies) {
return {
init: function (token) {
$http.defaults.headers.common['X-Access-Token'] = token || $cookies.token;
}
};
Upvotes: 0