Enhance user authentication and paste management

- Updated `AuthController.cs` to include token in `CreateSession` and improved validation for username and email.
- Refactored paste creation logic from `Program.cs` to a new `PasteController.cs`.
- Added email validation method in `HelperService.cs`.
- Introduced `CompressionService.cs` for handling string compression and decompression.
- Improved user experience with autocomplete and spellcheck in HTML forms.
- Enhanced date display in `archive.js` and refactored date formatting in `dash.js`.
- Updated API endpoints in `login.js` and `register.js` to reflect changes.
This commit is contained in:
whoswhip
2025-07-06 22:55:29 -04:00
parent cc0c6a8286
commit 3eb7374a07
16 changed files with 312 additions and 176 deletions
-1
View File
@@ -132,7 +132,6 @@ body {
overflow: hidden !important;
text-overflow: clip !important;
white-space: nowrap !important;
max-width: 80px;
text-align: left;
margin-right: -70px !important;
}
+3 -3
View File
@@ -18,8 +18,8 @@
<body>
<div class="main-container">
<form class="create-form" id="create-form">
<input type="text" name="title" id="title" placeholder="Title" maxlength="500">
<textarea name="content" id="content" placeholder="Content"></textarea>
<input type="text" name="title" id="title" placeholder="Title" maxlength="500" autocomplete="off" spellcheck="false">
<textarea name="content" id="content" placeholder="Content" autocomplete="off" spellcheck="false"></textarea>
<label id="syntax-label">Syntax Highlighting -</label>
<select name="syntax" id="syntax">
<option value="none">None</option>
@@ -52,7 +52,7 @@
<option value="1">Unlisted</option>
<option value="2">Private</option>
</select>
<input type="text" name="password" id="password" placeholder="Password" style="display: none;">
<input type="password" name="password" id="password" placeholder="Password" style="display: none;" autocomplete="off" spellcheck="false">
<button type="submit">Create Paste</button>
</form>
</div>
+25 -16
View File
@@ -49,7 +49,7 @@ async function renderArchive(page, limit) {
<li>
<a href="/${paste.id}">
<p id="archive-title" title="${paste.title}">${title}</p>
<p id="archive-date">${formatDate(paste.created)}</p>
<p id="archive-date" title="${formatDate(paste.created)}">${formatDate(paste.created, true)}</p>
<p id="archive-syntax">${paste.syntax || 'Plain Text'}</p>
</a>
</li>
@@ -88,25 +88,34 @@ async function getPastes(page, limit) {
return data;
}
function formatDate(unix) {
function getRelativeTime(date) {
const now = new Date();
const diff = now - date;
const seconds = Math.floor(diff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (isNaN(date.getTime())) return '';
if (seconds < 60) return 'just now';
if (minutes < 60) return `${minutes} min ago`;
if (hours < 24) return `${hours} hr${hours > 1 ? 's' : ''} ago`;
if (days < 7) return `${days} day${days > 1 ? 's' : ''} ago`;
return date.toLocaleDateString();
}
function formatDate(unix, relative = false) {
if (typeof unix !== 'number' || isNaN(unix)) {
return 'Invalid date';
}
const nowunix = Math.floor(Date.now() / 1000);
const diff = nowunix - unix;
if (diff < 60) {
return `${diff} second${diff !== 1 ? 's' : ''} ago`;
} else if (diff < 3600) {
const minutes = Math.floor(diff / 60);
return `${minutes} minute${minutes !== 1 ? 's' : ''} ago`;
} else if (diff < 86400) {
const hours = Math.floor(diff / 3600);
return `${hours} hour${hours !== 1 ? 's' : ''} ago`;
} else {
const days = Math.floor(diff / 86400);
return `${days} day${days !== 1 ? 's' : ''} ago`;
const date = new Date(unix * 1000);
if (isNaN(date.getTime())) {
return 'Invalid date';
}
if (relative) {
return getRelativeTime(date);
}
return date.toLocaleDateString([], { year: 'numeric', month: '2-digit', day: '2-digit' });
}
function nextPage() {
+29 -42
View File
@@ -1,4 +1,3 @@
let currentPage = 1;
let totalPages = 0;
let pastesPerPage = 10;
@@ -6,30 +5,30 @@ let userPastes = [];
document.addEventListener('DOMContentLoaded', function () {
fetchAccountInfo();
fetchUserPastes();
waitForAuthData().then(data => {
if (data.success === false) {
window.location.href = '/login';
} else {
updateAccountInfo(data.user);
fetchUserPastes();
}
}).catch(error => {
console.error('Error waiting for auth data:', error);
window.location.href = '/login';
});
});
async function fetchAccountInfo() {
try {
const response = await fetch('/api/auth/authorized');
const accountData = await response.json();
if (accountData.success === false) {
window.location.href = '/login';
}
updateAccountInfo(accountData.user);
} catch (error) {
console.error('Error fetching account information:', error);
if (error.message.includes('401')) {
window.location.href = '/login.html';
}
}
function waitForAuthData() {
return new Promise(resolve => {
const interval = setInterval(() => {
if (authData) {
clearInterval(interval);
resolve(authData);
}
}, 100);
});
}
function updateAccountInfo(data) {
document.getElementById('account-username').textContent = `Username: ${data.username}`;
document.getElementById('account-email').textContent = `Email: ${data.email || 'Not provided'}`;
@@ -49,31 +48,19 @@ function updateAccountInfo(data) {
}
function formatDate(date) {
function formatDate(date, minutes = false) {
if (isNaN(date.getTime())) {
return 'Never';
}
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
if (minutes)
{
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString([], { minute: '2-digit', hour: '2-digit' });
} else {
return date.toLocaleDateString([], { year: 'numeric', month: '2-digit', day: '2-digit' });
}
}
function getRelativeTime(date) {
const now = new Date();
const diff = now - date;
const seconds = Math.floor(diff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (isNaN(date.getTime())) return '';
if (seconds < 60) return 'just now';
if (minutes < 60) return `${minutes} min ago`;
if (hours < 24) return `${hours} hr${hours > 1 ? 's' : ''} ago`;
if (days < 7) return `${days} day${days > 1 ? 's' : ''} ago`;
return date.toLocaleDateString();
}
async function fetchUserPastes() {
try {
const response = await fetch('/api/pastes/my?page=0&limit=15');
@@ -154,7 +141,7 @@ function displayPastes() {
const dateP = document.createElement('p');
dateP.id = 'paste-date';
const createdDate = new Date(paste.created * 1000);
dateP.textContent = `${formatDate(createdDate)} (${getRelativeTime(createdDate)})`;
dateP.textContent = `${formatDate(createdDate)}`;
const syntaxP = document.createElement('p');
@@ -268,4 +255,4 @@ function deleteAccount() {
} catch { }
window.location.href = '/register';
};
}
}
+3
View File
@@ -35,6 +35,9 @@ document.getElementById("create-form").addEventListener("submit", async function
fetch(`/api/pastes/create?title=${title}&visibility=${visibility}&syntax=${syntax}`, {
method: "POST",
headers: {
'Content-Type': 'text/plain'
},
body: content
})
.then(response => response.json())
+1 -1
View File
@@ -74,7 +74,7 @@ document.addEventListener('DOMContentLoaded', function () {
async function checkAuthStatus() {
try {
const response = await fetch('/api/accounts/authorized');
const response = await fetch('/api/auth/authenticated');
const data = await response.json();
if (data.success) {
+4 -1
View File
@@ -171,6 +171,8 @@ const navbarHTML = `
</div>
`;
let authData = null;
document.addEventListener('DOMContentLoaded', async () => {
document.body.insertAdjacentHTML('afterbegin', navbarHTML);
var path = window.location.pathname;
@@ -178,8 +180,9 @@ document.addEventListener('DOMContentLoaded', async () => {
let accountHref = '/login';
try {
const resp = await fetch('/api/accounts/authorized');
const resp = await fetch('/api/auth/authenticated');
const data = await resp.json();
authData = data;
if (data && data.success !== false) {
accountHref = '/dash';
}
+1 -1
View File
@@ -96,7 +96,7 @@ document.addEventListener('DOMContentLoaded', function() {
async function checkAuthStatus() {
try {
const response = await fetch('/api/accounts/authorized');
const response = await fetch('/api/auth/authenticated');
const data = await response.json();
if (data.success) {
+2 -2
View File
@@ -22,11 +22,11 @@
<form id="login-form" class="auth-form">
<div class="form-group">
<label for="username">Username or Email</label>
<input type="text" id="username" name="username" placeholder="Enter your username or email" required>
<input type="text" id="username" name="username" placeholder="Enter your username or email" required autocomplete="off" spellcheck="false">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Enter your password" required>
<input type="password" id="password" name="password" placeholder="Enter your password" required autocomplete="off" spellcheck="false">
</div>
<button type="submit" class="auth-button">Login</button>
</form> <div class="auth-links">
+4 -4
View File
@@ -22,19 +22,19 @@
<form id="register-form" class="auth-form">
<div class="form-group">
<label for="username">Username</label>
<input type="text" id="username" name="username" placeholder="Choose a username" required>
<input type="text" id="username" name="username" placeholder="Choose a username" required autocomplete="off" spellcheck="false">
</div>
<div class="form-group">
<label for="email">Email (optional)</label>
<input type="email" id="email" name="email" placeholder="Enter your email">
<input type="email" id="email" name="email" placeholder="Enter your email" autocomplete="off" spellcheck="false">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Create a password" required>
<input type="password" id="password" name="password" placeholder="Create a password" required autocomplete="off" spellcheck="false">
</div>
<div class="form-group">
<label for="confirm-password">Confirm Password</label>
<input type="password" id="confirm-password" name="confirm-password" placeholder="Confirm your password" required>
<input type="password" id="confirm-password" name="confirm-password" placeholder="Confirm your password" required autocomplete="off" spellcheck="false">
</div>
<button type="submit" class="auth-button">Register</button>
</form>