mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
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:
@@ -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() {
|
||||
|
||||
@@ -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';
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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';
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user