summary refs log tree commit diff
path: root/synapse/static/client/register/js/register.js
blob: 959d120d7d4734739ee6d49fa6040049eb01396a (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
window.matrixRegistration = {
    endpoint: location.origin + "/_matrix/client/api/v1/register"
};

var setupCaptcha = function() {
    if (!window.matrixRegistrationConfig) {
        return;
    }
    $.get(matrixRegistration.endpoint, function(response) {
        var serverExpectsCaptcha = false;
        for (var i=0; i<response.flows.length; i++) {
            var flow = response.flows[i];
            if ("m.login.recaptcha" === flow.type) {
                serverExpectsCaptcha = true;
                break;
            }
        }
        if (!serverExpectsCaptcha) {
            console.log("This server does not require a captcha.");
            return;
        }
        console.log("Setting up ReCaptcha for "+matrixRegistration.endpoint);
        var public_key = window.matrixRegistrationConfig.recaptcha_public_key;
        if (public_key === undefined) {
            console.error("No public key defined for captcha!");
            setFeedbackString("Misconfigured captcha for server. Contact server admin.");
            return;
        }
        Recaptcha.create(public_key,
        "regcaptcha",
        {
            theme: "red",
            callback: Recaptcha.focus_response_field
        });
        window.matrixRegistration.isUsingRecaptcha = true;
    }).fail(errorFunc);
    
};

var submitCaptcha = function(user, pwd) {
    var challengeToken = Recaptcha.get_challenge();
    var captchaEntry = Recaptcha.get_response();
    var data = {
        type: "m.login.recaptcha",
        challenge: challengeToken,
        response: captchaEntry
    };
    console.log("Submitting captcha");
    $.post(matrixRegistration.endpoint, JSON.stringify(data), function(response) {
        console.log("Success -> "+JSON.stringify(response));
        submitPassword(user, pwd, response.session);
    }).fail(function(err) {
        Recaptcha.reload();
        errorFunc(err);
    });
};

var submitPassword = function(user, pwd, session) {
    console.log("Registering...");
    var data = {
        type: "m.login.password",
        user: user,
        password: pwd,
        session: session
    };
    $.post(matrixRegistration.endpoint, JSON.stringify(data), function(response) {
        matrixRegistration.onRegistered(
            response.home_server, response.user_id, response.access_token
        );
    }).fail(errorFunc);
};

var errorFunc = function(err) {
    if (err.responseJSON && err.responseJSON.error) {
        setFeedbackString(err.responseJSON.error + " (" + err.responseJSON.errcode + ")");
    }
    else {
        setFeedbackString("Request failed: " + err.status);
    }
};

var setFeedbackString = function(text) {
    $("#feedback").text(text);
};

matrixRegistration.onLoad = function() {
    setupCaptcha();
};

matrixRegistration.signUp = function() {
    var user = $("#desired_user_id").val();
    if (user.length == 0) {
        setFeedbackString("Must specify a username.");
        return;
    }
    var pwd1 = $("#pwd1").val();
    var pwd2 = $("#pwd2").val();
    if (pwd1.length < 6) {
        setFeedbackString("Password: min. 6 characters.");
        return;
    }
    if (pwd1 != pwd2) {
        setFeedbackString("Passwords do not match.");
        return;
    }
    if (window.matrixRegistration.isUsingRecaptcha) {
        submitCaptcha(user, pwd1);
    }
    else {
        submitPassword(user, pwd1);
    }
};

matrixRegistration.onRegistered = function(hs_url, user_id, access_token) {
    // clobber this function
    console.log("onRegistered - This function should be replaced to proceed.");
};