mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
Add user authentication and dashboard features
- Implemented login and registration forms with validation. - Created dashboard layout to display user account information and pastes. - Added JavaScript functionality for fetching account data and pastes. - Styled authentication and dashboard pages with CSS. - Introduced modals for logout and account deletion confirmation. - Included error handling for user actions and API responses. ### Copilot generated commit message
This commit is contained in:
@@ -1,50 +1,74 @@
|
||||
pages = 0;
|
||||
let pages = 0;
|
||||
let currentPage = 0;
|
||||
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
var queries = new URLSearchParams(window.location.search);
|
||||
var page = queries.get("page");
|
||||
var limit = queries.get("limit");
|
||||
const queries = new URLSearchParams(window.location.search);
|
||||
let page = parseInt(queries.get("page"));
|
||||
let limit = parseInt(queries.get("limit"));
|
||||
|
||||
if (parseInt(page).isNaN) {
|
||||
if (isNaN(page)) {
|
||||
page = 0;
|
||||
}
|
||||
|
||||
var archiveList = document.getElementById("archives");
|
||||
var archiveListHTML = "";
|
||||
var pagesElement = document.getElementById("paste-pages");
|
||||
|
||||
if (page == null || page < 0) {
|
||||
if (page < 0) {
|
||||
page = 0;
|
||||
}
|
||||
if (limit == null || limit < 1 || limit > 25) {
|
||||
|
||||
if (isNaN(limit) || limit < 1 || limit > 25) {
|
||||
limit = 25;
|
||||
}
|
||||
|
||||
var archiveData = await getPastes(page, limit);
|
||||
currentPage = page;
|
||||
|
||||
var pastes = archiveData.pastes;
|
||||
pages = archiveData.pages;
|
||||
pagesElement.innerText = `${parseInt(page) + 1}/${pages + 1}`;
|
||||
const archiveList = document.getElementById("archives");
|
||||
const pagesElement = document.getElementById("paste-pages");
|
||||
const previousButton = document.getElementById("previous-button");
|
||||
const nextButton = document.getElementById("next-button");
|
||||
|
||||
try {
|
||||
archiveList.innerHTML = '<div class="loading-spinner"><div></div><div></div><div></div></div>';
|
||||
|
||||
const archiveData = await getPastes(page, limit);
|
||||
|
||||
const pastes = archiveData.pastes;
|
||||
pages = archiveData.pages;
|
||||
|
||||
pagesElement.innerText = `${parseInt(page) + 1}/${pages}`;
|
||||
|
||||
previousButton.disabled = page <= 0;
|
||||
nextButton.disabled = page + 1 >= pages;
|
||||
|
||||
if (pastes.length === 0) {
|
||||
archiveList.innerHTML = '<div class="no-pastes">No pastes found</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
pastes.forEach(paste => {
|
||||
var title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title;
|
||||
archiveListHTML += `
|
||||
<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-syntax">${paste.syntax}</p>
|
||||
</a>
|
||||
</li>
|
||||
`;
|
||||
});
|
||||
let archiveListHTML = "";
|
||||
pastes.forEach(paste => {
|
||||
const title = paste.title.length > 30 ? paste.title.substring(0, 30) + '...' : paste.title;
|
||||
archiveListHTML += `
|
||||
<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-syntax">${paste.syntax || 'Plain Text'}</p>
|
||||
</a>
|
||||
</li>
|
||||
`;
|
||||
});
|
||||
|
||||
archiveList.innerHTML = archiveListHTML;
|
||||
archiveList.innerHTML = archiveListHTML;
|
||||
} catch (error) {
|
||||
console.error('Error fetching pastes:', error);
|
||||
archiveList.innerHTML = '<div class="error-message">Failed to load pastes. Please try again later.</div>';
|
||||
}
|
||||
});
|
||||
|
||||
async function getPastes(page, limit) {
|
||||
const response = await fetch(`/api/pastes/archive?page=${page}&limit=${limit}`);
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
return data;
|
||||
}
|
||||
@@ -53,8 +77,8 @@ function formatDate(unix) {
|
||||
if (typeof unix !== 'number' || isNaN(unix)) {
|
||||
return 'Invalid date';
|
||||
}
|
||||
var nowunix = Math.floor(Date.now() / 1000);
|
||||
var diff = nowunix - unix;
|
||||
const nowunix = Math.floor(Date.now() / 1000);
|
||||
const diff = nowunix - unix;
|
||||
|
||||
if (diff < 60) {
|
||||
return `${diff} second${diff !== 1 ? 's' : ''} ago`;
|
||||
@@ -71,14 +95,15 @@ function formatDate(unix) {
|
||||
}
|
||||
|
||||
function nextPage() {
|
||||
var queries = new URLSearchParams(window.location.search);
|
||||
var page = queries.get("page");
|
||||
var limit = queries.get("limit");
|
||||
const queries = new URLSearchParams(window.location.search);
|
||||
let page = parseInt(queries.get("page"));
|
||||
let limit = parseInt(queries.get("limit"));
|
||||
|
||||
if (page == null || page < 0) {
|
||||
if (isNaN(page) || page < 0) {
|
||||
page = 0;
|
||||
}
|
||||
if (limit == null || limit < 1 || limit > 25) {
|
||||
|
||||
if (isNaN(limit) || limit < 1 || limit > 25) {
|
||||
limit = 25;
|
||||
}
|
||||
|
||||
@@ -91,21 +116,22 @@ function nextPage() {
|
||||
}
|
||||
|
||||
function previousPage() {
|
||||
var queries = new URLSearchParams(window.location.search);
|
||||
var page = queries.get("page");
|
||||
var limit = queries.get("limit");
|
||||
const queries = new URLSearchParams(window.location.search);
|
||||
let page = parseInt(queries.get("page"));
|
||||
let limit = parseInt(queries.get("limit"));
|
||||
|
||||
if (page == null || page < 0) {
|
||||
if (isNaN(page) || page < 0) {
|
||||
page = 0;
|
||||
}
|
||||
if (limit == null || limit < 1 || limit > 25) {
|
||||
|
||||
if (isNaN(limit) || limit < 1 || limit > 25) {
|
||||
limit = 25;
|
||||
}
|
||||
|
||||
if (page < pages) {
|
||||
if (page <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
page--;
|
||||
window.location.href = `/archive?page=${page}&limit=${limit}`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
|
||||
let currentPage = 1;
|
||||
let totalPages = 0;
|
||||
let pastesPerPage = 10;
|
||||
let userPastes = [];
|
||||
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
fetchAccountInfo();
|
||||
fetchUserPastes();
|
||||
});
|
||||
|
||||
|
||||
async function fetchAccountInfo() {
|
||||
try {
|
||||
const response = await fetch('/api/accounts/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 updateAccountInfo(data) {
|
||||
document.getElementById('account-username').textContent = `Username: ${data.username}`;
|
||||
document.getElementById('account-email').textContent = `Email: ${data.email || 'Not provided'}`;
|
||||
|
||||
|
||||
if (data.displayname) {
|
||||
const usernameElement = document.getElementById('account-header');
|
||||
usernameElement.textContent = data.displayname;
|
||||
}
|
||||
|
||||
|
||||
const createdDate = new Date(data.created * 1000);
|
||||
const lastLoginDate = new Date(data.lastLogin * 1000);
|
||||
|
||||
document.getElementById('account-joindate').textContent = `Joined: ${formatDate(createdDate)}`;
|
||||
document.getElementById('account-lastlogin').textContent = `Last Login: ${formatDate(lastLoginDate)}`;
|
||||
}
|
||||
|
||||
|
||||
function formatDate(date) {
|
||||
if (isNaN(date.getTime())) {
|
||||
return 'Never';
|
||||
}
|
||||
|
||||
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
|
||||
}
|
||||
|
||||
|
||||
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');
|
||||
const pastesData = await response.json();
|
||||
|
||||
if (pastesData.message === 'No pastes found.' || pastesData.message === 'Page out of range.') {
|
||||
userPastes = [];
|
||||
document.getElementById('account-pastes').textContent = `Pastes: 0`;
|
||||
totalPages = 0;
|
||||
document.getElementById('paste-pages').textContent = `0/0`;
|
||||
displayPastes();
|
||||
return;
|
||||
}
|
||||
|
||||
userPastes = pastesData.pastes;
|
||||
document.getElementById('account-pastes').textContent = `Pastes: ${userPastes.length}`;
|
||||
totalPages = Math.ceil(userPastes.length / pastesPerPage);
|
||||
document.getElementById('paste-pages').textContent = `${currentPage}/${totalPages || 1}`;
|
||||
displayPastes();
|
||||
} catch (error) {
|
||||
console.error('Error fetching user pastes:', error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function displayPastes() {
|
||||
const pastesList = document.getElementById('pastes');
|
||||
pastesList.innerHTML = '';
|
||||
|
||||
|
||||
const pasteInfoHeader = document.querySelector('.paste-information');
|
||||
const pasteActions = document.querySelector('.paste-actions');
|
||||
const prevButton = document.getElementById('previous-button');
|
||||
const nextButton = document.getElementById('next-button');
|
||||
|
||||
|
||||
if (userPastes.length === 0) {
|
||||
if (pasteInfoHeader) pasteInfoHeader.style.display = 'none';
|
||||
if (pasteActions) pasteActions.style.display = 'none';
|
||||
if (prevButton) prevButton.disabled = true;
|
||||
if (nextButton) nextButton.disabled = true;
|
||||
const noItems = document.createElement('li');
|
||||
noItems.textContent = 'No pastes found';
|
||||
pastesList.appendChild(noItems);
|
||||
return;
|
||||
} else {
|
||||
if (pasteInfoHeader) pasteInfoHeader.style.display = '';
|
||||
if (pasteActions) pasteActions.style.display = '';
|
||||
}
|
||||
|
||||
|
||||
if (prevButton) prevButton.disabled = currentPage <= 1;
|
||||
if (nextButton) nextButton.disabled = currentPage >= totalPages || totalPages <= 1;
|
||||
|
||||
|
||||
const startIndex = (currentPage - 1) * pastesPerPage;
|
||||
const endIndex = Math.min(startIndex + pastesPerPage, userPastes.length);
|
||||
|
||||
|
||||
for (let i = startIndex; i < endIndex; i++) {
|
||||
const paste = userPastes[i];
|
||||
const pasteItem = document.createElement('li');
|
||||
|
||||
|
||||
const pasteLink = document.createElement('a');
|
||||
pasteLink.href = `/paste/${paste.id}`;
|
||||
|
||||
pasteLink.style.display = 'grid';
|
||||
pasteLink.style.gridTemplateColumns = '2fr 1fr 1fr';
|
||||
pasteLink.style.gap = '1rem';
|
||||
|
||||
|
||||
const titleP = document.createElement('p');
|
||||
titleP.id = 'paste-title';
|
||||
titleP.textContent = paste.title || 'Untitled';
|
||||
|
||||
|
||||
const dateP = document.createElement('p');
|
||||
dateP.id = 'paste-date';
|
||||
const createdDate = new Date(paste.created * 1000);
|
||||
dateP.textContent = `${formatDate(createdDate)} (${getRelativeTime(createdDate)})`;
|
||||
|
||||
|
||||
const syntaxP = document.createElement('p');
|
||||
syntaxP.id = 'paste-syntax';
|
||||
syntaxP.textContent = paste.syntax || 'plaintext';
|
||||
|
||||
pasteLink.appendChild(titleP);
|
||||
pasteLink.appendChild(dateP);
|
||||
pasteLink.appendChild(syntaxP);
|
||||
pasteItem.appendChild(pasteLink);
|
||||
pastesList.appendChild(pasteItem);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function previousPage() {
|
||||
if (currentPage > 1) {
|
||||
currentPage--;
|
||||
document.getElementById('paste-pages').textContent = `${currentPage}/${totalPages || 1}`;
|
||||
displayPastes();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function nextPage() {
|
||||
if (currentPage < totalPages) {
|
||||
currentPage++;
|
||||
document.getElementById('paste-pages').textContent = `${currentPage}/${totalPages || 1}`;
|
||||
displayPastes();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function logout() {
|
||||
|
||||
if (document.querySelector('.logout-modal-overlay')) return;
|
||||
|
||||
const modalOverlay = document.createElement('div');
|
||||
modalOverlay.className = 'logout-modal-overlay';
|
||||
|
||||
const modalBox = document.createElement('div');
|
||||
modalBox.className = 'logout-modal-box';
|
||||
modalBox.innerHTML = `
|
||||
<h2>Confirm Logout</h2>
|
||||
<p>Are you sure you want to log out?</p>
|
||||
<button id="confirm-logout">Logout</button>
|
||||
<button id="cancel-logout">Cancel</button>
|
||||
`;
|
||||
modalOverlay.appendChild(modalBox);
|
||||
document.body.appendChild(modalOverlay);
|
||||
|
||||
document.getElementById('cancel-logout').onclick = function () {
|
||||
document.body.removeChild(modalOverlay);
|
||||
};
|
||||
|
||||
document.getElementById('confirm-logout').onclick = async function () {
|
||||
try {
|
||||
await fetch('/api/accounts/logout', { method: 'POST', credentials: 'include' });
|
||||
} catch { }
|
||||
window.location.href = '/login';
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function deleteAccount() {
|
||||
|
||||
if (document.querySelector('.delete-modal-overlay')) return;
|
||||
|
||||
const username = document.getElementById('account-username').textContent.replace('Username: ', '').trim();
|
||||
const pasteCount = userPastes.length;
|
||||
|
||||
const modalOverlay = document.createElement('div');
|
||||
modalOverlay.className = 'delete-modal-overlay';
|
||||
|
||||
const modalBox = document.createElement('div');
|
||||
modalBox.className = 'delete-modal-box';
|
||||
modalBox.innerHTML = `
|
||||
<h2>Delete Account</h2>
|
||||
<p>This action is <b>irreversible</b> and will delete your account and all your ${pasteCount} pastes.</p>
|
||||
<p>Type your username <b>${username}</b> and your paste count <b>${pasteCount}</b> to confirm:</p>
|
||||
<input id="delete-username" type="text" placeholder="Username" style="margin-bottom:8px;width:90%;padding:8px;border-radius:5px;border:1px solid var(--neutral-700);background:var(--neutral-800);color:var(--neutral-100);"><br>
|
||||
<input id="delete-pastecount" type="number" placeholder="Paste count" style="margin-bottom:16px;width:90%;padding:8px;border-radius:5px;border:1px solid var(--neutral-700);background:var(--neutral-800);color:var(--neutral-100);"><br>
|
||||
<button id="confirm-delete-account" disabled>Delete</button>
|
||||
<button id="cancel-delete-account">Cancel</button>
|
||||
<p id="delete-error" style="color:#e74c3c;margin-top:1rem;display:none;"></p>
|
||||
`;
|
||||
modalOverlay.appendChild(modalBox);
|
||||
document.body.appendChild(modalOverlay);
|
||||
|
||||
document.getElementById('cancel-delete-account').onclick = function () {
|
||||
document.body.removeChild(modalOverlay);
|
||||
};
|
||||
|
||||
const userInput = document.getElementById('delete-username');
|
||||
const countInput = document.getElementById('delete-pastecount');
|
||||
const confirmBtn = document.getElementById('confirm-delete-account');
|
||||
function validateInputs() {
|
||||
if (userInput.value.trim() === username && countInput.value.trim() === String(pasteCount)) {
|
||||
confirmBtn.disabled = false;
|
||||
} else {
|
||||
confirmBtn.disabled = true;
|
||||
}
|
||||
}
|
||||
userInput.addEventListener('input', validateInputs);
|
||||
countInput.addEventListener('input', validateInputs);
|
||||
|
||||
confirmBtn.onclick = async function () {
|
||||
if (confirmBtn.disabled) return;
|
||||
try {
|
||||
await fetch('/api/accounts/delete', { method: 'POST', credentials: 'include' });
|
||||
} catch { }
|
||||
window.location.href = '/register';
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
const loginForm = document.getElementById('login-form');
|
||||
const errorMessage = document.getElementById('error-message');
|
||||
|
||||
|
||||
checkAuthStatus();
|
||||
|
||||
loginForm.addEventListener('submit', async function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
|
||||
const username = document.getElementById('username').value.trim();
|
||||
const password = document.getElementById('password').value;
|
||||
|
||||
|
||||
if (!username || !password) {
|
||||
showError('Please fill in all required fields');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
const submitButton = loginForm.querySelector('button[type="submit"]');
|
||||
const originalButtonText = submitButton.textContent;
|
||||
submitButton.disabled = true;
|
||||
submitButton.textContent = 'Logging in...';
|
||||
|
||||
|
||||
const isEmail = username.includes('@');
|
||||
const requestData = isEmail
|
||||
? { email: username, password: password }
|
||||
: { username: username, password: password };
|
||||
|
||||
|
||||
const response = await fetch('/api/accounts/login', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(requestData)
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
|
||||
window.location.href = '/dash';
|
||||
} else {
|
||||
showError(data.message || 'Login failed. Please check your credentials.');
|
||||
submitButton.disabled = false;
|
||||
submitButton.textContent = originalButtonText;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Login error:', error);
|
||||
showError('An unexpected error occurred. Please try again later.');
|
||||
|
||||
const submitButton = loginForm.querySelector('button[type="submit"]');
|
||||
submitButton.disabled = false;
|
||||
submitButton.textContent = 'Login';
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function showError(message) {
|
||||
errorMessage.textContent = message;
|
||||
errorMessage.classList.add('visible');
|
||||
|
||||
|
||||
setTimeout(() => {
|
||||
errorMessage.classList.remove('visible');
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
|
||||
async function checkAuthStatus() {
|
||||
try {
|
||||
const response = await fetch('/api/accounts/authorized');
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
|
||||
window.location.href = '/dash';
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
console.log('Not logged in');
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -1,22 +1,214 @@
|
||||
const navbarHTML = `
|
||||
<div class="nav-bar">
|
||||
<button class="hamburger" onclick="toggleMenu()">☰</button>
|
||||
<div class="nav-links">
|
||||
<a href="/" class="nav-link">Home</a>
|
||||
<a href="/archive" class="nav-link">Paste Archive</a>
|
||||
<div class="nav-brand">
|
||||
<a href="/" class="brand-link">Sharpbin</a>
|
||||
</div>
|
||||
<div class="nav-center">
|
||||
<div class="nav-links">
|
||||
<a href="/archive" class="nav-link">Archives</a>
|
||||
<a href="/" class="nav-link home-link">Home</a>
|
||||
<a href="#" class="nav-link">Account</a>
|
||||
</div>
|
||||
</div>
|
||||
<button class="hamburger" onclick="toggleMenu()" aria-label="Toggle menu">
|
||||
<span class="hamburger-line"></span>
|
||||
<span class="hamburger-line"></span>
|
||||
<span class="hamburger-line"></span>
|
||||
</button>
|
||||
<style>
|
||||
.nav-bar {display: flex;justify-content: space-between;align-items: center;padding: 1rem 2rem;background-color: var(--neutral-900);color: #fff;position: fixed;width: 100%;top: 0;left: 0;right: 0;}.nav-link {color: #fff;text-decoration: none;font-size: 1.2rem;transition: color 0.3s;}.nav-link:hover {color: var(--neutral-300);}.nav-links .nav-link {width: auto;padding: 0 1rem;border-bottom: none;}.nav-links .nav-link:last-child {border-bottom: none;}.hamburger {display: none;background: none;border: none;font-size: 2rem;cursor: pointer;margin: 0 auto;}.nav-links {display: flex;flex-direction: row;position: static;background-color: transparent;padding: 0;max-height: none;transition: none;}.nav-links.show {display: flex;flex-direction: column;position: absolute;top: 100%;left: 0;right: 0;background-color: var(--neutral-900);padding: 1rem 2rem;max-height: 500px;transition: max-height 0.3s ease-out;overflow: hidden;}@media (max-width: 800px) {.nav-links {display: none;flex-direction: column;width: 100%;position: absolute;top: 100%;left: 0;right: 0;border-top: 1px solid var(--neutral-800);background-color: var(--neutral-900);padding: 1rem 2rem;max-height: 0;transition: max-height 0.3s ease-out;overflow: hidden;}.nav-link{width: 100% !important;padding: 1rem 0 !important;text-align: center;border-bottom: 1px solid var(--neutral-800) !important;}.nav-links.show {display: flex;max-height: 500px;flex-direction: column;align-items: center;}.hamburger {display: block;margin: 0 auto;}.nav-bar {justify-content: center;}}
|
||||
.nav-bar {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 0.75rem 2rem;
|
||||
background-color: var(--neutral-900);
|
||||
color: #fff;
|
||||
position: fixed;
|
||||
width: 100%;
|
||||
top: 0;
|
||||
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.nav-brand {
|
||||
flex: 0 0 auto;
|
||||
z-index: 15;
|
||||
}
|
||||
|
||||
.brand-link {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 700;
|
||||
color: #fff;
|
||||
text-decoration: none;
|
||||
letter-spacing: 0.5px;
|
||||
transition: transform 0.2s ease;
|
||||
}
|
||||
|
||||
.brand-link:hover {
|
||||
transform: scale(1.05);
|
||||
}
|
||||
|
||||
.nav-center {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
right: 0;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.nav-links {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.nav-link {
|
||||
color: rgba(255, 255, 255, 0.85);
|
||||
text-decoration: none;
|
||||
font-size: 1.1rem;
|
||||
padding: 0.6rem 1.2rem;
|
||||
border-radius: 6px;
|
||||
transition: all 0.3s ease;
|
||||
font-weight: 500;
|
||||
text-align: center;
|
||||
border: 1px solid rgba(255, 255, 255, 0.2);
|
||||
max-width: 125px;
|
||||
width: 125px;
|
||||
}
|
||||
|
||||
.nav-link:hover {
|
||||
color: #fff;
|
||||
background-color: rgba(255, 255, 255, 0.1);
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
#nav-bar-selected {
|
||||
color: #fff;
|
||||
background-color: rgba(255, 255, 255, 0.15);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.hamburger {
|
||||
display: none;
|
||||
flex-direction: column;
|
||||
justify-content: space-between;
|
||||
width: 30px;
|
||||
height: 21px;
|
||||
background: transparent;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
padding: 0;
|
||||
z-index: 15;
|
||||
}
|
||||
|
||||
.hamburger-line {
|
||||
display: block;
|
||||
height: 3px;
|
||||
width: 100%;
|
||||
border-radius: 10px;
|
||||
background: #fff;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
@media (max-width: 800px) {
|
||||
.nav-center {
|
||||
position: static;
|
||||
}
|
||||
|
||||
.nav-links {
|
||||
position: fixed;
|
||||
top: 60px;
|
||||
left: 0;
|
||||
right: 0;
|
||||
background-color: var(--neutral-900);
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
padding: 0;
|
||||
max-height: 0;
|
||||
overflow: hidden;
|
||||
transition: max-height 0.3s ease-out, padding 0.3s ease;
|
||||
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
.nav-links.show {
|
||||
max-height: 300px;
|
||||
padding: 1rem 0;
|
||||
}
|
||||
|
||||
.nav-link {
|
||||
width: 90%;
|
||||
text-align: center;
|
||||
padding: 1rem;
|
||||
border-bottom: 1px solid var(--neutral-800);
|
||||
}
|
||||
|
||||
.home-link {
|
||||
border: none;
|
||||
border-bottom: 1px solid var(--neutral-800);
|
||||
order: -1;
|
||||
}
|
||||
|
||||
.nav-link:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.hamburger {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.hamburger.active .hamburger-line:nth-child(1) {
|
||||
transform: translateY(9px) rotate(45deg);
|
||||
}
|
||||
|
||||
.hamburger.active .hamburger-line:nth-child(2) {
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
.hamburger.active .hamburger-line:nth-child(3) {
|
||||
transform: translateY(-9px) rotate(-45deg);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</div>
|
||||
`;
|
||||
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
document.addEventListener('DOMContentLoaded', async () => {
|
||||
document.body.insertAdjacentHTML('afterbegin', navbarHTML);
|
||||
var path = window.location.pathname;
|
||||
var links = document.querySelectorAll('.nav-link');
|
||||
|
||||
let accountHref = '/login';
|
||||
try {
|
||||
const resp = await fetch('/api/accounts/authorized');
|
||||
const data = await resp.json();
|
||||
if (data && data.success !== false) {
|
||||
accountHref = '/dash';
|
||||
}
|
||||
} catch {}
|
||||
|
||||
const accountLink = document.querySelector('.nav-link[href="#"]');
|
||||
if (accountLink) {
|
||||
accountLink.setAttribute('href', accountHref);
|
||||
}
|
||||
|
||||
links.forEach(link => {
|
||||
const href = link.getAttribute('href');
|
||||
if (href === path) {
|
||||
link.id = 'nav-bar-selected';
|
||||
}
|
||||
|
||||
if ((path === '/dash' || path === '/login' || path === '/register') && (href === '/dash' || href === '/login')) {
|
||||
link.id = 'nav-bar-selected';
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
window.toggleMenu = function () {
|
||||
const nav = document.querySelector('.nav-links');
|
||||
const hamburger = document.querySelector('.hamburger');
|
||||
nav.classList.toggle('show');
|
||||
};
|
||||
|
||||
hamburger.classList.toggle('active');
|
||||
};
|
||||
@@ -2,17 +2,19 @@ const visibility = [{ id: 0, name: "Public" }, { id: 1, name: "Unlisted" }, { id
|
||||
|
||||
document.addEventListener("DOMContentLoaded", async function () {
|
||||
const id = window.location.pathname.split("/")[1];
|
||||
const content = await (await fetch(`/api/pastes/${id}`)).text();
|
||||
const info = await (await fetch(`/api/pastes/${id}/info`)).json();
|
||||
const paste = info.paste;
|
||||
const contentPromise = fetch(`/api/pastes/${id}`).then(response => response.text());
|
||||
const infoPromise = fetch(`/api/pastes/${id}/info`).then(response => response.json());
|
||||
|
||||
const blur = document.createElement("div");
|
||||
blur.id = "blur";
|
||||
blur.dataset.decrypted = "false";
|
||||
document.body.appendChild(blur);
|
||||
const blur2 = document.getElementById("blur");
|
||||
|
||||
if (paste.visibility === 2) {
|
||||
const [content, info] = await Promise.all([contentPromise, infoPromise]);
|
||||
const paste = info.paste;
|
||||
|
||||
if (paste.visibility === 2) {
|
||||
const password = prompt("Enter the password");
|
||||
try {
|
||||
const decrypted = await decryptAES(content, password);
|
||||
@@ -21,53 +23,37 @@ document.addEventListener("DOMContentLoaded", async function () {
|
||||
} catch (error) {
|
||||
alert("Failed to decrypt, invalid password, or invalid data.");
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
blur2.dataset.decrypted = "true";
|
||||
addContent(content, paste.syntax);
|
||||
}
|
||||
document.title = `${paste.title} - Sharpbin`;
|
||||
addInfo(paste);
|
||||
|
||||
});
|
||||
|
||||
function addInfo(paste) {
|
||||
document.getElementById("paste-title").innerText = paste.title;
|
||||
document.getElementById("paste-date").innerText = convertUnixToLocal(paste.created);
|
||||
document.getElementById("paste-syntax").innerText = paste.syntax === "none" ? "" : paste.syntax || "";
|
||||
document.getElementById("info-throbber").style.display = "none";
|
||||
document.getElementById("copy-button").addEventListener("click", function () {
|
||||
navigator.clipboard.writeText(window.location.href);
|
||||
});
|
||||
document.getElementById("download-button").addEventListener("click", function () {
|
||||
if (paste.visibility === 2) {
|
||||
const content = document.getElementById("paste-content").innerText;
|
||||
const blob = new Blob([content], { type: "text/plain" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${paste.title}.txt`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
else {
|
||||
const blob = new Blob([document.getElementById("paste-content").innerText], { type: "text/plain" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${paste.title}.txt`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
const content = document.getElementById("paste-content").innerText;
|
||||
const blob = new Blob([content], { type: "text/plain" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${paste.title}.txt`;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
});
|
||||
document.getElementById("raw-button").addEventListener("click", function () {
|
||||
window.location.href = `/raw/${paste.id}`;
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function addContent(content, syntax) {
|
||||
@@ -80,11 +66,10 @@ function addContent(content, syntax) {
|
||||
code.id = "paste-content";
|
||||
code.classList.add("language-" + syntax === "plaintext" ? "none" : syntax);
|
||||
if (syntax !== "plaintext" && syntax !== "none" && syntax !== "" && syntax !== null) {
|
||||
console.log(syntax);
|
||||
hljs.highlightElement(code);
|
||||
}
|
||||
hljs.lineNumbersBlock(code);
|
||||
|
||||
document.getElementById("content-throbber").style.display = "none";
|
||||
}
|
||||
|
||||
async function decryptAES(content, password) {
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
const registerForm = document.getElementById('register-form');
|
||||
const errorMessage = document.getElementById('error-message');
|
||||
|
||||
|
||||
checkAuthStatus();
|
||||
|
||||
registerForm.addEventListener('submit', async function(event) {
|
||||
event.preventDefault();
|
||||
|
||||
|
||||
const username = document.getElementById('username').value.trim();
|
||||
const email = document.getElementById('email').value.trim();
|
||||
const password = document.getElementById('password').value;
|
||||
const confirmPassword = document.getElementById('confirm-password').value;
|
||||
|
||||
|
||||
if (!username || !password) {
|
||||
showError('Please fill in all required fields');
|
||||
return;
|
||||
}
|
||||
|
||||
if (password !== confirmPassword) {
|
||||
showError('Passwords do not match');
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.length < 6) {
|
||||
showError('Password must be at least 6 characters long');
|
||||
return;
|
||||
}
|
||||
|
||||
if (username.length < 3) {
|
||||
showError('Username must be at least 3 characters long');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
const submitButton = registerForm.querySelector('button[type="submit"]');
|
||||
const originalButtonText = submitButton.textContent;
|
||||
submitButton.disabled = true;
|
||||
submitButton.textContent = 'Creating account...';
|
||||
|
||||
|
||||
const requestData = {
|
||||
username: username,
|
||||
password: password
|
||||
};
|
||||
|
||||
|
||||
if (email) {
|
||||
requestData.email = email;
|
||||
}
|
||||
|
||||
|
||||
const response = await fetch('/api/accounts/register', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(requestData)
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
|
||||
window.location.href = '/dash';
|
||||
} else {
|
||||
showError(data.message || 'Registration failed. Please try again.');
|
||||
submitButton.disabled = false;
|
||||
submitButton.textContent = originalButtonText;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Registration error:', error);
|
||||
showError('An unexpected error occurred. Please try again later.');
|
||||
|
||||
const submitButton = registerForm.querySelector('button[type="submit"]');
|
||||
submitButton.disabled = false;
|
||||
submitButton.textContent = 'Register';
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function showError(message) {
|
||||
errorMessage.textContent = message;
|
||||
errorMessage.classList.add('visible');
|
||||
|
||||
|
||||
setTimeout(() => {
|
||||
errorMessage.classList.remove('visible');
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
|
||||
async function checkAuthStatus() {
|
||||
try {
|
||||
const response = await fetch('/api/accounts/authorized');
|
||||
const data = await response.json();
|
||||
|
||||
if (data.success) {
|
||||
|
||||
window.location.href = '/dash';
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
console.log('Not logged in');
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user