Token-Based Authentication in AngularJS — a practical guide to AngularJS token authentication with clear examples you can reuse in real projects.
AngularJS Tutorial Series (73/99). Prefer one article? Read the complete AngularJS tutorial.
Short description
Clear tokens on 401 and redirect to login.
Interceptor tip
$httpProvider.interceptors.push(function ($q, $injector) {
return {
request: function (config) {
var token = localStorage.getItem('token');
if (token) {
config.headers.Authorization = 'Bearer ' + token;
}
return config;
}
};
});