<commit_before>/**
 * Created by Omnius on 18/08/2016.
 */
'use strict';

const submitForm = () => {

    $('#alertInvalid').addClass('hidden');
    $('#alertInvalidUsername').addClass('hidden');
    $('#alertInvalidEmail').addClass('hidden');
    $('#alertInvalidPassword').addClass('hidden');
    $('#alertInvalidFirstName').addClass('hidden');
    $('#alertInvalidSurname').addClass('hidden');
    $('#alertInvalidBirthDate').addClass('hidden');
    $('#alertUserExists').addClass('hidden');

    // TODO: Supposedly needs sanitation using ESAPI.encoder().encodeForJavascript() or some other sanitation
    // mechanism on input fields
    const username = $('#inputUsername').val();
    const email = $('#inputEmail').val();
    const password = $('#inputPassword').val();
    const firstName = $('#inputFirstName').val();
    const surname = $('#inputSurname').val();
    const birthDate = $('#inputBirthDate').val();

    const payload = {
        username: username,
        email: email,
        password: password,
        firstname: firstName,
        surname: surname,
        birthdate: birthDate
    };

    $.ajax({
        type: 'POST',
        url: '/user/registration',
        contentType: 'application/json; charset=utf-8',
        processData: false,
        beforeSend: (xhr) => {
            const crumbz = MyUtils.getCookie('Crumbz');
            xhr.setRequestHeader('X-CSRF-Token', crumbz);
        },
        data: JSON.stringify(payload),
        success: function (data, textStatus, xhr) {
            if (data === 'registered') {
                const protocol = location.protocol + '//';
                window.location = protocol + location.host + '/login?registered=true';
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            const csrf = xhr.getResponseHeader('x-csrf-token');

            if (csrf) {
                document.cookie = 'Crumbz=' + csrf + '; path=/';
            }

            if (xhr.status === 400 && textStatus === 'error') {
                $('#alertInvalid').removeClass('hidden');

                if (xhr.responseJSON.message.toLowerCase().indexOf('username') > -1) {
                    $('#alertInvalidUsername').removeClass('hidden');
                }
                if (xhr.responseJSON.message.toLowerCase().indexOf('email') > -1) {
                    $('#alertInvalidEmail').removeClass('hidden');
                }
                if (xhr.responseJSON.message.toLowerCase().indexOf('password') > -1) {
                    $('#alertInvalidPassword').removeClass('hidden');
                }<commit_msg>Add crumb to xhr payload in client-side<commit_after>/**
 * Created by Omnius on 18/08/2016.
 */
'use strict';

const submitForm = () => {

    $('#alertInvalid').addClass('hidden');
    $('#alertInvalidUsername').addClass('hidden');
    $('#alertInvalidEmail').addClass('hidden');
    $('#alertInvalidPassword').addClass('hidden');
    $('#alertInvalidFirstName').addClass('hidden');
    $('#alertInvalidSurname').addClass('hidden');
    $('#alertInvalidBirthDate').addClass('hidden');
    $('#alertUserExists').addClass('hidden');

    // TODO: Supposedly needs sanitation using ESAPI.encoder().encodeForJavascript() or some other sanitation
    // mechanism on input fields
    const username = $('#inputUsername').val();
    const email = $('#inputEmail').val();
    const password = $('#inputPassword').val();
    const firstName = $('#inputFirstName').val();
    const surname = $('#inputSurname').val();
    const birthDate = $('#inputBirthDate').val();
    const crumbz = $('#inputCrumbz').val();

    const payload = {
        username: username,
        email: email,
        password: password,
        firstname: firstName,
        surname: surname,
        birthdate: birthDate,
        Crumbz: crumbz
    };

    $.ajax({
        type: 'POST',
        url: '/user/registration',
        contentType: 'application/json; charset=utf-8',
        processData: false,
        beforeSend: (xhr) => {
            // const crumbz = MyUtils.getCookie('Crumbz');
            // xhr.setRequestHeader('X-CSRF-Token', crumbz);
        },
        data: JSON.stringify(payload),
        success: function (data, textStatus, xhr) {
            if (data === 'registered') {
                const protocol = location.protocol + '//';
                window.location = protocol + location.host + '/login?registered=true';
            }
        },
        error: function (xhr, textStatus, errorThrown) {
            // const csrf = xhr.getResponseHeader('x-csrf-token');
            //
            // if (csrf) {
            //     document.cookie = 'Crumbz=' + csrf + '; path=/';
            // }

            if (xhr.status === 400 && textStatus === 'error') {
                $('#alertInvalid').removeClass('hidden');

                if (xhr.responseJSON.message.toLowerCase().indexOf('username') > -1) {
                    $('#alertInvalidUsername').removeClass('hidden');
                }
                if (xhr.responseJSON.message.toLowerCase().indexOf('email') > -1) {
                    $('#alertInvalidEmail').removeClass('hidden');
                }
                if (xhr.responseJSON.message.toLowerCase().indexOf('password') > -1) {
                    $('#alertInvalidPassword').removeClass('hidden');
                }
{'fraction_non_alphanumeric': 0.16991017964071856, 'fraction_numerical': 0.00561377245508982, 'mean_word_length': 2.3095975232198143, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '932377', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1406, 'n_words': 271}