summary refs log tree commit diff
path: root/synapse/res/username_picker/script.js
blob: 416a7c6f41dd9a0374a137ba3e7ae71eb20c15ad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
let inputField = document.getElementById("field-username");
let inputForm = document.getElementById("form");
let submitButton = document.getElementById("button-submit");
let message = document.getElementById("message");

// Submit username and receive response
function showMessage(messageText) {
    // Unhide the message text
    message.classList.remove("hidden");

    message.textContent = messageText;
};

function doSubmit() {
    showMessage("Success. Please wait a moment for your browser to redirect.");

    // remove the event handler before re-submitting the form.
    delete inputForm.onsubmit;
    inputForm.submit();
}

function onResponse(response) {
    // Display message
    showMessage(response);

    // Enable submit button and input field
    submitButton.classList.remove('button--disabled');
    submitButton.value = "Submit";
};

let allowedUsernameCharacters = RegExp("[^a-z0-9\\.\\_\\=\\-\\/]");
function usernameIsValid(username) {
    return !allowedUsernameCharacters.test(username);
}
let allowedCharactersString = "lowercase letters, digits, ., _, -, /, =";

function buildQueryString(params) {
    return Object.keys(params)
        .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k]))
        .join('&');
}

function submitUsername(username) {
    if(username.length == 0) {
        onResponse("Please enter a username.");
        return;
    }
    if(!usernameIsValid(username)) {
        onResponse("Invalid username. Only the following characters are allowed: " + allowedCharactersString);
        return;
    }

    // if this browser doesn't support fetch, skip the availability check.
    if(!window.fetch) {
        doSubmit();
        return;
    }

    let check_uri = 'check?' + buildQueryString({"username": username});
    fetch(check_uri, {
        // include the cookie
        "credentials": "same-origin",
    }).then((response) => {
        if(!response.ok) {
            // for non-200 responses, raise the body of the response as an exception
            return response.text().then((text) => { throw text; });
        } else {
            return response.json();
        }
    }).then((json) => {
        if(json.error) {
            throw json.error;
        } else if(json.available) {
            doSubmit();
        } else {
            onResponse("This username is not available, please choose another.");
        }
    }).catch((err) => {
        onResponse("Error checking username availability: " + err);
    });
}

function clickSubmit() {
    event.preventDefault();
    if(submitButton.classList.contains('button--disabled')) { return; }

    // Disable submit button and input field
    submitButton.classList.add('button--disabled');

    // Submit username
    submitButton.value = "Checking...";
    submitUsername(inputField.value);
};

inputForm.onsubmit = clickSubmit;