您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

112 行
3.2 KiB

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <meta content='IE=edge' http-equiv='X-UA-Compatible'>
  6. <title>Simple Example</title>
  7. <meta content='width=device-width, initial-scale=1' name='viewport'>
  8. </head>
  9. <body>
  10. <form id="registerForm">
  11. <h1>Register</h1>
  12. <p id="registerStatus"></p>
  13. <label for="emailInput">
  14. E-Mail
  15. <input id="emailInput" name="email" placeholder="E-Mail" type="email">
  16. </label>
  17. <label for="passwordInput">
  18. Password
  19. <input id="passwordInput" name="password" placeholder="Password" type="password">
  20. </label>
  21. <input id="registerSubmit" type="submit" value="Submit">
  22. </form>
  23. <form id="loginForm">
  24. <h1>Login</h1>
  25. <p id="loginStatus"></p>
  26. <label for="loginEmailInput">
  27. E-Mail
  28. <input id="loginEmailInput" name="username" placeholder="E-Mail" type="email">
  29. </label>
  30. <label for="loginPasswordInput">
  31. Password
  32. <input id="loginPasswordInput" name="password" placeholder="Password" type="password">
  33. </label>
  34. <input id="loginSubmit" type="submit" value="Submit">
  35. </form>
  36. <div>
  37. <h1>Accessing private routes</h1>
  38. <p> Make request to a privte route:</p>
  39. <button id="privateRequest">Request</button>
  40. <p id="privateStatus"></p>
  41. </div>
  42. </body>
  43. <script>
  44. const registerSubmit = document.getElementById("registerSubmit");
  45. registerSubmit.onclick = (ev) => {
  46. ev.preventDefault();
  47. const registerForm = document.getElementById("registerForm");
  48. const data = new FormData(registerForm);
  49. // convert formdata to json
  50. let object = {}
  51. data.forEach((value, key) => object[key] = value)
  52. // send data to the backend route
  53. fetch("/auth/register", {
  54. method: "POST",
  55. body: JSON.stringify(object)
  56. }).then((response) => response.json())
  57. .then((data) => {
  58. const status = document.getElementById("registerStatus");
  59. status.innerText = data.detail;
  60. })
  61. .catch((err) => {
  62. console.log("Error: ", err)
  63. })
  64. }
  65. let token = undefined;
  66. const loginSubmit = document.getElementById("loginSubmit");
  67. loginSubmit.onclick = (ev) => {
  68. ev.preventDefault();
  69. const loginForm = document.getElementById("loginForm")
  70. const data = new FormData(loginForm)
  71. let xhr = new XMLHttpRequest();
  72. xhr.open("POST", "/auth/token", true);
  73. xhr.onload = (ev) => {
  74. const status = document.getElementById("loginStatus")
  75. const responseData = JSON.parse(xhr.responseText)
  76. if (xhr.status == 200) {
  77. status.innerText = "Successfully logged in, token: " + responseData.access_token;
  78. token = `${responseData.token_type} ${responseData.access_token}`;
  79. } else {
  80. status.innerText = "Error logging in: " + responseData.detail
  81. }
  82. }
  83. xhr.send(data)
  84. }
  85. const privateRequest = document.getElementById("privateRequest");
  86. privateRequest.onclick = (ev) => {
  87. fetch("/private", {
  88. method: "GET",
  89. headers: {
  90. "Authorization": token
  91. }
  92. })
  93. .then(response => response.json())
  94. .then(data => {
  95. const status = document.getElementById("privateStatus");
  96. if (status.ok) {
  97. status.innerText = data.detail
  98. } else {
  99. status.innerText = data.detail
  100. }
  101. })
  102. }
  103. </script>
  104. </html>