summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--static/client/register/index.html14
-rw-r--r--static/client/register/js/register.js123
2 files changed, 113 insertions, 24 deletions
diff --git a/static/client/register/index.html b/static/client/register/index.html
index 2248d6e5a8..d7ed8a1fe7 100644
--- a/static/client/register/index.html
+++ b/static/client/register/index.html
@@ -2,6 +2,7 @@
 <head>
 <title> Registration </title>
 <link rel="stylesheet" href="style.css">
+<script src="js/jquery-2.1.3.min.js"></script>
 <script src="js/recaptcha_ajax.js"></script>
 <script src="register_config.js"></script>
 <script src="js/register.js"></script>
@@ -18,16 +19,11 @@
         	<br/>
         	<input id="pwd2" size="32" type="password" placeholder="Confirm your password"/>
         	<br/>
+            <span id="feedback" style="color: #f00"></span>
+            <br/>
+        	<div id="regcaptcha"></div>
 
-        	<div id="regcaptcha" />
-
-        	<button style="margin: 10px">Sign up</button>
-        </div>
-
-        <div id="serverConfig" ng-show="!wait_3pid_code">
-            <label for="homeserver">Home Server:</label> 
-            <input id="homeserver" size="32" type="text" placeholder="URL (e.g. http://matrix.org:8080)"/>
-            <div class="smallPrint">Your home server stores all your conversation and account data.</div>
+        	<button type="button" style="margin: 10px" onclick="matrixRegistration.signUp()">Sign up</button>
         </div>
     </div>
 </form>
diff --git a/static/client/register/js/register.js b/static/client/register/js/register.js
index b8a9247e6d..38408f731f 100644
--- a/static/client/register/js/register.js
+++ b/static/client/register/js/register.js
@@ -1,23 +1,116 @@
-window.matrixRegistration = {};
+window.matrixRegistration = {
+    endpoint: location.origin + "/_matrix/client/api/v1/register"
+};
 
 var setupCaptcha = function() {
-	if (!window.matrixRegistrationConfig) {
-		return;
-	}
-    console.log("Setting up ReCaptcha");
-    var public_key = window.matrixRegistrationConfig.recaptcha_public_key;
-    if (public_key === undefined) {
-        console.error("No public key defined for captcha!");
+    if (!window.matrixRegistrationConfig) {
         return;
     }
-    Recaptcha.create(public_key,
-    "regcaptcha",
-    {
-      theme: "red",
-      callback: Recaptcha.focus_response_field
+    $.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!");
+            return;
+        }
+        Recaptcha.create(public_key,
+        "regcaptcha",
+        {
+            theme: "red",
+            callback: Recaptcha.focus_response_field
+        });
+        window.matrixRegistration.isUsingRecaptcha = true;
+    }).error(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);
+    }).error(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
+        );
+    }).error(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();
-};
\ No newline at end of file
+    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.");
+};