| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Privater Bereich</title>
- <link rel="stylesheet" href="../css/bootstrap.min.css" />
- <link rel="stylesheet" href="../css/styles.css" />
- </head>
- <body>
- <div class="container my-5">
- <h1>Privater Bereich</h1>
- <p class="alert alert-warning">Hier solltest du nur sein, wenn du dich davor eingeloggt hast</p>
- <h2>Kleine Chat App</h2>
- <p>(Inklusive XSRF-Token-Security)</p>
- <div class="chat-box">
- <div id="chat-messages"></div>
- <div class="row">
- <div class="col input-group">
- <input type="text" id="chat-input" class="form-control" />
- <button type="button" id="chat-submit" class="btn btn-outline-secondary" onclick="sendMessage()">Senden</button>
- </div>
- </div>
- </div>
- <h2>Logout</h2>
- <form method="POST" action="/auth/logout" enctype="application/x-www-form-urlencoded" class="login-form">
- <table border="0" cellpadding="0" cellspacing="0">
- <tr>
- <td colspan="2">
- <input type="submit" name="submit" value="Logout" />
- </td>
- </tr>
- </table>
- </form>
- </div>
- <script type="text/javascript" src="js/jquery.min.js"></script>
- <script type="text/javascript">
- var chat = {};
- $(function () {
- chat.container = $('#chat-messages');
- chat.input = $('#chat-input');
- chat.submitBtn = $('#chat-submit');
- chat.input.on('keyup', function (e) {
- if (e.keyCode === 13) {
- // Enter
- chat.submitBtn.click();
- }
- });
- fetchMessages();
- });
- function fetchMessages() {
- $.ajax({
- url: 'chat',
- method: 'GET',
- dataType: 'json',
- success: function (data, textStatus, xhr) {
- if (data && data.length > 0) {
- chat.messages = data;
- renderMessages();
- } else {
- chat.container.append('<p>There are no messages yet to display</p>');
- chat.messages = [];
- }
- },
- error: function (xhr, textStatus, errorMsg) {
- console.error(xhr, textStatus, errorMsg);
- }
- });
- }
- function renderMessages() {
- chat.container.empty();
- chat.messages.forEach((msg, i) => renderMessage(msg, i));
- }
- function renderMessage(msg, idx) {
- chat.container.append(
- '<div class="card chat-message">' +
- '<div class="card-body">' +
- '<h5 class="card-title">' +
- msg.author +
- '</h5>' +
- '<button class="btn btn-outline-danger btn-sm" onclick="deleteMessage(' +
- idx +
- ')">Delete</button>' +
- '<h6 class="card-subtitle mb-2 text-muted">' +
- msg.time +
- '</h6>' +
- '<p class="card-text">' +
- msg.text +
- '</p>' +
- '</div>' +
- '</div>'
- );
- }
- function sendMessage() {
- var text = chat.input.val();
- if (!text || text.trim().length == 0) return;
- var token = getXsrfTokenFromCookie();
- if (!token) {
- alert('Konnte XSRF-TOKEN Cookie nicht auslesen');
- return;
- }
- $.ajax({
- url: 'chat',
- method: 'POST',
- dataType: 'json',
- data: {
- text
- },
- headers: {
- 'X-CSRF-Token': token
- },
- success: function (data, textStatus, xhr) {
- renderMessage(data, chat.messages.length);
- chat.input.val('');
- },
- error: function (xhr, textStatus, errorMsg) {
- console.error(xhr, textStatus, errorMsg);
- alert(errorMsg + ': ' + xhr.responseText);
- }
- });
- }
- function deleteMessage(idx) {
- var token = getXsrfTokenFromCookie();
- if (!token) {
- alert('Konnte XSRF-TOKEN Cookie nicht auslesen');
- return;
- }
- $.ajax({
- url: 'chat/' + idx,
- method: 'DELETE',
- dataType: 'json',
- headers: {
- 'X-CSRF-Token': token
- },
- success: function (data, textStatus, xhr) {
- fetchMessages();
- },
- error: function (xhr, textStatus, errorMsg) {
- console.error(xhr, textStatus, errorMsg);
- alert(errorMsg + ': ' + xhr.responseText);
- }
- });
- }
- function getXsrfTokenFromCookie() {
- var regMatch = /XSRF-TOKEN=([^;]+).*$/.exec(document.cookie);
- if (!regMatch || !regMatch[1]) {
- return null;
- }
- return regMatch[1];
- }
- </script>
- </body>
- </html>
|