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

Commit 0d48071

Browse files
committed
Merge pull request #584 from appirio-tech/feature/SUP-2592
Feature/sup 2592
2 parents 03f2500 + 6753076 commit 0d48071

File tree

3 files changed

+70
-49
lines changed

3 files changed

+70
-49
lines changed

app/account/login/login.controller.js

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,96 +3,116 @@
33

44
angular.module('tc.account').controller('LoginController', LoginController);
55

6-
LoginController.$inject = ['$log', '$state', '$stateParams', '$location', '$scope', 'TcAuthService', 'AuthTokenService', 'UserService', 'NotificationService', 'Helpers', 'CONSTANTS'];
6+
LoginController.$inject = ['$log', '$state', '$stateParams', '$location', '$scope', 'TcAuthService', 'UserService', 'NotificationService', 'Helpers', 'CONSTANTS'];
77

8-
function LoginController($log, $state, $stateParams, $location, $scope, TcAuthService, AuthTokenService, UserService, NotificationService, Helpers, CONSTANTS) {
9-
$log = $log.getInstance("LoginController");
8+
function LoginController($log, $state, $stateParams, $location, $scope, TcAuthService, UserService, NotificationService, Helpers, CONSTANTS) {
109
var vm = this;
10+
$log = $log.getInstance("LoginController");
1111
vm.$stateParams = $stateParams;
1212
vm.passwordReset = false;
13-
vm.usernameExists = true;
1413
vm.currentPasswordDefaultPlaceholder = "Password";
14+
vm.loginErrors = {
15+
USERNAME_NONEXISTANT: false,
16+
WRONG_PASSWORD: false,
17+
SOCIAL_LOGIN_ERROR: false
18+
};
19+
20+
vm.login = login;
21+
vm.socialLogin = socialLogin;
22+
1523
// reference for main vm
1624
var mainVm = $scope.$parent.main;
1725

18-
if ($stateParams.notifyReset) {
19-
NotificationService.inform('Your new password has been set. Please log in. If you have any trouble, please contact [email protected].');
20-
}
26+
activate();
2127

22-
function _doLogin(usernameOrEmail, password) {
23-
return TcAuthService.login(usernameOrEmail, password).then(
24-
function(data) {
25-
// success
26-
$log.debug('logged in');
27-
// setup login event for analytics tracking
28-
Helpers.setupLoginEventMetrices(usernameOrEmail);
29-
return Helpers.redirectPostLogin($stateParams.next);
30-
})
31-
.catch(function(resp) {
32-
$log.warn(resp);
33-
switch (resp.status) {
34-
case "ACCOUNT_INACTIVE":
35-
$state.go('registeredSuccessfully');
36-
// user should already be redirected
37-
break;
38-
case "UNKNOWN_ERROR":
39-
default:
40-
vm.wrongPassword = true;
41-
vm.password = '';
42-
}
43-
});
28+
function activate() {
29+
if ($stateParams.notifyReset) {
30+
NotificationService.inform('Your new password has been set. Please log in. If you have any trouble, please contact [email protected].');
31+
}
4432
}
4533

46-
vm.login = function() {
47-
vm.usernameExists = true;
48-
vm.wrongPassword = false;
34+
function login() {
35+
vm.loginErrors.USERNAME_NONEXISTANT = false;
36+
vm.loginErrors.WRONG_PASSWORD = false;
37+
4938
// TODO ideally it should be done by dedicated directive to handle all outside clicks
5039
mainVm.menuVisible = false;
5140

5241
if (Helpers.isEmail(vm.username)) {
42+
// the user is loggin in using email
5343
vm.emailOrUsername = 'email';
44+
5445
// ensure email exists
46+
// uses same validity check as registration
47+
// valid => email isn't already used by someone
5548
UserService.validateUserEmail(vm.username).then(function(data) {
5649
if (data.valid) {
5750
// email doesn't exist
58-
vm.usernameExists = false;
51+
vm.loginErrors.USERNAME_NONEXISTANT = true;
5952
} else {
60-
vm.usernameExists = true;
6153
_doLogin(vm.username, vm.currentPassword);
6254
}
6355
}).catch(function(resp) {
6456
// TODO handle error
6557
// assume email exists, login would in any case if it didn't
66-
vm.usernameExists = true;
58+
vm.loginErrors.USERNAME_NONEXISTANT = false;
6759
_doLogin(vm.username, vm.currentPassword);
6860
});
6961
} else {
62+
// the user is logging in using a username
7063
vm.emailOrUsername = 'username';
64+
7165
// username - make sure it exists
7266
UserService.validateUserHandle(vm.username).then(function(data) {
7367
if (data.valid) {
7468
// username doesn't exist
75-
vm.usernameExists = false;
69+
vm.loginErrors.USERNAME_NONEXISTANT = true;
7670
} else {
77-
vm.usernameExists = true;
7871
_doLogin(vm.username, vm.currentPassword);
7972
}
8073
}).catch(function(resp) {
8174
// TODO handle error
8275
// assume email exists, login would in any case if it didn't
83-
vm.usernameExists = true;
8476
_doLogin(vm.username, vm.currentPassword);
8577
});
8678
}
8779
};
8880

89-
vm.socialLogin = function(backend) {
90-
var params = {}, callbackUrl;
81+
function _doLogin(usernameOrEmail, password) {
82+
return TcAuthService.login(usernameOrEmail, password).then(function(data) {
83+
// success
84+
$log.debug('logged in');
85+
86+
// setup login event for analytics tracking
87+
Helpers.setupLoginEventMetrics(usernameOrEmail);
88+
return Helpers.redirectPostLogin($stateParams.next);
89+
90+
}).catch(function(resp) {
91+
$log.warn(resp);
92+
switch (resp.status) {
93+
case "ACCOUNT_INACTIVE":
94+
$state.go('registeredSuccessfully');
95+
// user should already be redirected
96+
break;
97+
case "UNKNOWN_ERROR":
98+
default:
99+
vm.loginErrors.WRONG_PASSWORD = true;
100+
vm.password = '';
101+
}
102+
});
103+
}
104+
105+
function socialLogin(platform) {
106+
// we need to pass on the 'next' param if we have one
107+
var params = {};
91108
if ($stateParams.next) {
92109
params = {next: $stateParams.next};
93110
}
94-
callbackUrl = $state.href('login', params, {absolute: true});
95-
TcAuthService.socialLogin(backend, callbackUrl)
111+
112+
// redirect back to login
113+
var callbackUrl = $state.href('login', params, {absolute: true});
114+
115+
TcAuthService.socialLogin(platform, callbackUrl)
96116
.then(function() {
97117
$log.debug('logged in');
98118
return Helpers.redirectPostLogin($stateParams.next);
@@ -104,6 +124,7 @@
104124
case "USER_NOT_REGISTERED":
105125
default:
106126
vm.socialLoginError = 401;
127+
vm.loginErrors.SOCIAL_LOGIN_ERROR = true;
107128
break;
108129
}
109130
});

app/account/login/login.jade

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
h1 LOG IN TO TOPCODER
77

88
form(name="vm.loginForm", role="form", ng-submit="vm.loginForm.$valid && vm.login()", novalidate)
9-
.form-errors
10-
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.
9+
.form-errors(ng-messages="vm.loginErrors")
10+
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.
1111

12-
p.form-error(ng-show="vm.wrongPassword") That password is incorrect. Please check that you entered the right one.
12+
p.form-error(ng-message="WRONG_PASSWORD") That password is incorrect. Please check that you entered the right one.
1313

14-
p.form-error(ng-show="vm.socialLoginError === 401") User with that profile is not registered.
14+
p.form-error(ng-message="SOCIAL_LOGIN_ERROR") User with that profile is not registered.
1515

16-
div.validation-bar(ng-class="{'error-bar': !vm.usernameExists}")
16+
div.validation-bar(ng-class="{'error-bar': vm.loginErrors.USERNAME_NONEXISTANT}")
1717
input(ng-model="vm.username", name="username", placeholder="Username or Email", type="text", required)
1818

1919
toggle-password

app/services/helpers.service.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
getCountyObjFromIP: getCountyObjFromIP,
2121
redirectPostLogin: redirectPostLogin,
2222
getSocialUserData: getSocialUserData,
23-
setupLoginEventMetrices: setupLoginEventMetrices,
23+
setupLoginEventMetrics: setupLoginEventMetrics,
2424
npad: npad
2525

2626
};
@@ -297,7 +297,7 @@
297297
}
298298
}
299299

300-
function setupLoginEventMetrices (usernameOrEmail) {
300+
function setupLoginEventMetrics (usernameOrEmail) {
301301
if (_kmq) {
302302
_kmq.push(['identify', usernameOrEmail ]);
303303
}

0 commit comments

Comments
 (0)