making it better

This commit is contained in:
aj 2017-08-14 23:00:01 +00:00
parent a7d93827d1
commit ff8d740da1
6 changed files with 91 additions and 31 deletions

View File

@ -18,7 +18,7 @@ app.config(['$stateProvider', '$urlRouterProvider', 'localStorageServiceProvider
views: {
'header': {
templateUrl: '/templates/partials/header.html',
controller: 'HomeController',
controller: 'SignInController',
controllerAs: 'vm'
},
'menu': {
@ -34,7 +34,7 @@ app.config(['$stateProvider', '$urlRouterProvider', 'localStorageServiceProvider
views: {
'content@': {
templateUrl: 'templates/home.html',
controller: 'HomeController',
controller: 'SignInController',
controllerAs: 'vm'
}
}

View File

@ -17,4 +17,5 @@ app.controller('HomeController', [
return profile.email;
};
throw new Error('loaded home controller');
}]);

View File

@ -9,10 +9,22 @@ app.controller('SignInController', [
vm.timers = {};
vm.defaultIssuer = 'provider.' + location.host.replace(/^cloud\./, '');
vm.session = Auth.session;
vm.sessions = Auth.sessions;
vm.showAdvanced = true;
vm.toggleAdvanced = function () {
vm.showAdvanced = !vm.showAdvanced;
vm.independentIssuer = !vm.independentIssuer;
}
vm.notification = true;
vm.signOut = function () {
Auth.signOut();
$location.path('/splash-page');
};
vm._setSubject = function (subject) {
vm.currentSubject = vm.newSubject;
subject = subject || vm.newSubject;
@ -69,14 +81,14 @@ app.controller('SignInController', [
};
vm.selectSession = function (session) {
vm.session = session;
vm.oauth3.init({
location: location
, issuer: vm.currentIssuer
, audience: vm.currentIssuer
, issuer: session.issuer
, audience: session.audience || session.issuer
});
};
vm.oauth3.sessions = vm.oauth3.sessions || [];
vm.instaauth = function () {
return vm._setSubject().then(function () {
return vm._setIssuer().then(function () {
@ -91,11 +103,9 @@ app.controller('SignInController', [
subject: subject
, scope: [ 'domains@oauth3.org', 'domains', 'dns@oauth3.org', 'dns', 'www@daplie.com' ]
}).then(function (session) {
console.log('session', session);
vm.hasSession = session;
session.subject = subject;
session.issuer = issuer;
vm.oauth3.sessions.push(session);
Auth.add(session);
}, function (err) {
console.log('auth error');
console.log(err);
@ -106,13 +116,7 @@ app.controller('SignInController', [
vm.setIssuer(vm.defaultIssuer);
vm.signIn = function () {
vm.auth().then(function () {
var userInfo = {
email: vm.currentSubject,
name: 'Johnny Cash'
};
Auth.setUser(userInfo);
});
vm.auth();
};
}]);

View File

@ -1,4 +1,4 @@
app.controller('WebsiteController', [ 'azp@oauth3.org', function ($scope, Oauth3) {
app.controller('WebsiteController', [ '$scope', 'azp@oauth3.org', function ($scope, Oauth3) {
var vm = this;
vm.oauth3 = Oauth3.oauth3;

View File

@ -1,26 +1,66 @@
app.factory('Auth', [
'$rootScope', 'localStorageService', '$location', 'azp@oauth3.org'
, function($rootScope, localStorageService, $location, Oauth3) {
var user;
return {
var dapSession = 'dap-session';
var dapSessions = 'dap-sessions';
var Auth = {
setUser: function (currentUser) {
user = currentUser;
if (redirectedURL === '/splash-page') {
$location.path('/home');
} else {
$location.path('/' + redirectedURL);
}
user = localStorageService.set('userAuth', JSON.stringify(currentUser));
localStorageService.set('userAuth', JSON.stringify(currentUser));
},
isLoggedIn: function () {
// TODO check sessions instead
user = JSON.parse(localStorageService.get('userAuth'));
return (user) ? user : false;
Auth.restore();
return Auth.session || false;
},
getProfile: function (profile) {
profile = user;
return 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
};
return Auth;
}]);

View File

@ -19,13 +19,28 @@
</form>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">{{ vm.userName() }} <i class="fa fa-user" aria-hidden="true"></i> <span class="caret"></span></a>
<a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><span ng-bind="vm.session.subject">me@example.com</span>
<i class="fa fa-user" aria-hidden="true"></i> <span class="caret"></span>
<br><small ng-bind="vm.session.issuer">example.com</small></a>
<ul class="dropdown-menu">
<li><a href="feed.html" target="_blank">Preview My Public Site</a></li>
<li><a>Account Settings</a></li>
<li role="separator" class="divider"></li>
<li><a>Switch User</a></li>
<li ng-click="vm.signOut()"><a>Logout</a></li>
<li ng-if="vm.sessions.length">Switch User</li>
<li ng-repeat="session in vm.sessions"><a ng-click="vm.selectSession(session)"><span ng-bind="session.subject">me@example.com</span> <i class="fa fa-user" aria-hidden="true"></i>
<br><small ng-bind="session.issuer">example.com</small></a></li>
<li ng-if="vm.sessions.length" role="separator" class="divider"></li>
<li>Add User <i class="fa fa-cog" aria-hidden="true" ng-click="vm.toggleAdvanced()"></i>
<br><input class="input" type="text" ng-model="vm.newSubject" ng-change="vm.setSubject()" />
<br><input class="input" type="text" ng-if="vm.showAdvanced" ng-model="vm.newIssuer" ng-change="vm.setIssuer()" />
<br><button class="btn btn-default" type="button" ng-click="vm.auth()" >Add</button>
</li>
<li ng-click="vm.signOut()"><a>Logout <i class="fa fa-sign-out" aria-hidden="true"></i></a></li>
</ul>
</li>
</ul>