Add initial project files
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
59
frontend/js/api.js
Normal file
59
frontend/js/api.js
Normal file
@@ -0,0 +1,59 @@
|
||||
/* Central API client — all fetch calls go through here */
|
||||
|
||||
const API = (() => {
|
||||
const base = '/api';
|
||||
|
||||
function token() {
|
||||
return localStorage.getItem('bb_token');
|
||||
}
|
||||
|
||||
async function request(method, path, body) {
|
||||
const headers = { 'Content-Type': 'application/json' };
|
||||
const tok = token();
|
||||
if (tok) headers['Authorization'] = `Bearer ${tok}`;
|
||||
|
||||
const res = await fetch(base + path, {
|
||||
method,
|
||||
headers,
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||
});
|
||||
|
||||
if (res.status === 204) return null;
|
||||
|
||||
const data = await res.json().catch(() => null);
|
||||
|
||||
if (!res.ok) {
|
||||
const msg = data?.detail || `HTTP ${res.status}`;
|
||||
throw new Error(Array.isArray(msg) ? msg.map(e => e.msg).join(', ') : msg);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
return {
|
||||
get: (path) => request('GET', path),
|
||||
post: (path, body) => request('POST', path, body),
|
||||
put: (path, body) => request('PUT', path, body),
|
||||
delete: (path) => request('DELETE', path),
|
||||
|
||||
auth: {
|
||||
login: (email, password) => request('POST', '/auth/login', { email, password }),
|
||||
register: (email, password, display_name) =>
|
||||
request('POST', '/auth/register', { email, password, display_name }),
|
||||
},
|
||||
users: {
|
||||
me: () => request('GET', '/users/me'),
|
||||
update: (body) => request('PUT', '/users/me', body),
|
||||
changePassword: (body) => request('PUT', '/users/me/password', body),
|
||||
},
|
||||
entries: {
|
||||
list: () => request('GET', '/entries'),
|
||||
stats: () => request('GET', '/entries/stats'),
|
||||
create: (body) => request('POST', '/entries', body),
|
||||
delete: (id) => request('DELETE', `/entries/${id}`),
|
||||
},
|
||||
public: {
|
||||
stats: () => request('GET', '/public/stats'),
|
||||
},
|
||||
};
|
||||
})();
|
||||
72
frontend/js/auth.js
Normal file
72
frontend/js/auth.js
Normal file
@@ -0,0 +1,72 @@
|
||||
/* Auth state helpers shared across all pages */
|
||||
|
||||
const Auth = (() => {
|
||||
const KEY = 'bb_token';
|
||||
const USER_KEY = 'bb_user';
|
||||
|
||||
function getToken() { return localStorage.getItem(KEY); }
|
||||
|
||||
function saveToken(token) { localStorage.setItem(KEY, token); }
|
||||
|
||||
function getUser() {
|
||||
const raw = localStorage.getItem(USER_KEY);
|
||||
return raw ? JSON.parse(raw) : null;
|
||||
}
|
||||
|
||||
function saveUser(user) { localStorage.setItem(USER_KEY, JSON.stringify(user)); }
|
||||
|
||||
function logout() {
|
||||
localStorage.removeItem(KEY);
|
||||
localStorage.removeItem(USER_KEY);
|
||||
window.location.href = '/index.html';
|
||||
}
|
||||
|
||||
function isLoggedIn() { return !!getToken(); }
|
||||
|
||||
/* Redirect to login if not authenticated */
|
||||
function requireAuth() {
|
||||
if (!isLoggedIn()) {
|
||||
window.location.href = '/login.html';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Redirect away from auth pages if already logged in */
|
||||
function redirectIfLoggedIn() {
|
||||
if (isLoggedIn()) {
|
||||
window.location.href = '/dashboard.html';
|
||||
}
|
||||
}
|
||||
|
||||
/* Render the nav user area; call after DOM ready */
|
||||
async function renderNav(activePage) {
|
||||
const navLinksEl = document.getElementById('nav-links');
|
||||
const navUserEl = document.getElementById('nav-user');
|
||||
if (!navLinksEl || !navUserEl) return;
|
||||
|
||||
if (isLoggedIn()) {
|
||||
let user = getUser();
|
||||
if (!user) {
|
||||
try { user = await API.users.me(); saveUser(user); } catch (_) {}
|
||||
}
|
||||
navLinksEl.innerHTML = `
|
||||
<a href="/dashboard.html" class="${activePage === 'dashboard' ? 'active' : ''}">My Bottle</a>
|
||||
<a href="/log.html" class="${activePage === 'log' ? 'active' : ''}">Log Entry</a>
|
||||
`;
|
||||
navUserEl.innerHTML = `
|
||||
<a href="/profile.html" class="nav-user">${user?.display_name || user?.email || 'Account'}</a>
|
||||
<a href="#" class="btn btn-ghost btn-sm" id="logout-btn">Logout</a>
|
||||
`;
|
||||
document.getElementById('logout-btn')?.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
logout();
|
||||
});
|
||||
} else {
|
||||
navLinksEl.innerHTML = '';
|
||||
navUserEl.innerHTML = `<a href="/login.html" class="btn btn-primary btn-sm">Login</a>`;
|
||||
}
|
||||
}
|
||||
|
||||
return { getToken, saveToken, getUser, saveUser, logout, isLoggedIn, requireAuth, redirectIfLoggedIn, renderNav };
|
||||
})();
|
||||
Reference in New Issue
Block a user