Files
cloudron-box/src/js/setupaccount.js
T

73 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-02-05 15:05:34 +01:00
'use strict';
/* global angular, $ */
// create main application module
var app = angular.module('Application', []);
app.controller('SetupAccountController', ['$scope', '$http', function ($scope, $http) {
// Stupid angular location provider either wants html5 location mode or not, do the query parsing on my own
var search = decodeURIComponent(window.location.search).slice(1).split('&').map(function (item) { return item.indexOf('=') === -1 ? [item, true] : [item.slice(0, item.indexOf('=')), item.slice(item.indexOf('=')+1)]; }).reduce(function (o, k) { o[k[0]] = k[1]; return o; }, {});
var API_ORIGIN = '<%= oauth.apiOrigin %>' || window.location.origin;
$scope.busy = false;
2020-02-05 16:12:52 +01:00
$scope.error = null;
$scope.view = 'setup';
2020-02-05 15:05:34 +01:00
$scope.existingUsername = !!search.username;
$scope.username = search.username || '';
2020-02-05 16:12:52 +01:00
$scope.displayName = search.displayName || '';
2020-02-05 15:05:34 +01:00
$scope.password = '';
$scope.passwordRepeat = '';
$scope.onSubmit = function () {
$scope.busy = true;
2020-02-05 16:12:52 +01:00
$scope.error = null;
2020-02-05 15:05:34 +01:00
var data = {
resetToken: search.resetToken,
email: search.email,
username: $scope.username,
displayName: $scope.displayName,
password: $scope.password
};
2020-02-05 16:12:52 +01:00
function error(data, status) {
2020-02-05 15:05:34 +01:00
$scope.busy = false;
2020-02-05 16:12:52 +01:00
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);
}
2020-02-05 15:05:34 +01:00
}
2020-02-05 16:12:52 +01:00
$http.post(API_ORIGIN + '/api/v1/cloudron/setup_account', data).success(function (data, status) {
if (status !== 201) return error(data, status);
2020-02-05 15:05:34 +01:00
// set token to autologin
localStorage.token = data.accessToken;
2020-02-05 16:12:52 +01:00
$scope.view = 'done';
}).error(error);
2020-02-05 15:05:34 +01:00
};
2020-02-05 16:12:52 +01:00
2020-02-05 15:05:34 +01:00
}]);