Spaces:
Sleeping
Sleeping
Auth UI translated to English
Browse files12345678929 unmodified lines394041424344452 unmodified lines484950515253541 unmodified line565758596061622 unmodified lines65666768697071727374757677787980818283848586878889909192939495969798991001011021031041055 unmodified lines1111121131141151161171181191201211221231241251261271281291301311321335 unmodified lines1391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681 unmodified line170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203<!DOCTYPE html><html lang="pt-BR"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Planner Auth — Autenticação</title><style>:root { --accent: #0d9488; --danger: #e11d48; --border: #e2e8f0; --muted: #64748b; }* { box-sizing: border-box; }29 unmodified lines'use strict';const $ = (id) => document.getElementById(id);/* ---------- Chamadas à API (mesma origem: o cookie de sessão vai junto) ---------- */async function api(path, body) {const res = await fetch(path, {method: body ? 'POST' : 'GET',2 unmodified linescredentials: 'same-origin'});const data = await res.json().catch(() => ({}));if (!res.ok) throw new Error(data.error || 'Erro inesperado (' + res.status + ')');return data;}1 unmodified linefunction busy(btn, on) {if (on && btn.dataset.label === undefined) btn.dataset.label = btn.textContent;btn.disabled = on;btn.textContent = on ? 'Aguarde…' : (btn.dataset.label || '');}function showMsg(text, type = 'err') {const el = $('msg'); if (!el) return;2 unmodified linesif (type === 'ok') setTimeout(() => { el.className = 'msg'; }, 4000);}/* ---------- Tela: sessão ativa ---------- */function renderProfile(user) {$('card').innerHTML = `<div class="logo"><div class="logo-badge">🔐</div></div><h1>Você está autenticado</h1><p class="sub">Esta tela só é exibida com uma sessão válida no servidor.</p><div class="kv"><span>Nome</span><b>${esc(user.name)}</b></div><div class="kv"><span>E-mail</span><b>${esc(user.email)}</b></div><div class="kv"><span>Conta criada em</span><b>${new Date(user.createdAt).toLocaleString('pt-BR')}</b></div><a class="btn" href="/app">Abrir o Planner 🍃 →</a><button class="accent" id="logoutBtn">Sair (destrói a sessão)</button><div class="msg" id="msg"></div><p class="note">O cookie <code>planner.sid</code> é <b>httpOnly</b>: o JavaScript da página não consegue lê-lo, e quem valida a sessão é o servidor. O Planner em <code>/app</code> só abre com sessão válida — e cada usuário só vê os próprios dados, filtrados pelo servidor.</p>`;$('logoutBtn').onclick = async () => {busy($('logoutBtn'), true);try { await api('/api/logout', {}); } catch { /* sessão já morreu */ }renderLogin();};}/* ---------- Tela: login ---------- */function renderLogin() {$('card').innerHTML = `<div class="logo"><div class="logo-badge">🔐</div></div><h1>Entrar</h1><p class="sub">Autenticação no servidor, com bcrypt e sessão httpOnly</p><form id="f"><label>E-mail</label><input id="email" type="email" required autocomplete="email" placeholder="voce@email.com"><label>Senha</label><input id="pass" type="password" required autocomplete="current-password" placeholder="••••••••"><button type="submit" class="accent">Entrar</button></form><div class="msg" id="msg"></div><div class="links">Não tem conta? <a data-go="register">Cadastrar</a> · <a data-go="forgot">Esqueci a senha</a></div>`;$('f').onsubmit = async (e) => {e.preventDefault();const btn = $('f').querySelector('button'); busy(btn, true);5 unmodified linesdocument.querySelectorAll('[data-go]').forEach(a => a.onclick = () => a.dataset.go === 'register' ? renderRegister() : renderForgot());}/* ---------- Tela: cadastro ---------- */function renderRegister() {$('card').innerHTML = `<div class="logo"><div class="logo-badge">🔐</div></div><h1>Criar conta</h1><p class="sub">A senha é transformada em hash bcrypt no servidor</p><form id="f"><label>Nome</label><input id="name" required minlength="2" maxlength="80" placeholder="Seu nome"><label>E-mail</label><input id="email" type="email" required autocomplete="email" placeholder="voce@email.com"><label>Senha (mínimo 8 caracteres)</label><input id="pass" type="password" required minlength="8" autocomplete="new-password" placeholder="••••••••"><button type="submit" class="accent">Cadastrar</button></form><div class="msg" id="msg"></div><div class="links">Já tem conta? <a data-go="login">Entrar</a></div>`;$('f').onsubmit = async (e) => {e.preventDefault();const btn = $('f').querySelector('button'); busy(btn, true);5 unmodified linesdocument.querySelectorAll('[data-go]').forEach(a => a.onclick = renderLogin);}/* ---------- Tela: esqueci a senha ---------- */function renderForgot() {$('card').innerHTML = `<div class="logo"><div class="logo-badge">🔑</div></div><h1>Esqueci a senha</h1><p class="sub">Gera um token de uso único, válido por 15 minutos</p><form id="f"><label>E-mail da conta</label><input id="email" type="email" required placeholder="voce@email.com"><button type="submit" class="accent">Gerar token</button></form><div class="msg" id="msg"></div><div id="tokenBox"></div><div class="links"><a data-go="login">Voltar ao login</a> · <a data-go="reset">Já tenho o token</a></div>`;$('f').onsubmit = async (e) => {e.preventDefault();const btn = $('f').querySelector('button'); busy(btn, true);try {const r = await api('/api/forgot-password', { email: $('email').value });busy(btn, false);showMsg('Se o e-mail estiver cadastrado, o token aparece abaixo. (Demo sem e-mail: em produção ele chegaria por e-mail.)', 'ok');if (r.token) {$('tokenBox').innerHTML = `<label>Seu token (demo)</label><div class="token">${esc(r.token)}</div><button id="goReset" class="accent">Redefinir senha com este token</button>`;$('goReset').onclick = () => renderReset(r.token);}} catch (err) { busy(btn, false); showMsg(err.message); }1 unmodified linedocument.querySelectorAll('[data-go]').forEach(a => a.onclick = () => a.dataset.go === 'login' ? renderLogin() : renderReset());}/* ---------- Tela: redefinir senha ---------- */function renderReset(presetToken) {$('card').innerHTML = `<div class="logo"><div class="logo-badge">🔑</div></div><h1>Redefinir senha</h1><p class="sub">Informe o token e a nova senha</p><form id="f"><label>Token</label><input id="token" required placeholder="cole o token aqui" value="${esc(presetToken || '')}"><label>Nova senha (mínimo 8 caracteres)</label><input id="pass" type="password" required minlength="8" autocomplete="new-password"><button type="submit" class="accent">Redefinir</button></form><div class="msg" id="msg"></div><div class="links"><a data-go="login">Voltar ao login</a></div>`;$('f').onsubmit = async (e) => {e.preventDefault();const btn = $('f').querySelector('button'); busy(btn, true);try {const r = await api('/api/reset-password', { token: $('token').value.trim(), password: $('pass').value });showMsg(r.message || 'Senha redefinida!', 'ok');setTimeout(renderLogin, 1500);} catch (err) { busy(btn, false); showMsg(err.message); }};document.querySelectorAll('[data-go]').forEach(a => a.onclick = renderLogin);}/* ---------- Init: /api/me decide a tela inicial ---------- */(async function init() {try {const { user } = await api('/api/me');12345678929 unmodified lines394041424344452 unmodified lines484950515253541 unmodified line565758596061622 unmodified lines65666768697071727374757677787980818283848586878889909192939495969798991001011021031041055 unmodified lines1111121131141151161171181191201211221231241251261271281291301311321335 unmodified lines1391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681 unmodified line170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Planner Auth — Authentication</title><style>:root { --accent: #0d9488; --danger: #e11d48; --border: #e2e8f0; --muted: #64748b; }* { box-sizing: border-box; }29 unmodified lines'use strict';const $ = (id) => document.getElementById(id);/* ---------- API calls (same origin: the session cookie is sent along) ---------- */async function api(path, body) {const res = await fetch(path, {method: body ? 'POST' : 'GET',2 unmodified linescredentials: 'same-origin'});const data = await res.json().catch(() => ({}));if (!res.ok) throw new Error(data.error || 'Unexpected error (' + res.status + ')');return data;}1 unmodified linefunction busy(btn, on) {if (on && btn.dataset.label === undefined) btn.dataset.label = btn.textContent;btn.disabled = on;btn.textContent = on ? 'Please wait…' : (btn.dataset.label || '');}function showMsg(text, type = 'err') {const el = $('msg'); if (!el) return;2 unmodified linesif (type === 'ok') setTimeout(() => { el.className = 'msg'; }, 4000);}/* ---------- Screen: active session ---------- */function renderProfile(user) {$('card').innerHTML = `<div class="logo"><div class="logo-badge">🔐</div></div><h1>You are authenticated</h1><p class="sub">This screen is only shown with a valid server-side session.</p><div class="kv"><span>Name</span><b>${esc(user.name)}</b></div><div class="kv"><span>E-mail</span><b>${esc(user.email)}</b></div><div class="kv"><span>Account created</span><b>${new Date(user.createdAt).toLocaleString('en-US')}</b></div><a class="btn" href="/app">Open the Planner 🍃 →</a><button class="accent" id="logoutBtn">Sign out (destroys the session)</button><div class="msg" id="msg"></div><p class="note">The <code>planner.sid</code> cookie is <b>httpOnly</b>: page JavaScript cannot read it, and the server is what validates the session. The planner at <code>/app</code> only opens with a valid session — and each user only ever sees their own data.</p>`;$('logoutBtn').onclick = async () => {busy($('logoutBtn'), true);try { await api('/api/logout', {}); } catch { /* session already gone */ }renderLogin();};}/* ---------- Screen: login ---------- */function renderLogin() {$('card').innerHTML = `<div class="logo"><div class="logo-badge">🔐</div></div><h1>Sign in</h1><p class="sub">Server-side authentication with bcrypt and an httpOnly session</p><form id="f"><label>E-mail</label><input id="email" type="email" required autocomplete="email" placeholder="you@email.com"><label>Password</label><input id="pass" type="password" required autocomplete="current-password" placeholder="••••••••"><button type="submit" class="accent">Sign in</button></form><div class="msg" id="msg"></div><div class="links">No account? <a data-go="register">Sign up</a> · <a data-go="forgot">Forgot password</a></div>`;$('f').onsubmit = async (e) => {e.preventDefault();const btn = $('f').querySelector('button'); busy(btn, true);5 unmodified linesdocument.querySelectorAll('[data-go]').forEach(a => a.onclick = () => a.dataset.go === 'register' ? renderRegister() : renderForgot());}/* ---------- Screen: registration ---------- */function renderRegister() {$('card').innerHTML = `<div class="logo"><div class="logo-badge">🔐</div></div><h1>Create account</h1><p class="sub">The password is turned into a bcrypt hash on the server</p><form id="f"><label>Name</label><input id="name" required minlength="2" maxlength="80" placeholder="Your name"><label>E-mail</label><input id="email" type="email" required autocomplete="email" placeholder="you@email.com"><label>Password (minimum 8 characters)</label><input id="pass" type="password" required minlength="8" autocomplete="new-password" placeholder="••••••••"><button type="submit" class="accent">Sign up</button></form><div class="msg" id="msg"></div><div class="links">Already have an account? <a data-go="login">Sign in</a></div>`;$('f').onsubmit = async (e) => {e.preventDefault();const btn = $('f').querySelector('button'); busy(btn, true);5 unmodified linesdocument.querySelectorAll('[data-go]').forEach(a => a.onclick = renderLogin);}/* ---------- Screen: forgot password ---------- */function renderForgot() {$('card').innerHTML = `<div class="logo"><div class="logo-badge">🔑</div></div><h1>Forgot password</h1><p class="sub">Generates a single-use token, valid for 15 minutes</p><form id="f"><label>Account e-mail</label><input id="email" type="email" required placeholder="you@email.com"><button type="submit" class="accent">Generate token</button></form><div class="msg" id="msg"></div><div id="tokenBox"></div><div class="links"><a data-go="login">Back to sign in</a> · <a data-go="reset">I already have a token</a></div>`;$('f').onsubmit = async (e) => {e.preventDefault();const btn = $('f').querySelector('button'); busy(btn, true);try {const r = await api('/api/forgot-password', { email: $('email').value });busy(btn, false);showMsg('If the e-mail is registered, the token appears below. (Demo without e-mail: in production it would arrive by e-mail.)', 'ok');if (r.token) {$('tokenBox').innerHTML = `<label>Your token (demo)</label><div class="token">${esc(r.token)}</div><button id="goReset" class="accent">Reset password with this token</button>`;$('goReset').onclick = () => renderReset(r.token);}} catch (err) { busy(btn, false); showMsg(err.message); }1 unmodified linedocument.querySelectorAll('[data-go]').forEach(a => a.onclick = () => a.dataset.go === 'login' ? renderLogin() : renderReset());}/* ---------- Screen: reset password ---------- */function renderReset(presetToken) {$('card').innerHTML = `<div class="logo"><div class="logo-badge">🔑</div></div><h1>Reset password</h1><p class="sub">Enter the token and the new password</p><form id="f"><label>Token</label><input id="token" required placeholder="paste the token here" value="${esc(presetToken || '')}"><label>New password (minimum 8 characters)</label><input id="pass" type="password" required minlength="8" autocomplete="new-password"><button type="submit" class="accent">Reset</button></form><div class="msg" id="msg"></div><div class="links"><a data-go="login">Back to sign in</a></div>`;$('f').onsubmit = async (e) => {e.preventDefault();const btn = $('f').querySelector('button'); busy(btn, true);try {const r = await api('/api/reset-password', { token: $('token').value.trim(), password: $('pass').value });showMsg(r.message || 'Password reset!', 'ok');setTimeout(renderLogin, 1500);} catch (err) { busy(btn, false); showMsg(err.message); }};document.querySelectorAll('[data-go]').forEach(a => a.onclick = renderLogin);}/* ---------- Init: /api/me decides the initial screen ---------- */(async function init() {try {const { user } = await api('/api/me');
public/index.html+49-49