Files
cloudron-box/dashboard/public/js/setupaccount.js

152 lines
5.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
2020-11-19 11:33:46 +01:00
var app = angular.module('Application', ['pascalprecht.translate', 'ngCookies']);
2020-02-05 15:05:34 +01:00
app.filter('markdown2html', function () {
var converter = new showdown.Converter({
simplifiedAutoLink: true,
strikethrough: true,
tables: true,
openLinksInNewWindow: 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-11-19 11:33:46 +01:00
app.config(['$translateProvider', function ($translateProvider) {
$translateProvider.useStaticFilesLoader({
prefix: 'translation/',
suffix: '.json'
});
$translateProvider.useLocalStorage();
$translateProvider.preferredLanguage('en');
$translateProvider.fallbackLanguage('en');
}]);
// Add shorthand "tr" filter to avoid having ot use "translate"
// This is a copy of the code at https://github.com/angular-translate/angular-translate/blob/master/src/filter/translate.js
// If we find out how to get that function handle somehow dynamically we can use that, otherwise the copy is required
function translateFilterFactory($parse, $translate) {
'use strict';
var translateFilter = function (translationId, interpolateParams, interpolation, forceLanguage) {
if (!angular.isObject(interpolateParams)) {
var ctx = this || {
'__SCOPE_IS_NOT_AVAILABLE': 'More info at https://github.com/angular/angular.js/commit/8863b9d04c722b278fa93c5d66ad1e578ad6eb1f'
};
interpolateParams = $parse(interpolateParams)(ctx);
}
2020-11-19 11:33:46 +01:00
return $translate.instant(translationId, interpolateParams, interpolation, forceLanguage);
};
2020-11-19 11:33:46 +01:00
if ($translate.statefulFilter()) {
translateFilter.$stateful = true;
}
2020-11-19 11:33:46 +01:00
return translateFilter;
2020-11-19 11:33:46 +01:00
}
translateFilterFactory.displayName = 'translateFilterFactory';
app.filter('tr', translateFilterFactory);
app.controller('SetupAccountController', ['$scope', '$translate', '$http', function ($scope, $translate, $http) {
2020-02-05 15:05:34 +01:00
// Stupid angular location provider either wants html5 location mode or not, do the query parsing on my own
2024-10-04 17:43:45 +02:00
const 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; }, {});
2020-02-05 15:05:34 +01:00
2024-10-04 17:43:45 +02:00
const API_ORIGIN = window.cloudronApiOrigin || 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.branding = null;
2024-04-03 18:23:29 +02:00
$scope.dashboardUrl = '';
2020-02-05 15:05:34 +01:00
$scope.profileLocked = !!search.profileLocked;
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 = {
2021-10-01 12:27:59 +02:00
inviteToken: search.inviteToken,
2020-02-05 15:05:34 +01:00
password: $scope.password
};
if (!$scope.profileLocked) {
if (!$scope.existingUsername) data.username = $scope.username;
data.displayName = $scope.displayName;
}
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
}
2023-08-10 16:21:22 +05:30
$http.post(API_ORIGIN + '/api/v1/auth/setup_account', data).success(function (data, status) {
2020-02-05 16:12:52 +01:00
if (status !== 201) return error(data, status);
2020-02-05 15:05:34 +01:00
// set token to autologin on first oidc flow
2024-04-03 18:23:29 +02:00
localStorage.cloudronFirstTimeToken = data.accessToken;
$scope.dashboardUrl = '/openid/auth?client_id=cid-webadmin&scope=openid email profile&response_type=code token&redirect_uri=' + window.location.origin + '/authcallback.html';
2020-02-05 15:05:34 +01:00
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
if (!$scope.existingUsername && $scope.profileLocked) {
$scope.view = 'noUsername';
$scope.initialized = true;
} else {
$http.get(API_ORIGIN + '/api/v1/auth/branding').success(function (data, status) {
$scope.initialized = true;
if (status !== 200) return;
if (data.language) $translate.use(data.language);
2020-11-19 11:33:46 +01:00
$scope.branding = data;
}).error(function () {
$scope.initialized = false;
});
}
2020-02-05 15:05:34 +01:00
}]);