From b857c644f71788d6c61f49719a3b56240d61bbf2 Mon Sep 17 00:00:00 2001 From: Lheorvine Date: Mon, 26 May 2025 19:24:45 +0200 Subject: [PATCH] polish #4 --- src/main.rs | 35 +++++++++++++--- static/book.html | 54 ++++++++++++------------ static/cart.html | 34 +++++++++++++++ static/css/styles.css | 69 ++++++++++++++++++++++++++++++ static/index.html | 62 +++++++++++++-------------- static/js/main.js | 97 ++++++++++++++++++++++++++----------------- static/login.html | 54 ++++++++++++------------ static/profile.html | 55 ++++++++++++------------ static/register.html | 54 ++++++++++++------------ static/thankyou.html | 34 +++++++++++++++ 10 files changed, 364 insertions(+), 184 deletions(-) diff --git a/src/main.rs b/src/main.rs index e35a8e3..0543bca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -253,16 +253,41 @@ async fn get_ksiazka( } } +#[derive(Serialize)] +struct UserInfo { + id: i32, + imie: String, +} + #[get("/api/check-auth")] -async fn check_auth(req: HttpRequest) -> impl Responder { +async fn check_auth( + req: HttpRequest, + pool: web::Data, // Dodajemy pool jako parametr +) -> impl Responder { let token = req.headers().get("Authorization") .and_then(|h| h.to_str().ok()); match validate_token(token).await { - Ok(user_id) => HttpResponse::Ok().json(json!({ - "authenticated": true, - "user": {"id": user_id} - })), + Ok(user_id) => { + match sqlx::query!( + "SELECT imie FROM uzytkownicy WHERE id = $1", + user_id + ) + .fetch_one(pool.get_ref()) // Używamy pool z parametru + .await { + Ok(u) => HttpResponse::Ok().json(json!({ + "authenticated": true, + "user": { + "id": user_id, + "imie": u.imie + } + })), + Err(_) => HttpResponse::Ok().json(json!({ + "authenticated": false, + "user": null + })) + } + }, Err(_) => HttpResponse::Ok().json(json!({ "authenticated": false, "user": null diff --git a/static/book.html b/static/book.html index 8c567a9..06ca840 100644 --- a/static/book.html +++ b/static/book.html @@ -8,39 +8,39 @@ -
diff --git a/static/cart.html b/static/cart.html index f71fcfd..e3c150c 100644 --- a/static/cart.html +++ b/static/cart.html @@ -12,6 +12,40 @@ + +

Twój koszyk

