aboutsummaryrefslogtreecommitdiffhomepage
path: root/main/app/sprinkles/account/assets/userfrosting/js/pages/sign-in.js
blob: 769a1b3bd4509270011079f264310636a526f8ee (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
/**
 * Page-specific Javascript file.  Should generally be included as a separate asset bundle in your page template.
 * example: {{ assets.js('js/pages/sign-in-or-register') | raw }}
 *
 * This script depends on validation rules specified in pages/partials/page.js.twig.
 *
 * Target page: account/sign-in
 */
$(document).ready(function () {
    /**
     * If there is a redirect parameter in the query string, redirect to that page.
     * Otherwise, if there is a UF-Redirect header, redirect to that page.
     * Otherwise, redirect to the home page.
     */
    function redirectOnLogin(jqXHR) {
        var components = URI.parse(window.location.href);
        var query = URI.parseQuery(components['query']);

        if (query && query['redirect']) {
            // Strip leading slashes from redirect strings           
            var redirectString = site.uri.public + '/' + query['redirect'].replace(/^\/+/, "");
            // Strip excess trailing slashes for clean URLs. e.g. if redirect=%2F
            redirectString = redirectString.replace(/\/+$/, "/");
            // Redirect
            window.location.replace(redirectString);
        } else if (jqXHR.getResponseHeader('UF-Redirect')) {
            window.location.replace(jqXHR.getResponseHeader('UF-Redirect'));
        } else {
            window.location.replace(site.uri.public);
        }
    }

    $("#sign-in").ufForm({
        validators: page.validators.login,
        msgTarget: $("#alerts-page")
    }).on("submitSuccess.ufForm", function (event, data, textStatus, jqXHR) {
        if (localStorage.getItem("PrivateKey") === null && localStorage.getItem("🔒") === null) {
            // GENERATE KEYS
            var openpgp = window.openpgp;
            var options;
            var randomString = Math.random().toString(36).substr(2, 11); // PRIVKEY ENCRYPTION KEY
            openpgp.initWorker({path: '/assets-raw/core/assets/SiteAssets/js/openpgp.worker.js'});
            options = {
                userIds: [{name: $("input[name=user_name]").val()}],
                curve: "curve25519",
                passphrase: randomString
            };
            openpgp.generateKey(options).then(function (key) {
                localStorage.setItem("PrivateKey", key.privateKeyArmored);
                localStorage.setItem("🔒", randomString);

                console.log(key.publicKeyArmored);
                console.log(key.privateKeyArmored);
                // SAVE PUBLIC KEY TO DATABASE
                var data = {
                    csrf_name: site.csrf.name,
                    csrf_value: site.csrf.value,
                    PublicKey: key.publicKeyArmored
                };
                $.ajax({
                    type: 'POST',
                    dataType: "json",
                    url: site.uri.public + '/api/users/u/' + $("input[name=user_name]").val() + '/publickey',
                    data: data,
                    async: false
                });
                redirectOnLogin(jqXHR);
            });
        } else {
            redirectOnLogin(jqXHR);
        }
    });
});