|
3 | 3 |
|
4 | 4 | angular.module('tc.account').controller('LoginController', LoginController);
|
5 | 5 |
|
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']; |
7 | 7 |
|
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) { |
9 | 9 | var vm = this;
|
10 | 10 | $log = $log.getInstance("LoginController");
|
11 | 11 | vm.$stateParams = $stateParams;
|
12 | 12 | vm.passwordReset = false;
|
13 |
| - vm.usernameExists = true; |
14 | 13 | vm.currentPasswordDefaultPlaceholder = "Password";
|
| 14 | + vm.loginErrors = {}; |
| 15 | + |
15 | 16 | vm.login = login;
|
16 | 17 | vm.socialLogin = socialLogin;
|
17 |
| - vm.loginErrors = {}; |
| 18 | + |
18 | 19 | // reference for main vm
|
19 | 20 | var mainVm = $scope.$parent.main;
|
20 | 21 |
|
|
26 | 27 | }
|
27 | 28 | }
|
28 | 29 |
|
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 |
| - |
53 | 30 | function login() {
|
54 | 31 | vm.loginErrors['username-nonexistant'] = false;
|
55 | 32 | vm.loginErrors['wrong-password'] = false;
|
| 33 | + |
56 | 34 | // TODO ideally it should be done by dedicated directive to handle all outside clicks
|
57 | 35 | mainVm.menuVisible = false;
|
58 | 36 |
|
59 | 37 | if (Helpers.isEmail(vm.username)) {
|
| 38 | + // the user is loggin in using email |
60 | 39 | vm.emailOrUsername = 'email';
|
| 40 | + |
61 | 41 | // ensure email exists
|
| 42 | + // uses same validity check as registration |
| 43 | + // valid => email isn't already used by someone |
62 | 44 | UserService.validateUserEmail(vm.username).then(function(data) {
|
63 | 45 | if (data.valid) {
|
64 | 46 | // email doesn't exist
|
65 | 47 | vm.loginErrors['username-nonexistant'] = true;
|
66 | 48 | } else {
|
67 |
| - vm.loginErrors['username-nonexistant'] = false; |
68 | 49 | _doLogin(vm.username, vm.currentPassword);
|
69 | 50 | }
|
70 | 51 | }).catch(function(resp) {
|
|
74 | 55 | _doLogin(vm.username, vm.currentPassword);
|
75 | 56 | });
|
76 | 57 | } else {
|
| 58 | + // the user is logging in using a username |
77 | 59 | vm.emailOrUsername = 'username';
|
| 60 | + |
78 | 61 | // username - make sure it exists
|
79 | 62 | UserService.validateUserHandle(vm.username).then(function(data) {
|
80 | 63 | if (data.valid) {
|
81 | 64 | // username doesn't exist
|
82 | 65 | vm.loginErrors['username-nonexistant'] = true;
|
83 | 66 | } else {
|
84 |
| - vm.loginErrors['username-nonexistant'] = false; |
85 | 67 | _doLogin(vm.username, vm.currentPassword);
|
86 | 68 | }
|
87 | 69 | }).catch(function(resp) {
|
88 | 70 | // TODO handle error
|
89 | 71 | // assume email exists, login would in any case if it didn't
|
90 |
| - vm.loginErrors['username-nonexistant'] = false; |
91 | 72 | _doLogin(vm.username, vm.currentPassword);
|
92 | 73 | });
|
93 | 74 | }
|
94 | 75 | };
|
95 | 76 |
|
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 = {}; |
98 | 104 | if ($stateParams.next) {
|
99 | 105 | params = {next: $stateParams.next};
|
100 | 106 | }
|
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) |
103 | 112 | .then(function() {
|
104 | 113 | $log.debug('logged in');
|
105 | 114 | return Helpers.redirectPostLogin($stateParams.next);
|
|
0 commit comments