diff --git a/app/login/LoginClient.tsx b/app/login/LoginClient.tsx index edbd4123..25b6c239 100644 --- a/app/login/LoginClient.tsx +++ b/app/login/LoginClient.tsx @@ -1,6 +1,6 @@ "use client"; -import { useState, useEffect, useRef } from "react"; +import { useState, useEffect, useRef, type FormEvent } from "react"; import { useRouter } from "next/navigation"; import Heading2 from "@/components/Heading2"; import TextField from "@/components/TextField"; @@ -46,7 +46,8 @@ export default function LoginClient() { const identifierError = touchedIdentifier ? getIdentifierError(identifier) : ""; const passwordError = touchedPassword ? getPasswordError(password) : ""; - const handleLogin = async () => { + const handleLogin = async (event: FormEvent) => { + event.preventDefault(); if (loading) return; setTouchedIdentifier(true); @@ -141,9 +142,11 @@ export default function LoginClient() { /> -
+
setTouchedIdentifier(true)} @@ -158,6 +161,8 @@ export default function LoginClient() { setTouchedPassword(true)} @@ -187,12 +192,12 @@ export default function LoginClient() { @@ -206,7 +211,7 @@ export default function LoginClient() { Create an account

-
+ ); diff --git a/app/navbar-auth-state.test.ts b/app/navbar-auth-state.test.ts index d1354d85..be34693e 100644 --- a/app/navbar-auth-state.test.ts +++ b/app/navbar-auth-state.test.ts @@ -28,7 +28,7 @@ describe("navbar authentication state", () => { }); it.each([ - "components/NavBar.tsx", + "components/SignOutButton.tsx", "components/AccountLifecycleStatus.tsx", "app/settings/account/AccountSettingsClient.tsx", "app/account-deletion/confirm/AccountDeletionConfirmationClient.tsx", diff --git a/app/profile/page.tsx b/app/profile/page.tsx index e340c7d4..ea23b429 100644 --- a/app/profile/page.tsx +++ b/app/profile/page.tsx @@ -1,6 +1,7 @@ import Link from "next/link"; import { redirect } from "next/navigation"; import Image from "@/components/Image"; +import SignOutButton from "@/components/SignOutButton"; import StudyBuddyIcon, { type StudyBuddyIconName, } from "@/components/StudyBuddyIcon"; @@ -173,16 +174,21 @@ export default async function ProfilePage() {
-
-

- Account overview -

-

- The essentials for your current Study Buddy account. -

+
+
+

+ Account overview +

+

+ The essentials for your current Study Buddy account. +

+
+
+ +
diff --git a/components/NavBar.tsx b/components/NavBar.tsx index 88a61057..00cde210 100644 --- a/components/NavBar.tsx +++ b/components/NavBar.tsx @@ -10,8 +10,8 @@ import { usePathname, useRouter } from "next/navigation"; import StudyBuddyIcon, { type StudyBuddyIconName, } from "./StudyBuddyIcon"; -import { useAuthState } from "./AuthStateProvider"; import NotificationBell from "./NotificationBell"; +import SignOutButton from "./SignOutButton"; interface NavLink { label: string; @@ -48,10 +48,8 @@ export default function NavBar({ }: Readonly) { const pathname = usePathname(); const router = useRouter(); - const { setIsAuthenticated } = useAuthState(); const [loadingLogin, setLoadingLogin] = useState(false); const [loadingSignup, setLoadingSignup] = useState(false); - const [loadingLogOut, setLoadingLogOut] = useState(false); const linkBase = "text-gray-700 hover:text-primary-500 font-medium transition-colors content-center"; @@ -59,25 +57,6 @@ export default function NavBar({ const linkActive = "text-primary-500 font-semibold bg-accent-100 px-3 py-1 rounded-lg"; - const handleLogout = async () => { - setLoadingLogOut(true); - try { - const response = await fetch("/api/v1/logout", { - method: "POST", - cache: "no-store", - }); - if (!response.ok) { - setLoadingLogOut(false); - return; - } - setIsAuthenticated(false); - router.replace("/login"); - router.refresh(); - } catch { - setLoadingLogOut(false); - } - }; - const handleLogin = () => { setLoadingLogin(true); router.push(signInLink ?? "/login"); @@ -134,6 +113,11 @@ export default function NavBar({
{isAuthenticated && showNotifications && } + {!isAuthenticated && pathname === "/" && ( + + )}
{!isAuthenticated && ( <> @@ -159,19 +143,7 @@ export default function NavBar({ )} - {isAuthenticated && ( - <> - - - )} + {isAuthenticated && }
diff --git a/components/SignOutButton.tsx b/components/SignOutButton.tsx new file mode 100644 index 00000000..7ff9d7da --- /dev/null +++ b/components/SignOutButton.tsx @@ -0,0 +1,52 @@ +"use client"; + +import { useId, useState } from "react"; +import { useRouter } from "next/navigation"; +import { useAuthState } from "./AuthStateProvider"; +import Button from "./Button"; +import FormErrorMessage from "./FormErrorMessage"; + +export default function SignOutButton() { + const router = useRouter(); + const { setIsAuthenticated } = useAuthState(); + const errorId = useId(); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(""); + + async function handleSignOut() { + if (loading) return; + setLoading(true); + setError(""); + try { + const response = await fetch("/api/v1/logout", { + method: "POST", + cache: "no-store", + }); + if (!response.ok) throw new Error("Sign-out request failed."); + } catch { + setError("We couldn't log you out. Please try again."); + setLoading(false); + return; + } + + setIsAuthenticated(false); + router.replace("/login"); + router.refresh(); + } + + return ( +
+ + +
+ ); +} diff --git a/docs/WEBSITE_GUIDE.md b/docs/WEBSITE_GUIDE.md index d9a7be15..eceb2069 100644 --- a/docs/WEBSITE_GUIDE.md +++ b/docs/WEBSITE_GUIDE.md @@ -46,6 +46,7 @@ cleanup; a source regression test guards ownership. This is not site-wide certif - [NavBar](../components/NavBar.tsx) shows desktop product links at **1110px and wider**. - Below 1110px, authenticated learners use the six-tab [BottomNav](../components/BottomNav.tsx): Home, Materials, Exams, Progress, Chat and Profile. Do not restore the legacy mobile menu. +- The mobile homepage offers a direct **Log In** link. Signed-in learners can use **Log Out** in Profile on mobile or in the header on desktop. - The approved profile SVG remains in both navigation variants. Settings is reached through Profile, not an extra bottom-nav tab. - [NotificationBell](../components/NotificationBell.tsx) preserves the existing bell and opens the inbox on mobile and desktop. Unknown counts must not be presented as zero. - The mobile bar includes the device safe area; the shell reserves footer clearance. @@ -144,6 +145,8 @@ guaranteed grades or licensed content solely because a route or illustration exi Login/signup use inline errors, not browser `alert()` popups. Account journeys include email verification, age/guardian decisions, password reset, security alert/recovery, deletion confirmation, deactivation and deletion-pending views. +The login form submits through the same validation when the learner presses +Enter or activates the **Log In** button. Provide distinct checking, incomplete-link, working, success and failure states. Never include a real recovery URL or token in screenshots or public examples. diff --git a/tests/e2e/account-lifecycle.spec.ts b/tests/e2e/account-lifecycle.spec.ts index b841dfe0..1d9dc3ad 100644 --- a/tests/e2e/account-lifecycle.spec.ts +++ b/tests/e2e/account-lifecycle.spec.ts @@ -150,6 +150,41 @@ test("sign-up checks age, verifies email once and signs out across tabs", async expect(await getUserId(page)).toBe(userId); }); +test("a mobile learner can reach login, submit with Enter and sign out from Profile", async ({ browser }) => { + const mobile = await browser.newContext({ + baseURL: APP_ORIGIN, viewport: { width: 320, height: 720 }, isMobile: true, hasTouch: true, + }); + try { + const page = await mobile.newPage(); + await page.goto("/"); + await page.getByRole("banner").getByRole("link", { name: "Log In", exact: true }).tap(); + await expect(page).toHaveURL(/\/login$/); + + const email = page.getByLabel(/Email or Phone Number/); + const password = page.getByLabel(/^Password/); + await email.fill("invalid"); + await password.fill(fixtureUsers.learner.password); + await password.press("Enter"); + await expect(page.getByText("Enter a valid email or phone number")).toBeVisible(); + await expect(page).toHaveURL(/\/login$/); + + await email.fill(fixtureUsers.learner.email); + await password.press("Enter"); + await expect(page).toHaveURL(/\/dashboard$/); + + await page.getByRole("navigation", { name: "Primary mobile navigation" }) + .getByRole("link", { name: "Profile", exact: true }).tap(); + await expect(page).toHaveURL(/\/profile$/); + await page.getByRole("main").getByRole("button", { name: "Log Out", exact: true }).tap(); + await expect(page).toHaveURL(/\/login$/); + expect((await page.request.get("/api/v1/me")).status()).toBe(401); + await page.goto("/dashboard"); + await expect(page).toHaveURL(/\/unauthorized$/); + } finally { + await mobile.close(); + } +}); + for (const approval of [true, false]) { test(`a guardian can ${approval ? "approve learning without AI" : "deny the account"}`, async ({ page, browser, request }) => { const variant = approval ? "approval" : "denial";