This repository was archived by the owner on Mar 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Feature/sup 2592 #584
Merged
Merged
Feature/sup 2592 #584
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
333fb05
initial restructuring
tladendo 0169719
placeholder 2
tladendo 74017c4
placeholder 3
tladendo 79a6cee
add ng-messages
tladendo 4c510f9
Rest of cleanup for login
tladendo 6753076
Made properties more enum-like
tladendo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
} 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 +120,7 @@ | |
case "USER_NOT_REGISTERED": | ||
default: | ||
vm.socialLoginError = 401; | ||
vm.loginErrors['social-login-error'] = true; | ||
break; | ||
} | ||
}); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.