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

Commit 4c510f9

Browse files
committed
Rest of cleanup for login
1 parent 79a6cee commit 4c510f9

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

app/account/login/login.controller.js

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
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) {
8+
function LoginController($log, $state, $stateParams, $location, $scope, TcAuthService, UserService, NotificationService, Helpers, CONSTANTS) {
99
var vm = this;
1010
$log = $log.getInstance("LoginController");
1111
vm.$stateParams = $stateParams;
1212
vm.passwordReset = false;
13-
vm.usernameExists = true;
1413
vm.currentPasswordDefaultPlaceholder = "Password";
14+
vm.loginErrors = {};
15+
1516
vm.login = login;
1617
vm.socialLogin = socialLogin;
17-
vm.loginErrors = {};
18+
1819
// reference for main vm
1920
var mainVm = $scope.$parent.main;
2021

@@ -26,45 +27,25 @@
2627
}
2728
}
2829

29-
function _doLogin(usernameOrEmail, password) {
30-
return TcAuthService.login(usernameOrEmail, password).then(function(data) {
31-
// success
32-
$log.debug('logged in');
33-
34-
// setup login event for analytics tracking
35-
Helpers.setupLoginEventMetrices(usernameOrEmail);
36-
return Helpers.redirectPostLogin($stateParams.next);
37-
38-
}).catch(function(resp) {
39-
$log.warn(resp);
40-
switch (resp.status) {
41-
case "ACCOUNT_INACTIVE":
42-
$state.go('registeredSuccessfully');
43-
// user should already be redirected
44-
break;
45-
case "UNKNOWN_ERROR":
46-
default:
47-
vm.loginErrors['wrong-password'] = true;
48-
vm.password = '';
49-
}
50-
});
51-
}
52-
5330
function login() {
5431
vm.loginErrors['username-nonexistant'] = false;
5532
vm.loginErrors['wrong-password'] = false;
33+
5634
// TODO ideally it should be done by dedicated directive to handle all outside clicks
5735
mainVm.menuVisible = false;
5836

5937
if (Helpers.isEmail(vm.username)) {
38+
// the user is loggin in using email
6039
vm.emailOrUsername = 'email';
40+
6141
// ensure email exists
42+
// uses same validity check as registration
43+
// valid => email isn't already used by someone
6244
UserService.validateUserEmail(vm.username).then(function(data) {
6345
if (data.valid) {
6446
// email doesn't exist
6547
vm.loginErrors['username-nonexistant'] = true;
6648
} else {
67-
vm.loginErrors['username-nonexistant'] = false;
6849
_doLogin(vm.username, vm.currentPassword);
6950
}
7051
}).catch(function(resp) {
@@ -74,32 +55,60 @@
7455
_doLogin(vm.username, vm.currentPassword);
7556
});
7657
} else {
58+
// the user is logging in using a username
7759
vm.emailOrUsername = 'username';
60+
7861
// username - make sure it exists
7962
UserService.validateUserHandle(vm.username).then(function(data) {
8063
if (data.valid) {
8164
// username doesn't exist
8265
vm.loginErrors['username-nonexistant'] = true;
8366
} else {
84-
vm.loginErrors['username-nonexistant'] = false;
8567
_doLogin(vm.username, vm.currentPassword);
8668
}
8769
}).catch(function(resp) {
8870
// TODO handle error
8971
// assume email exists, login would in any case if it didn't
90-
vm.loginErrors['username-nonexistant'] = false;
9172
_doLogin(vm.username, vm.currentPassword);
9273
});
9374
}
9475
};
9576

96-
function socialLogin(backend) {
97-
var params = {}, callbackUrl;
77+
function _doLogin(usernameOrEmail, password) {
78+
return TcAuthService.login(usernameOrEmail, password).then(function(data) {
79+
// success
80+
$log.debug('logged in');
81+
82+
// setup login event for analytics tracking
83+
Helpers.setupLoginEventMetrics(usernameOrEmail);
84+
return Helpers.redirectPostLogin($stateParams.next);
85+
86+
}).catch(function(resp) {
87+
$log.warn(resp);
88+
switch (resp.status) {
89+
case "ACCOUNT_INACTIVE":
90+
$state.go('registeredSuccessfully');
91+
// user should already be redirected
92+
break;
93+
case "UNKNOWN_ERROR":
94+
default:
95+
vm.loginErrors['wrong-password'] = true;
96+
vm.password = '';
97+
}
98+
});
99+
}
100+
101+
function socialLogin(platform) {
102+
// we need to pass on the 'next' param if we have one
103+
var params = {};
98104
if ($stateParams.next) {
99105
params = {next: $stateParams.next};
100106
}
101-
callbackUrl = $state.href('login', params, {absolute: true});
102-
TcAuthService.socialLogin(backend, callbackUrl)
107+
108+
// redirect back to login
109+
var callbackUrl = $state.href('login', params, {absolute: true});
110+
111+
TcAuthService.socialLogin(platform, callbackUrl)
103112
.then(function() {
104113
$log.debug('logged in');
105114
return Helpers.redirectPostLogin($stateParams.next);

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)