diff --git a/static/css/styles.css b/static/css/styles.css index 56f595a..0cef4e9 100644 --- a/static/css/styles.css +++ b/static/css/styles.css @@ -255,3 +255,72 @@ footer a { .order-item:last-child { margin-bottom: 0; } + +.hidden { + display: none !important; +} + +/* Pokazywanie elementów */ +.visible { + display: flex !important; +} + +.auth-links { + display: flex; + gap: 1rem; +} + +.anonymous-links, +.user-links { + display: none; +} + +.anonymous-links.visible, +.user-links.visible { + display: flex !important; + gap: 1rem; +} + +.navbar-brand { + font-size: 1.8rem; + position: absolute; + left: 50%; + transform: translateX(-50%); +} + +@media (max-width: 991px) { + .navbar-brand { + position: static; + transform: none; + order: 0 !important; + margin: 0.5rem 0; + } + + .navbar-toggler { + order: 1; + } + + #searchForm { + order: 2; + width: 100%; + margin-top: 1rem; + } + + .auth-links { + order: 3; + width: 100%; + justify-content: center; + margin-top: 1rem; + } +} + +/* Responsywność formularza wyszukiwania */ +@media (min-width: 992px) { + #searchForm { + max-width: 400px; + } + + .navbar-brand { + position: absolute; + } +} diff --git a/static/index.html b/static/index.html index 54019aa..605308f 100644 --- a/static/index.html +++ b/static/index.html @@ -11,41 +11,39 @@ -

NOWOŚCI

diff --git a/static/js/main.js b/static/js/main.js index 891567d..91dfebc 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -51,6 +51,54 @@ document.addEventListener('DOMContentLoaded', loadBooks); })(); +async function updateAuthUI() { + const token = localStorage.getItem('token'); + const authContainers = document.querySelectorAll('.auth-links'); + + try { + const response = await fetch('/api/check-auth', { + headers: token ? { 'Authorization': `Bearer ${token}` } : {} + }); + + const data = await response.json(); + + authContainers.forEach(container => { + const anonymous = container.querySelector('.anonymous-links'); + const user = container.querySelector('.user-links'); + const cart = container.querySelector('a[href="/cart.html"]'); + + if (data.authenticated) { + anonymous?.classList.remove('visible'); + user?.classList.add('visible'); + cart?.classList.add('visible'); + } else { + anonymous?.classList.add('visible'); + user?.classList.remove('visible'); + cart?.classList.remove('visible'); + localStorage.removeItem('token'); + } + }); + } catch (error) { + console.error('Błąd autentykacji:', error); + } +} + +// Obsługa wylogowania +function setupLogout() { + document.getElementById('logoutLink')?.addEventListener('click', async (e) => { + e.preventDefault(); + localStorage.removeItem('token'); + await updateAuthUI(); + window.location.href = '/'; + }); +} + +// Inicjalizacja na każdej stronie +document.addEventListener('DOMContentLoaded', () => { + updateAuthUI(); + setupLogout(); +}); + document.addEventListener('DOMContentLoaded', () => { updateNavVisibility(); checkAuthStatus(); @@ -94,30 +142,27 @@ document.getElementById('logoutLink')?.addEventListener('click', (e) => { document.getElementById('loginForm')?.addEventListener('submit', async (e) => { e.preventDefault(); - const email = document.getElementById('loginEmail').value; - const password = document.getElementById('loginPassword').value; - + try { const response = await fetch('/login', { method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ email, haslo: password }), + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + email: document.getElementById('loginEmail').value, + haslo: document.getElementById('loginPassword').value + }) }); - const data = await response.json(); if (response.ok) { - localStorage.setItem('token', data.token); - localStorage.setItem('imie', data.imie); - updateNavVisibility(); + const { token } = await response.json(); + localStorage.setItem('token', token); + await updateAuthUI(); window.location.href = '/'; } else { - alert(data.message || 'Logowanie nieudane'); + alert('Błąd logowania!'); } } catch (error) { - console.error('Błąd logowania:', error); - alert('Wystąpił błąd podczas logowania'); + console.error('Błąd:', error); } }); @@ -276,29 +321,5 @@ function getAuthHeaders() { }; } -async function updateNavbar() { - try { - const response = await fetch('/api/check-auth', { - headers: getAuthHeaders() - }); - - const authInfo = await response.json(); - const userLinks = document.querySelector('.user-links'); - const anonLinks = document.querySelector('.anonymous-links'); - - if (authInfo.authenticated) { - userLinks.style.display = 'flex'; - anonLinks.style.display = 'none'; - } else { - userLinks.style.display = 'none'; - anonLinks.style.display = 'flex'; - } - } catch (error) { - console.error('Error checking auth:', error); - } -} - -// Wywołaj przy każdym załadowaniu strony -document.addEventListener('DOMContentLoaded', updateNavbar); localStorage.setItem('token', response.token); diff --git a/static/login.html b/static/login.html index 5fe2bb6..ee88462 100644 --- a/static/login.html +++ b/static/login.html @@ -10,39 +10,39 @@ -

LOGOWANIE

diff --git a/static/profile.html b/static/profile.html index bf83cc4..f9a8d30 100644 --- a/static/profile.html +++ b/static/profile.html @@ -11,41 +11,40 @@ - +

Twój profil

diff --git a/static/register.html b/static/register.html index 74b36de..b5b1d86 100644 --- a/static/register.html +++ b/static/register.html @@ -9,39 +9,39 @@ -

REJESTRACJA

diff --git a/static/thankyou.html b/static/thankyou.html index 4b0aa5b..80ede27 100644 --- a/static/thankyou.html +++ b/static/thankyou.html @@ -7,6 +7,40 @@ + +

Dziękujemy za zakup!

Twoje zamówienie zostało pomyślnie zrealizowane.