Created
August 31, 2015 16:32
-
-
Save electblake/406d83718de3afc468a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
angular.module('myApp') | |
.constant('AUTH_EVENTS', { | |
loginSuccess: 'auth-login-success', | |
loginFailed: 'auth-login-failed', | |
loginCancelled: 'auth-login-cancelled', | |
logoutSuccess: 'auth-logout-success', | |
logoutFailed: 'auth-logout-failed', | |
sessionTimeout: 'auth-session-timeout', | |
notAuthenticated: 'auth-not-authenticated', | |
notAuthorized: 'auth-not-authorized' | |
}) | |
.constant('AUTH_ROUTES', { | |
loginSuccess: 'home.welcome', | |
logoutSuccess: 'welcome', | |
loginFailed: 'login', | |
notAuthenticated: 'login', | |
notAuthorized: 'login', | |
sessionTimeout: 'login' | |
}) | |
.constant('USER_ROLES', { | |
authenticated: 'authenticated user', | |
admin: 'admin' | |
}) | |
.factory('authInterceptor', ['$rootScope', '$q', '$window', 'getLoginSession', '$log', function ($rootScope, $q, $window, getLoginSession, $log) { | |
return { | |
request: function (config) { | |
config.headers = config.headers || {}; | |
// console.log('config', config); | |
var session = getLoginSession; | |
if (session.token) { | |
// console.log('$http Auth', session.token); | |
// config.headers.Authorization = 'Bearer ' + session.token; | |
config.headers['x-token'] = session.token; | |
config.headers['x-username'] = session.username; | |
} | |
return config; | |
}, | |
response: function (response) { | |
if (response.status === 401) { | |
// handle the case where the user is not authenticated | |
} | |
return response || $q.when(response); | |
} | |
}; | |
}]) | |
.config(['DSHttpAdapterProvider', 'getLoginSessionProvider', function (DSHttpAdapterProvider, getLoginSessionProvider) { | |
var headers = {}; | |
var session = getLoginSessionProvider.$get; | |
// console.log('session', session); | |
if (session.token) { | |
// console.log('DS Auth', session.token); | |
// headers.Authorization = 'Bearer ' + session.token; | |
headers['x-token'] = session.token; | |
headers['x-username'] = session.username; | |
} | |
// console.log('getLoginSession', getLoginSession; | |
// console.log('DSHttp headers', headers); | |
angular.extend(DSHttpAdapterProvider.defaults.$httpConfig, { | |
headers: headers, | |
timeout: 20000 | |
}); | |
}]) | |
.config(['$httpProvider', function ($httpProvider) { | |
$httpProvider.interceptors.push('authInterceptor'); | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment