walnut_launchpad.html/js/services/auth.js

70 lines
1.9 KiB
JavaScript

app.factory('Auth', [
'$rootScope', 'localStorageService', '$location', 'azp@oauth3.org'
, function($rootScope, localStorageService, $location, Oauth3) {
var dapSession = 'dap-session';
var dapSessions = 'dap-sessions';
var Auth = {
setUser: function (currentUser) {
if (redirectedURL === '/splash-page') {
$location.path('/home');
} else {
$location.path('/' + redirectedURL);
}
localStorageService.set('userAuth', JSON.stringify(currentUser));
},
isLoggedIn: function () {
Auth.restore();
return Auth.session || false;
},
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));
}
, 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
, oauth3: null
};
Auth.oauth3 = Oauth3.create(window.location);
return Auth;
}]);