|
|
@@ -14,6 +14,18 @@
|
|
|
<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">
|
|
|
@@ -25,5 +37,136 @@
|
|
|
</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>
|