walnut_launchpad.html/js/services/auth.js

67 lines
1.8 KiB
JavaScript
Raw Normal View History

2017-08-14 19:35:53 +00:00
app.factory('Auth', [
'$rootScope', 'localStorageService', '$location', 'azp@oauth3.org'
, function($rootScope, localStorageService, $location, Oauth3) {
2017-08-14 23:00:01 +00:00
var dapSession = 'dap-session';
var dapSessions = 'dap-sessions';
var Auth = {
setUser: function (currentUser) {
2017-08-10 22:33:54 +00:00
if (redirectedURL === '/splash-page') {
2017-08-11 21:37:06 +00:00
$location.path('/home');
2017-08-10 22:33:54 +00:00
} else {
2017-08-11 21:37:06 +00:00
$location.path('/' + redirectedURL);
2017-08-10 22:33:54 +00:00
}
2017-08-14 23:00:01 +00:00
localStorageService.set('userAuth', JSON.stringify(currentUser));
2017-08-09 18:41:43 +00:00
},
2017-08-14 23:00:01 +00:00
isLoggedIn: function () {
Auth.restore();
return Auth.session || false;
2017-08-11 15:09:41 +00:00
},
2017-08-14 23:00:01 +00:00
getProfile: function (profile) {
Auth.restore();
return Auth.session || false;
}
, add: function (session) {
var obj = JSON.parse(localStorage.getItem(dapSessions) || 'null') || {};
var dapName = 'dap-' + session.subject + '|' + session.issuer;
console.log('session', session);
Auth.session = session;
Auth.sessions.push(session);
localStorage.setItem(dapName, JSON.stringify(session));
localStorage.setItem(dapSession, dapName);
obj[dapName] = true;
localStorage.setItem(dapSessions, JSON.stringify(obj));
2017-08-09 18:41:43 +00:00
}
2017-08-14 23:00:01 +00:00
, restore: function () {
var dapName = localStorage.getItem(dapSession);
Auth.sessions.length = 0; // don't overwrite with a new array, keep original references
(Object.keys(JSON.parse(localStorage.getItem(dapSessions) || 'null') || {})).forEach(function (name) {
var session = JSON.parse(localStorage.getItem(name) || 'null');
if (session) {
session.email = session.subject;
}
if (dapName === name) {
Auth.session = session;
}
Auth.sessions.push(session);
});
return Auth.session;
}
, sessions: []
, session: null
2017-08-09 18:41:43 +00:00
};
2017-08-14 23:00:01 +00:00
return Auth;
2017-08-09 04:23:19 +00:00
}]);