index.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>Privater Bereich</title>
  8. <link rel="stylesheet" href="../css/bootstrap.min.css" />
  9. <link rel="stylesheet" href="../css/styles.css" />
  10. </head>
  11. <body>
  12. <div class="container my-5">
  13. <h1>Privater Bereich</h1>
  14. <p class="alert alert-warning">Hier solltest du nur sein, wenn du dich davor eingeloggt hast</p>
  15. <h2>Kleine Chat App</h2>
  16. <p>(Inklusive XSRF-Token-Security)</p>
  17. <div class="chat-box">
  18. <div id="chat-messages"></div>
  19. <div class="row">
  20. <div class="col input-group">
  21. <input type="text" id="chat-input" class="form-control" />
  22. <button type="button" id="chat-submit" class="btn btn-outline-secondary" onclick="sendMessage()">Senden</button>
  23. </div>
  24. </div>
  25. </div>
  26. <h2>Logout</h2>
  27. <form method="POST" action="/auth/logout" enctype="application/x-www-form-urlencoded" class="login-form">
  28. <table border="0" cellpadding="0" cellspacing="0">
  29. <tr>
  30. <td colspan="2">
  31. <input type="submit" name="submit" value="Logout" />
  32. </td>
  33. </tr>
  34. </table>
  35. </form>
  36. </div>
  37. <script type="text/javascript" src="js/jquery.min.js"></script>
  38. <script type="text/javascript">
  39. var chat = {};
  40. $(function () {
  41. chat.container = $('#chat-messages');
  42. chat.input = $('#chat-input');
  43. chat.submitBtn = $('#chat-submit');
  44. chat.input.on('keyup', function (e) {
  45. if (e.keyCode === 13) {
  46. // Enter
  47. chat.submitBtn.click();
  48. }
  49. });
  50. fetchMessages();
  51. });
  52. function fetchMessages() {
  53. $.ajax({
  54. url: 'chat',
  55. method: 'GET',
  56. dataType: 'json',
  57. success: function (data, textStatus, xhr) {
  58. if (data && data.length > 0) {
  59. chat.messages = data;
  60. renderMessages();
  61. } else {
  62. chat.container.append('<p>There are no messages yet to display</p>');
  63. chat.messages = [];
  64. }
  65. },
  66. error: function (xhr, textStatus, errorMsg) {
  67. console.error(xhr, textStatus, errorMsg);
  68. }
  69. });
  70. }
  71. function renderMessages() {
  72. chat.container.empty();
  73. chat.messages.forEach(msg => renderMessage(msg));
  74. }
  75. function renderMessage(msg) {
  76. chat.container.append(
  77. '<div class="card chat-message">' +
  78. '<div class="card-body">' +
  79. '<h5 class="card-title">' +
  80. msg.author +
  81. '</h5>' +
  82. '<button class="btn btn-outline-danger btn-sm" onclick="deleteMessage(' +
  83. msg.id +
  84. ')">Delete</button>' +
  85. '<h6 class="card-subtitle mb-2 text-muted">' +
  86. msg.time +
  87. '</h6>' +
  88. '<p class="card-text">' +
  89. msg.text +
  90. '</p>' +
  91. '</div>' +
  92. '</div>'
  93. );
  94. }
  95. function sendMessage() {
  96. var text = chat.input.val();
  97. if (!text || text.trim().length == 0) return;
  98. var token = getXsrfTokenFromCookie();
  99. if (!token) {
  100. alert('Konnte XSRF-TOKEN Cookie nicht auslesen');
  101. return;
  102. }
  103. $.ajax({
  104. url: 'chat',
  105. method: 'POST',
  106. dataType: 'json',
  107. data: {
  108. text
  109. },
  110. headers: {
  111. 'X-CSRF-Token': token
  112. },
  113. success: function (data, textStatus, xhr) {
  114. renderMessage(data);
  115. chat.input.val('');
  116. },
  117. error: function (xhr, textStatus, errorMsg) {
  118. console.error(xhr, textStatus, errorMsg);
  119. alert(errorMsg + ': ' + xhr.responseText);
  120. }
  121. });
  122. }
  123. function deleteMessage(id) {
  124. var token = getXsrfTokenFromCookie();
  125. if (!token) {
  126. alert('Konnte XSRF-TOKEN Cookie nicht auslesen');
  127. return;
  128. }
  129. $.ajax({
  130. url: 'chat/' + id,
  131. method: 'DELETE',
  132. dataType: 'json',
  133. headers: {
  134. 'X-CSRF-Token': token
  135. },
  136. success: function (data, textStatus, xhr) {
  137. fetchMessages();
  138. },
  139. error: function (xhr, textStatus, errorMsg) {
  140. console.error(xhr, textStatus, errorMsg);
  141. alert(errorMsg + ': ' + xhr.responseText);
  142. }
  143. });
  144. }
  145. function getXsrfTokenFromCookie() {
  146. var regMatch = /XSRF-TOKEN=([^;]+).*$/.exec(document.cookie);
  147. if (!regMatch || !regMatch[1]) {
  148. return null;
  149. }
  150. return regMatch[1];
  151. }
  152. </script>
  153. </body>
  154. </html>