my auth system behaving strangely, , realized access.js
, file sends auth tokens in headers, gets loaded after client has made request server. server log:
example app listening on port 3000! couldn't find yer token, matey gonna load page up! / 304 3.765 ms - - couldn't find yer token, matey /access.js 304 1.031 ms - - couldn't find yer token, matey nice token, mate /main.html 304 2.192 ms - -
this creates effect every request gets response corresponding token not being there. here server.js
:
var authenticator = function(req, res, next){ var token = req.headers['authorization']; if (token){ var authenticated; jwt.verify(token, app.get('supersecret'), function (err, decoded){ if (err){ console.log(err); authenticated = false; req.decoded = null; } else { req.decoded = decoded; authenticated = true; console.log("nice token, mate"); } }) } console.log("couldn't find yer token, matey"); next(); } app.use(authenticator); app.get('/', function(req, res){ console.log("gonna load up!"); if (req.decoded == null){ res.sendfile('/home/daniel/app/public/index.html'); } else { res.send("gold!!"); } });
and here access.js
:
access.factory('authenticationinterceptor', ['$q', '$location', '$localstorage', function ($q, $location, $localstorage) { return { 'request': function (config) { config.headers = config.headers || {}; if ($localstorage.token) { config.headers.authorization = $localstorage.token; } return config; }, 'responseerror': function (response) { if (response.status === 401 || response.status === 403) { $location.path('/login'); } return $q.reject(response); } }; }]); access.config(['$routeprovider', '$httpprovider', function($routeprovider, $httpprovider) { $httpprovider.interceptors.push('authenticationinterceptor'); $routeprovider .when('/', { templateurl: 'main.html' }) .when('/register', { templateurl: 'register.html', controller: 'createsession' }) .when('/login', { templateurl: 'login.html', controller: 'createsession' }) .otherwise({ redirectto: '/' });
i'm not sure how fix this. way can send token without relying on server serve file? or there routing scheme can devise makes initial request take care of logic?
Comments
Post a Comment