Extra checks and delete confirmation for pastes

## Copilot Generated Commit Details Below

Enhance auth and UI with notifications and delete confirmation

Enhanced authentication in Program.cs by adding checks for the
logged-in user and ensuring proper response handling. Improved
UI in paste.html and privatepaste.html by adding a confirmation
dialog for deletions and a notification system to inform users
of actions and errors. Added CSS styles to support these new
features with appropriate styling and animations.
This commit is contained in:
whoswhip
2024-10-18 20:44:19 -04:00
parent 49f8911be2
commit 0d5fd3bc5d
3 changed files with 258 additions and 27 deletions
+10 -1
View File
@@ -5,6 +5,7 @@ using Newtonsoft.Json.Linq;
using System.Data; using System.Data;
using System.Text; using System.Text;
using Sharpbin; using Sharpbin;
using System.Reflection;
class Program class Program
{ {
@@ -2296,6 +2297,7 @@ class Program
await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Unauthorized" }.ToString()); await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Unauthorized" }.ToString());
return; return;
} }
var user = await GetLoggedInUserAsync(token); var user = await GetLoggedInUserAsync(token);
if (user == null) if (user == null)
{ {
@@ -2304,6 +2306,13 @@ class Program
await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Unauthorized" }.ToString()); await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Unauthorized" }.ToString());
return; return;
} }
if (user.UUID == null)
{
context.Response.StatusCode = 401;
context.Response.ContentType = "application/json";
await context.Response.WriteAsJsonAsync(new JObject { ["error"] = "Unauthorized" }.ToString());
return;
}
var responseJson = new JObject(); var responseJson = new JObject();
responseJson["message"] = "Authenticated"; responseJson["message"] = "Authenticated";
@@ -2317,7 +2326,7 @@ class Program
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
context.Response.ContentType = "application/json"; context.Response.ContentType = "application/json";
await context.Response.WriteAsJsonAsync(responseJson.ToString()); await context.Response.WriteAsJsonAsync(responseJson);
})); }));
#endregion #endregion
+148 -3
View File
@@ -32,8 +32,9 @@
</div> </div>
<div class="container-wrapper"> <div class="container-wrapper">
<div class="paste-container"> <div class="paste-container">
<h1 id="paste-title" ></h1> <h1 id="paste-title"></h1>
<h3 class="uploader-text"><a id="uploader-username" ></a> <a id="creationdate"></a> <a id="paste-size"></a></h3> <h3 class="uploader-text"><a id="uploader-username"></a> <a id="creationdate"></a> <a id="paste-size"></a>
</h3>
<div class="paste-wrapper"> <div class="paste-wrapper">
<p id="text"></p> <p id="text"></p>
</div> </div>
@@ -47,6 +48,18 @@
</div> </div>
</div> </div>
</div> </div>
<div class="notifcations-wrapper" id="notifications"></div>
<div class="input-wrapper" id="input-wrapper">
<div class="input-wrapper-container">
<h2>
Are you sure you want to delete this paste?
</h2>
<div>
<button id="yes-button" class="button">Yes</button>
<button id="no-button" class="button">No</button>
</div>
</div>
</div>
</body> </body>
<script> <script>
info = ""; info = "";
@@ -151,7 +164,61 @@
element.click(); element.click();
document.body.removeChild(element); document.body.removeChild(element);
}); });
{ deletescript } document.getElementById("delete-button").addEventListener("click", function () {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.style.display = 'flex';
});
document.getElementById("yes-button").addEventListener("click", function () {
var requestUrl = "/api/pastes/delete";
fetch(requestUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: '{pasteid}'
})
})
.then(response => response.json())
.then(obj => {
const result = JSON.parse(obj);
if (result.message != null) {
window.location.href = `/`;
}else{
createNotification(`Error: ${result.error}`);
}
});
});
document.getElementById("no-button").addEventListener("click", function () {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.style.display = 'none';
});
function createNotification(message) {
const notificationWrapper = document.getElementById('notifications');
const notificationDiv = document.createElement('div');
notificationDiv.classList.add('notification-div');
notificationDiv.id = 'notification';
const notificationText = document.createElement('h2');
notificationText.id = 'notification-text';
notificationText.innerText = message;
notificationDiv.appendChild(notificationText);
notificationWrapper.appendChild(notificationDiv);
notificationDiv.style.animation = 'none';
notificationDiv.style.display = 'none';
notificationDiv.style.display = 'flex';
notificationDiv.style.animation = 'hoverup 1s';
setTimeout(() => {
notificationDiv.style.animation = 'hoverdown 1s';
}, 4000);
setTimeout(() => {
notificationDiv.remove();
}, 4900);
}
</script> </script>
@@ -160,6 +227,30 @@
<style> <style>
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@100..900&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@100..900&display=swap');
.input-wrapper{
z-index: 100;
display: none;
position: absolute;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
color: white;
backdrop-filter: blur(2.5px);
}
.input-wrapper-container{
z-index: 101;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 50%;
border-radius: 5px;
padding: 20px;
background-color: #181818;
}
body { body {
font-family: "Noto Sans Mono", "Consolas", "SF Mono", monospace; font-family: "Noto Sans Mono", "Consolas", "SF Mono", monospace;
font-weight: bold; font-weight: bold;
@@ -356,4 +447,58 @@
} }
} }
@keyframes hoverup {
0% {
opacity: 0;
transform: translateY(100%);
}
25% {
opacity: 1;
}
50% {
transform: translateY(-10px);
}
100% {
opacity: 1;
transform: translateY(0);
}
}
@keyframes hoverdown {
0% {
transform: translateY(0);
}
50% {
transform: translateY(-10px);
}
100% {
transform: translateY(200%);
}
}
.notification-div {
display: flex;
right: 0;
bottom: 0;
width: fit-content;
height: fit-content;
color: white;
background-color: #181818;
border-radius: 5px;
border: #222222 2px solid;
padding-left: 10px;
padding-right: 10px;
margin-right: 10px;
margin-bottom: 10px;
justify-content: center;
align-items: center;
}
</style> </style>
+83 -6
View File
@@ -17,7 +17,8 @@
<a href="dash" id="dash-btn" style="display: none;">dashboard</a> <a href="dash" id="dash-btn" style="display: none;">dashboard</a>
</div> </div>
<div class="nav-hamburger-menu"> <div class="nav-hamburger-menu">
<svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" fill="currentColor" class="bi bi-list" viewBox="0 0 16 16" id="hamburger-button" style="cursor: pointer;"> <svg xmlns="http://www.w3.org/2000/svg" width="56" height="56" fill="currentColor" class="bi bi-list"
viewBox="0 0 16 16" id="hamburger-button" style="cursor: pointer;">
<path fill-rule="evenodd" <path fill-rule="evenodd"
d="M2.5 12a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5m0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5m0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5" /> d="M2.5 12a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5m0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5m0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5" />
</svg> </svg>
@@ -33,7 +34,8 @@
<div class="container-wrapper"> <div class="container-wrapper">
<div class="paste-container"> <div class="paste-container">
<h1 id="paste-title" title="{pastetitleattribute}">{pastetitle}</h1> <h1 id="paste-title" title="{pastetitleattribute}">{pastetitle}</h1>
<h3 class="uploader-text"><a id="uploader-username" ></a> <a id="creationdate"></a> <a id="paste-size"></a></h3> <h3 class="uploader-text"><a id="uploader-username"></a> <a id="creationdate"></a> <a id="paste-size"></a>
</h3>
<div class="paste-wrapper"> <div class="paste-wrapper">
<p id="text"></p> <p id="text"></p>
</div> </div>
@@ -49,7 +51,8 @@
<div class="passwordDiv"> <div class="passwordDiv">
<div class="inputDiv"> <div class="inputDiv">
<h1>This paste is password protected</h1> <h1>This paste is password protected</h1>
<input type="password" id="password" class="textinput" name="password" maxlength="500" placeholder="password"> <input type="password" id="password" class="textinput" name="password" maxlength="500"
placeholder="password">
<button id="submitPassword" class="button-input">Submit</button> <button id="submitPassword" class="button-input">Submit</button>
</div> </div>
</div> </div>
@@ -57,6 +60,17 @@
<div class="notification-div" id="notification"> <div class="notification-div" id="notification">
<h2 id="notification-text"></h2> <h2 id="notification-text"></h2>
</div> </div>
<div class="input-wrapper" id="input-wrapper">
<div class="input-wrapper-container">
<h2>
Are you sure you want to delete this paste?
</h2>
<div>
<button id="yes-button" class="button">Yes</button>
<button id="no-button" class="button">No</button>
</div>
</div>
</div>
</body> </body>
<script> <script>
@@ -113,7 +127,7 @@
.then(response => response.json()) .then(response => response.json())
.then(obj => { .then(obj => {
const result = JSON.parse(obj); const result = JSON.parse(obj);
if (result.content != null){ if (result.content != null) {
const textElement = document.getElementById('text'); const textElement = document.getElementById('text');
textElement.innerText = result.content; textElement.innerText = result.content;
document.querySelector(".passwordDiv").style.display = "none"; document.querySelector(".passwordDiv").style.display = "none";
@@ -130,7 +144,7 @@
return date.toLocaleString(); return date.toLocaleString();
} }
document.addEventListener("DOMContentLoaded", function() { document.addEventListener("DOMContentLoaded", function () {
const creationdateElement = document.getElementById('creationdate'); const creationdateElement = document.getElementById('creationdate');
const creationdateText = creationdateElement.innerText.trim(); const creationdateText = creationdateElement.innerText.trim();
fetchInfo(); fetchInfo();
@@ -168,7 +182,35 @@
element.click(); element.click();
document.body.removeChild(element); document.body.removeChild(element);
}); });
{deletescript} document.getElementById("delete-button").addEventListener("click", function () {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.style.display = 'flex';
});
document.getElementById("yes-button").addEventListener("click", function () {
var requestUrl = "/api/pastes/delete";
fetch(requestUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
id: '{pasteid}'
})
})
.then(response => response.json())
.then(obj => {
const result = JSON.parse(obj);
if (result.message != null) {
window.location.href = `/`;
} else {
createNotification(`Error: ${result.error}`);
}
});
});
document.getElementById("no-button").addEventListener("click", function () {
const inputWrapper = document.getElementById('input-wrapper');
inputWrapper.style.display = 'none';
});
function notification(message) { function notification(message) {
const notification = document.getElementById('notification'); const notification = document.getElementById('notification');
const notificationText = document.getElementById('notification-text'); const notificationText = document.getElementById('notification-text');
@@ -193,6 +235,7 @@
</html> </html>
<style> <style>
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@100..900&display=swap'); @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@100..900&display=swap');
body { body {
font-family: "Noto Sans Mono", "Consolas", "SF Mono", monospace; font-family: "Noto Sans Mono", "Consolas", "SF Mono", monospace;
font-weight: bold; font-weight: bold;
@@ -203,6 +246,31 @@
background-color: #0d0d0d; background-color: #0d0d0d;
} }
.input-wrapper {
z-index: 100;
display: none;
position: absolute;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
color: white;
backdrop-filter: blur(2.5px);
}
.input-wrapper-container {
z-index: 101;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 50%;
border-radius: 5px;
padding: 20px;
background-color: #181818;
}
.container-wrapper { .container-wrapper {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@@ -211,6 +279,7 @@
width: 100%; width: 100%;
height: 98.7vh; height: 98.7vh;
} }
.button-input { .button-input {
transition: 300ms; transition: 300ms;
background-color: #222222; background-color: #222222;
@@ -228,11 +297,13 @@
font-weight: 625; font-weight: 625;
box-sizing: content-box; box-sizing: content-box;
} }
.button-input:hover { .button-input:hover {
transition: 300ms; transition: 300ms;
background-color: #333333; background-color: #333333;
border: 2px solid #3a3a3a; border: 2px solid #3a3a3a;
} }
.passwordDiv { .passwordDiv {
z-index: 100; z-index: 100;
position: absolute; position: absolute;
@@ -245,6 +316,7 @@
justify-content: center; justify-content: center;
align-items: center; align-items: center;
} }
.inputDiv { .inputDiv {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@@ -253,6 +325,7 @@
width: fit-content; width: fit-content;
color: white; color: white;
} }
.textinput { .textinput {
width: 100%; width: 100%;
height: 50px; height: 50px;
@@ -265,6 +338,7 @@
font-weight: 625; font-weight: 625;
color: white; color: white;
} }
.textinput:focus { .textinput:focus {
outline: none; outline: none;
border: 2px solid #333333; border: 2px solid #333333;
@@ -429,6 +503,7 @@
background-color: #333333; background-color: #333333;
border: #3a3a3a 2px solid; border: #3a3a3a 2px solid;
} }
@media screen and (max-width: 800px) { @media screen and (max-width: 800px) {
.nav-div { .nav-div {
display: none; display: none;
@@ -447,6 +522,7 @@
} }
} }
@keyframes hoverup { @keyframes hoverup {
0% { 0% {
opacity: 0; opacity: 0;
@@ -482,6 +558,7 @@
} }
} }
.notification-div { .notification-div {
position: fixed; position: fixed;
display: none; display: none;