diff --git a/app/account/login/login.controller.js b/app/account/login/login.controller.js index 73cca451f..ae56fed41 100644 --- a/app/account/login/login.controller.js +++ b/app/account/login/login.controller.js @@ -3,96 +3,116 @@ angular.module('tc.account').controller('LoginController', LoginController); - LoginController.$inject = ['$log', '$state', '$stateParams', '$location', '$scope', 'TcAuthService', 'AuthTokenService', 'UserService', 'NotificationService', 'Helpers', 'CONSTANTS']; + LoginController.$inject = ['$log', '$state', '$stateParams', '$location', '$scope', 'TcAuthService', 'UserService', 'NotificationService', 'Helpers', 'CONSTANTS']; - function LoginController($log, $state, $stateParams, $location, $scope, TcAuthService, AuthTokenService, UserService, NotificationService, Helpers, CONSTANTS) { - $log = $log.getInstance("LoginController"); + function LoginController($log, $state, $stateParams, $location, $scope, TcAuthService, UserService, NotificationService, Helpers, CONSTANTS) { var vm = this; + $log = $log.getInstance("LoginController"); vm.$stateParams = $stateParams; vm.passwordReset = false; - vm.usernameExists = true; vm.currentPasswordDefaultPlaceholder = "Password"; + vm.loginErrors = { + USERNAME_NONEXISTANT: false, + WRONG_PASSWORD: false, + SOCIAL_LOGIN_ERROR: false + }; + + vm.login = login; + vm.socialLogin = socialLogin; + // reference for main vm var mainVm = $scope.$parent.main; - if ($stateParams.notifyReset) { - NotificationService.inform('Your new password has been set. Please log in. If you have any trouble, please contact support@topcoder.com.'); - } + activate(); - function _doLogin(usernameOrEmail, password) { - return TcAuthService.login(usernameOrEmail, password).then( - function(data) { - // success - $log.debug('logged in'); - // setup login event for analytics tracking - Helpers.setupLoginEventMetrices(usernameOrEmail); - return Helpers.redirectPostLogin($stateParams.next); - }) - .catch(function(resp) { - $log.warn(resp); - switch (resp.status) { - case "ACCOUNT_INACTIVE": - $state.go('registeredSuccessfully'); - // user should already be redirected - break; - case "UNKNOWN_ERROR": - default: - vm.wrongPassword = true; - vm.password = ''; - } - }); + function activate() { + if ($stateParams.notifyReset) { + NotificationService.inform('Your new password has been set. Please log in. If you have any trouble, please contact support@topcoder.com.'); + } } - vm.login = function() { - vm.usernameExists = true; - vm.wrongPassword = false; + function login() { + vm.loginErrors.USERNAME_NONEXISTANT = false; + vm.loginErrors.WRONG_PASSWORD = false; + // TODO ideally it should be done by dedicated directive to handle all outside clicks mainVm.menuVisible = false; if (Helpers.isEmail(vm.username)) { + // the user is loggin in using email vm.emailOrUsername = 'email'; + // ensure email exists + // uses same validity check as registration + // valid => email isn't already used by someone UserService.validateUserEmail(vm.username).then(function(data) { if (data.valid) { // email doesn't exist - vm.usernameExists = false; + vm.loginErrors.USERNAME_NONEXISTANT = true; } else { - vm.usernameExists = true; _doLogin(vm.username, vm.currentPassword); } }).catch(function(resp) { // TODO handle error // assume email exists, login would in any case if it didn't - vm.usernameExists = true; + vm.loginErrors.USERNAME_NONEXISTANT = false; _doLogin(vm.username, vm.currentPassword); }); } else { + // the user is logging in using a username vm.emailOrUsername = 'username'; + // username - make sure it exists UserService.validateUserHandle(vm.username).then(function(data) { if (data.valid) { // username doesn't exist - vm.usernameExists = false; + vm.loginErrors.USERNAME_NONEXISTANT = true; } else { - vm.usernameExists = true; _doLogin(vm.username, vm.currentPassword); } }).catch(function(resp) { // TODO handle error // assume email exists, login would in any case if it didn't - vm.usernameExists = true; _doLogin(vm.username, vm.currentPassword); }); } }; - vm.socialLogin = function(backend) { - var params = {}, callbackUrl; + function _doLogin(usernameOrEmail, password) { + return TcAuthService.login(usernameOrEmail, password).then(function(data) { + // success + $log.debug('logged in'); + + // setup login event for analytics tracking + Helpers.setupLoginEventMetrics(usernameOrEmail); + return Helpers.redirectPostLogin($stateParams.next); + + }).catch(function(resp) { + $log.warn(resp); + switch (resp.status) { + case "ACCOUNT_INACTIVE": + $state.go('registeredSuccessfully'); + // user should already be redirected + break; + case "UNKNOWN_ERROR": + default: + vm.loginErrors.WRONG_PASSWORD = true; + vm.password = ''; + } + }); + } + + function socialLogin(platform) { + // we need to pass on the 'next' param if we have one + var params = {}; if ($stateParams.next) { params = {next: $stateParams.next}; } - callbackUrl = $state.href('login', params, {absolute: true}); - TcAuthService.socialLogin(backend, callbackUrl) + + // redirect back to login + var callbackUrl = $state.href('login', params, {absolute: true}); + + TcAuthService.socialLogin(platform, callbackUrl) .then(function() { $log.debug('logged in'); return Helpers.redirectPostLogin($stateParams.next); @@ -104,6 +124,7 @@ case "USER_NOT_REGISTERED": default: vm.socialLoginError = 401; + vm.loginErrors.SOCIAL_LOGIN_ERROR = true; break; } }); diff --git a/app/account/login/login.jade b/app/account/login/login.jade index 3ac5deb41..ce7f103ce 100644 --- a/app/account/login/login.jade +++ b/app/account/login/login.jade @@ -6,14 +6,14 @@ h1 LOG IN TO TOPCODER form(name="vm.loginForm", role="form", ng-submit="vm.loginForm.$valid && vm.login()", novalidate) - .form-errors - p.form-error(ng-hide="vm.usernameExists") We couldn't find a member with that {{vm.emailOrUsername || "username"}}. Please check that you entered it correctly. + .form-errors(ng-messages="vm.loginErrors") + p.form-error(ng-message="USERNAME_NONEXISTANT") We couldn't find a member with that {{vm.emailOrUsername || "username"}}. Please check that you entered it correctly. - p.form-error(ng-show="vm.wrongPassword") That password is incorrect. Please check that you entered the right one. + p.form-error(ng-message="WRONG_PASSWORD") That password is incorrect. Please check that you entered the right one. - p.form-error(ng-show="vm.socialLoginError === 401") User with that profile is not registered. + p.form-error(ng-message="SOCIAL_LOGIN_ERROR") User with that profile is not registered. - div.validation-bar(ng-class="{'error-bar': !vm.usernameExists}") + div.validation-bar(ng-class="{'error-bar': vm.loginErrors.USERNAME_NONEXISTANT}") input(ng-model="vm.username", name="username", placeholder="Username or Email", type="text", required) toggle-password diff --git a/app/services/helpers.service.js b/app/services/helpers.service.js index 11a98703d..d04ace38f 100644 --- a/app/services/helpers.service.js +++ b/app/services/helpers.service.js @@ -20,7 +20,7 @@ getCountyObjFromIP: getCountyObjFromIP, redirectPostLogin: redirectPostLogin, getSocialUserData: getSocialUserData, - setupLoginEventMetrices: setupLoginEventMetrices, + setupLoginEventMetrics: setupLoginEventMetrics, npad: npad }; @@ -297,7 +297,7 @@ } } - function setupLoginEventMetrices (usernameOrEmail) { + function setupLoginEventMetrics (usernameOrEmail) { if (_kmq) { _kmq.push(['identify', usernameOrEmail ]); }