Skip to content

Commit

Permalink
Verify user session before login
Browse files Browse the repository at this point in the history
  • Loading branch information
phuonghuynh committed Jan 9, 2015
1 parent c56423d commit 1ce018f
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 43 deletions.
7 changes: 7 additions & 0 deletions src/main/webapp/assets/modules/common/cleanup.fac.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ angular.module("Common").factory("cleanupFactory", function (jsonValue, utils, l
cleanSession: function () {
$rootScope.userInfo = undefined;
localStorageService.cookie.remove(jsonValue.storage.key);
},

cleanBack2Me: function() {
if (utils.getView() !== jsonValue.views.signIn) {
localStorageService.remove(jsonValue.storage.back2Me);
}
}
}

Expand All @@ -23,5 +29,6 @@ angular.module("Common").factory("cleanupFactory", function (jsonValue, utils, l
utils.registerNotification(jsonValue.notifications.cleanSession, $$.cleanSession);
utils.registerNotification(jsonValue.notifications.loginFailed, $$.cleanSession);
utils.registerNotification(jsonValue.notifications.logoutSuccess, $$.cleanSession);
utils.registerNotification(jsonValue.notifications.changeUrl, $$.cleanBack2Me);
return instance;
});
2 changes: 0 additions & 2 deletions src/main/webapp/assets/modules/common/connection.fac.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ angular.module("Common").factory("connectionFactory",
localStorageService.set(jsonValue.storage.back2Me, "true");
return utils.sendNotification(jsonValue.notifications.loginFailed);
}
return utils.sendNotification(jsonValue.notifications.http404);
}
}

Expand Down Expand Up @@ -101,7 +100,6 @@ angular.module("Common").factory("connectionFactory",
utils.sendNotification(jsonValue.notifications.userInfo, userInfo);
})
.catch(function() {
//localStorageService.set(jsonValue.storage.back2Me, "true");
utils.sendNotification(jsonValue.notifications.notUserInfo);
});

