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

106 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-02-05 15:05:34 +01:00
'use strict';
/* global angular, $, showdown */
2020-02-05 15:05:34 +01:00
// create main application module
var app = angular.module('Application', []);
app.filter('markdown2html', function () {
var converter = new showdown.Converter({
extensions: [],
simplifiedAutoLink: true,
strikethrough: true,
tables: true
});
return function (text) {
return converter.makeHtml(text);
};
});
// disable sce for footer https://code.angularjs.org/1.5.8/docs/api/ng/service/$sce
app.config(function ($sceProvider) {
$sceProvider.enabled(false);
});
2020-02-05 15:05:34 +01:00
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 = '<%= apiOrigin %>' || window.location.origin;
2020-02-05 15:05:34 +01:00
$scope.initialized = false;
2020-02-05 15:05:34 +01:00
$scope.busy = false;
2020-02-05 16:12:52 +01:00
$scope.error = null;
$scope.view = 'setup';
$scope.cloudronName = 'Cloudron';
$scope.footer = '';
$scope.version = '';
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
$http.get(API_ORIGIN + '/api/v1/cloudron/status').success(function (data, status) {
$scope.initialized = true;
if (status !== 200) return;
$scope.cloudronName = data.cloudronName;
$scope.footer = data.footer;
$scope.version = data.version;
}).error(function () {
$scope.initialized = false;
});
2020-02-05 15:05:34 +01:00
}]);