register.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. window.matrixRegistration = {
  2. endpoint: location.origin + "/_matrix/client/api/v1/register"
  3. };
  4. var setupCaptcha = function() {
  5. if (!window.matrixRegistrationConfig) {
  6. return;
  7. }
  8. $.get(matrixRegistration.endpoint, function(response) {
  9. var serverExpectsCaptcha = false;
  10. for (var i=0; i<response.flows.length; i++) {
  11. var flow = response.flows[i];
  12. if ("m.login.recaptcha" === flow.type) {
  13. serverExpectsCaptcha = true;
  14. break;
  15. }
  16. }
  17. if (!serverExpectsCaptcha) {
  18. console.log("This server does not require a captcha.");
  19. return;
  20. }
  21. console.log("Setting up ReCaptcha for "+matrixRegistration.endpoint);
  22. var public_key = window.matrixRegistrationConfig.recaptcha_public_key;
  23. if (public_key === undefined) {
  24. console.error("No public key defined for captcha!");
  25. setFeedbackString("Misconfigured captcha for server. Contact server admin.");
  26. return;
  27. }
  28. Recaptcha.create(public_key,
  29. "regcaptcha",
  30. {
  31. theme: "red",
  32. callback: Recaptcha.focus_response_field
  33. });
  34. window.matrixRegistration.isUsingRecaptcha = true;
  35. }).error(errorFunc);
  36. };
  37. var submitCaptcha = function(user, pwd) {
  38. var challengeToken = Recaptcha.get_challenge();
  39. var captchaEntry = Recaptcha.get_response();
  40. var data = {
  41. type: "m.login.recaptcha",
  42. challenge: challengeToken,
  43. response: captchaEntry
  44. };
  45. console.log("Submitting captcha");
  46. $.post(matrixRegistration.endpoint, JSON.stringify(data), function(response) {
  47. console.log("Success -> "+JSON.stringify(response));
  48. submitPassword(user, pwd, response.session);
  49. }).error(function(err) {
  50. Recaptcha.reload();
  51. errorFunc(err);
  52. });
  53. };
  54. var submitPassword = function(user, pwd, session) {
  55. console.log("Registering...");
  56. var data = {
  57. type: "m.login.password",
  58. user: user,
  59. password: pwd,
  60. session: session
  61. };
  62. $.post(matrixRegistration.endpoint, JSON.stringify(data), function(response) {
  63. matrixRegistration.onRegistered(
  64. response.home_server, response.user_id, response.access_token
  65. );
  66. }).error(errorFunc);
  67. };
  68. var errorFunc = function(err) {
  69. if (err.responseJSON && err.responseJSON.error) {
  70. setFeedbackString(err.responseJSON.error + " (" + err.responseJSON.errcode + ")");
  71. }
  72. else {
  73. setFeedbackString("Request failed: " + err.status);
  74. }
  75. };
  76. var setFeedbackString = function(text) {
  77. $("#feedback").text(text);
  78. };
  79. matrixRegistration.onLoad = function() {
  80. setupCaptcha();
  81. };
  82. matrixRegistration.signUp = function() {
  83. var user = $("#desired_user_id").val();
  84. if (user.length == 0) {
  85. setFeedbackString("Must specify a username.");
  86. return;
  87. }
  88. var pwd1 = $("#pwd1").val();
  89. var pwd2 = $("#pwd2").val();
  90. if (pwd1.length < 6) {
  91. setFeedbackString("Password: min. 6 characters.");
  92. return;
  93. }
  94. if (pwd1 != pwd2) {
  95. setFeedbackString("Passwords do not match.");
  96. return;
  97. }
  98. if (window.matrixRegistration.isUsingRecaptcha) {
  99. submitCaptcha(user, pwd1);
  100. }
  101. else {
  102. submitPassword(user, pwd1);
  103. }
  104. };
  105. matrixRegistration.onRegistered = function(hs_url, user_id, access_token) {
  106. // clobber this function
  107. console.log("onRegistered - This function should be replaced to proceed.");
  108. };