Implement account setup view logic

This commit is contained in:
Johannes Zellner
2020-02-05 16:12:52 +01:00
parent 9dbdad324a
commit 9283537efc
2 changed files with 98 additions and 51 deletions

View File

@@ -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);
};
}]);