Expand Down
10 changes: 5 additions & 5 deletions src/main/webapp/assets/modules/common/history.fac.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
angular.module("Common").factory("historyFactory", function (jsonValue, $location, $rootScope, utils) {
var historyStack = [];
//var exceptViews = [jsonValue.views.analyticsSkill, jsonValue.views.signIn];
var exceptViews = [jsonValue.views.analyticsSkill, jsonValue.views.signIn];

$rootScope.$on('$routeChangeSuccess', function (event, next, current) {
switch (utils.getView()) {
case jsonValue.views.bubbleChart:
case jsonValue.views.pieChart:
// TODO: #1 - change the body background to black
$("body").css("background-color", "#201d1e");
instance.trackHistory();
}
console.log(historyStack);
instance.trackHistory();

utils.sendNotification(jsonValue.notifications.changeUrl/*, current.$$route.originalPath, next.$$route.originalPath*/);
});
Expand All @@ -25,8 +24,9 @@ angular.module("Common").factory("historyFactory", function (jsonValue, $locatio
},

popHistory: function () {
//var url; // remove current item
//do {url = historyStack.pop()} while(exceptViews.indexOf(url) >= 0);
var url; // remove current item
do {url = historyStack.pop()} while(exceptViews.indexOf(url) >= 0);
console.log(historyStack);
//if (historyStack.length === 0) return "/";
//return historyStack.pop();
switch (utils.getView()) {
Expand Down
10 changes: 1 addition & 9 deletions src/main/webapp/assets/modules/common/router.ser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ angular.module("Common").factory("routerService", function (jsonValue, utils, $l

loginSuccess: function () {
if (localStorageService.get(jsonValue.storage.back2Me) === "true") {
localStorageService.remove(jsonValue.storage.back2Me);
var path = historyFactory.popHistory();
$location.path(path);
}
Expand All @@ -17,25 +16,18 @@ angular.module("Common").factory("routerService", function (jsonValue, utils, $l
},

serverError: function () {
//$location.path("/");
console.log("Unable to connect to server");
},

logoutSuccess: function() {
$location.path("/");
},

changeUrl: function() {
if (utils.getView() !== jsonValue.views.signIn) {
localStorageService.remove(jsonValue.storage.back2Me);
}
}
}

utils.registerNotification(jsonValue.notifications.loginFailed, $$.loginFailed);
utils.registerNotification(jsonValue.notifications.logoutSuccess, $$.logoutSuccess);
utils.registerNotification(jsonValue.notifications.loginSuccess, $$.loginSuccess);
utils.registerNotification(jsonValue.notifications.serverError, $$.serverError);
utils.registerNotification(jsonValue.notifications.changeUrl, $$.changeUrl);

var instance = {
initialize: function () {}
Expand Down
44 changes: 34 additions & 10 deletions src/main/webapp/assets/modules/common/user.ser.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,61 @@
angular.module("Common").factory("userService", function (jsonValue, utils, connectionFactory, $rootScope, $q) {
angular.module("Common").factory("userService", function (jsonValue, utils, connectionFactory, $rootScope, $q, localStorageService, $cookies) {

var defers = {
getUserInfo: []
}

var $$ = {
notUserInfo: function() {
$$.reject("getUserInfo");
},

reject: function (key, reply) {
$.each(defers[key], function (i, d) {d.reject(reply);});
defers[key].length = 0;
},

resolve: function (key, reply) {
$.each(defers[key], function (i, d) {d.resolve(reply);});
defers[key].length = 0;
}
}

var instance = {
initialize: function () {},

getUserInfo: function () {
var deferred = $q.defer();
defers.getUserInfo.push(deferred);

if (instance.notLoggedIn()) {
if (!instance.verifyUserSession()) {
$$.notUserInfo();
}
else if (instance.notLoggedIn()) {
connectionFactory.findUserInfoByKey();
}
else {
instance.resolve("getUserInfo", $rootScope.userInfo);
$$.resolve("getUserInfo", $rootScope.userInfo);
}
return deferred.promise;
},

resolve: function (key, reply) {
$.each(defers[key], function (i, d) {d.resolve(reply);});
defers[key].length = 0;
},

//TODO: check actual user logged-in
notLoggedIn: function () {
return ($rootScope.userInfo === undefined);
},

verifyUserSession: function() {
return localStorageService.cookie.get(jsonValue.storage.key) !== null;
}
}

instance.getUserInfo(); //handle refresh button: read user-info
if (instance.verifyUserSession()) {
instance.getUserInfo(); //handle refresh button: read user-info
}
else {
utils.sendNotification(jsonValue.notifications.cleanSession);
}

utils.registerNotification(jsonValue.notifications.notUserInfo, $$.notUserInfo);
utils.registerNotification(jsonValue.notifications.userInfo, instance.getUserInfo);
return instance;
});
12 changes: 9 additions & 3 deletions src/main/webapp/assets/modules/register/register.con.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ angular.module('Register').controller('registerController',
$scope.openOathDialog = registerService.openOathDialog;
$scope.salaryOptions = registerService.getSalaryOptions();

userService.getUserInfo().then(function () {
utils.sendNotification(jsonValue.notifications.gotData);
});
userService.getUserInfo()
.then(function () {
localStorageService.remove(jsonValue.storage.back2Me, "true");
utils.sendNotification(jsonValue.notifications.gotData);
})
.catch(function () {
localStorageService.set(jsonValue.storage.back2Me, "true");
utils.sendNotification(jsonValue.notifications.loginFailed);
});
navigationService.addSpaceforNavi();
navigationService.reSetingPositionLangIcon();
navigationService.keepNaviBar();
Expand Down
7 changes: 2 additions & 5 deletions src/main/webapp/assets/modules/register/register.ser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ angular.module('Register').factory('registerService',
var $$ = {
initialize: function ($scope) {
scope = $scope;

$('input[type="checkbox"]').checkbox();
$("#salary").slider({});
$('.btn-close').click(function () {
Expand Down Expand Up @@ -46,10 +47,6 @@ angular.module('Register').factory('registerService',

enableNotifications: function () {
return utils.getView() === jsonValue.views.register;
},

notUserInfo: function() {
localStorageService.set(jsonValue.storage.back2Me, "true");
}
}

Expand Down Expand Up @@ -81,7 +78,7 @@ angular.module('Register').factory('registerService',
}
};

utils.registerNotification(jsonValue.notifications.notUserInfo, $$.notUserInfo, $$.enableNotifications);
//utils.registerNotification(jsonValue.notifications.notUserInfo, $$.notUserInfo, $$.enableNotifications);
utils.registerNotification(jsonValue.notifications.switchScope, $$.initialize, $$.enableNotifications);
return instance;
});
18 changes: 9 additions & 9 deletions src/main/webapp/assets/modules/signin/signin.ser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
angular.module('SignIn').factory('signInService',
function (jsonValue, utils, shortcutFactory, $location, tourService, $auth, localStorageService,
$window, $http, connectionFactory, $rootScope) {
$window, $http, connectionFactory, $rootScope, userService) {

var $$ = {
enableNotifications: function () {
Expand All @@ -20,14 +20,14 @@ angular.module('SignIn').factory('signInService',
utils.sendNotification(jsonValue.notifications.loading, $(window).height());

// check if user already login
connectionFactory.verifyUserLogin()
.then(function() {
utils.sendNotification(jsonValue.notifications.loginSuccess);
})
.catch(function() {
$rootScope.userInfo = undefined;
utils.sendNotification(jsonValue.notifications.loaded);
});
if (userService.verifyUserSession()) {
connectionFactory.verifyUserLogin()
.then(function () {utils.sendNotification(jsonValue.notifications.loginSuccess);})
.catch(function () {utils.sendNotification(jsonValue.notifications.loginFailed);});
}
else {
utils.sendNotification(jsonValue.notifications.hideLoadingBox);
}

$('.signin-accounts').parallax();

Expand Down

0 comments on commit 1ce018f

Please sign in to comment.