Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Feature/sup 2592 #584

Merged
merged 6 commits into from
Nov 30, 2015
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 59 additions & 42 deletions app/account/login/login.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,96 +3,112 @@

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 = {};

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 [email protected].');
}
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 [email protected].');
}
}

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have constant for the string literal? May be at the top of the controller if used only in this controller otherwise in constants object.
Same for other login error strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll add an enum for these.

} 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);
Expand All @@ -104,6 +120,7 @@
case "USER_NOT_REGISTERED":
default:
vm.socialLoginError = 401;
vm.loginErrors['social-login-error'] = true;
break;
}
});
Expand Down
8 changes: 4 additions & 4 deletions app/account/login/login.jade
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
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}")
input(ng-model="vm.username", name="username", placeholder="Username or Email", type="text", required)
Expand Down
4 changes: 2 additions & 2 deletions app/services/helpers.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
getCountyObjFromIP: getCountyObjFromIP,
redirectPostLogin: redirectPostLogin,
getSocialUserData: getSocialUserData,
setupLoginEventMetrices: setupLoginEventMetrices,
setupLoginEventMetrics: setupLoginEventMetrics,
npad: npad

};
Expand Down Expand Up @@ -297,7 +297,7 @@
}
}

function setupLoginEventMetrices (usernameOrEmail) {
function setupLoginEventMetrics (usernameOrEmail) {
if (_kmq) {
_kmq.push(['identify', usernameOrEmail ]);
}
Expand Down