diff --git a/src/js/setupaccount.js b/src/js/setupaccount.js index 23a7f3bb9..b3d7954e5 100644 --- a/src/js/setupaccount.js +++ b/src/js/setupaccount.js @@ -12,16 +12,18 @@ app.controller('SetupAccountController', ['$scope', '$http', function ($scope, $ var API_ORIGIN = '<%= oauth.apiOrigin %>' || window.location.origin; $scope.busy = false; - $scope.error = false; + $scope.error = null; + $scope.view = 'setup'; $scope.existingUsername = !!search.username; $scope.username = search.username || ''; - $scope.displayName = ''; + $scope.displayName = search.displayName || ''; $scope.password = ''; $scope.passwordRepeat = ''; $scope.onSubmit = function () { $scope.busy = true; + $scope.error = null; var data = { resetToken: search.resetToken, @@ -31,24 +33,40 @@ app.controller('SetupAccountController', ['$scope', '$http', function ($scope, $ password: $scope.password }; - function error(status) { - console.log('error', status) + function error(data, status) { $scope.busy = false; - if (status === 401) $scope.error = 'Invalid reset token'; - else if (status === 409) $scope.error = 'Ask your admin for an invite link first'; - else $scope.error = 'Unknown error'; + if (status === 401) { + $scope.view = 'invalidToken'; + } else if (status === 409) { + $scope.error = { + username: true, + message: 'Username already taken' + }; + $scope.setupAccountForm.username.$setPristine(); + setTimeout(function () { $('#inputUsername').focus(); }, 200); + } else if (status === 400) { + $scope.error = { + message: data.message + }; + if (data.message.indexOf('Username') === 0) { + $scope.setupAccountForm.username.$setPristine(); + $scope.error.username = true; + } + } else { + $scope.error = { message: 'Unknown error. Please try again later.' }; + console.error(status, data); + } } - $http.post(API_ORIGIN + '/api/v1/cloudron/password_reset', data).success(function (data, status) { - if (status !== 202) return error(status); + $http.post(API_ORIGIN + '/api/v1/cloudron/setup_account', data).success(function (data, status) { + if (status !== 201) return error(data, status); // set token to autologin localStorage.token = data.accessToken; - $scope.mode = 'newPasswordDone'; - }).error(function (data, status) { - error(status); - }); + $scope.view = 'done'; + }).error(error); }; + }]); diff --git a/src/setupaccount.html b/src/setupaccount.html index c0615ddba..d224b0cef 100644 --- a/src/setupaccount.html +++ b/src/setupaccount.html @@ -40,69 +40,98 @@
Contact your server admin to get a new invite link.
+