Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions app/login/LoginClient.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<HTMLFormElement>) => {
event.preventDefault();
if (loading) return;

setTouchedIdentifier(true);
Expand Down Expand Up @@ -141,9 +142,11 @@ export default function LoginClient() {
/>
</div>

<div className="flex flex-col gap-4">
<form className="flex flex-col gap-4" method="post" onSubmit={handleLogin}>
<TextField
label="Email or Phone Number"
name="identifier"
autoComplete="username"
placeholder="Enter your email or phone number"
value={identifier}
onFocus={() => setTouchedIdentifier(true)}
Expand All @@ -158,6 +161,8 @@ export default function LoginClient() {
<TextField
label="Password"
type="password"
name="password"
autoComplete="current-password"
placeholder="Enter your password"
value={password}
onFocus={() => setTouchedPassword(true)}
Expand Down Expand Up @@ -187,12 +192,12 @@ export default function LoginClient() {
<FormErrorMessage id="login-form-error" message={formError} />

<Button
type="submit"
variant="primary"
size="lg"
className="w-full rounded-xl mt-1"
loading={loading}
disabled={isLoginDisabled}
onClick={handleLogin}
>
Log In
</Button>
Expand All @@ -206,7 +211,7 @@ export default function LoginClient() {
Create an account
</a>
</p>
</div>
</form>
</div>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion app/navbar-auth-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
26 changes: 16 additions & 10 deletions app/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -173,16 +174,21 @@ export default async function ProfilePage() {
</header>

<section aria-labelledby="account-overview-heading">
<div className="mb-4">
<h2
id="account-overview-heading"
className="text-xl font-bold text-gray-950"
>
Account overview
</h2>
<p className="mt-1 text-sm text-gray-600">
The essentials for your current Study Buddy account.
</p>
<div className="mb-4 flex flex-wrap items-start justify-between gap-3">
<div>
<h2
id="account-overview-heading"
className="text-xl font-bold text-gray-950"
>
Account overview
</h2>
<p className="mt-1 text-sm text-gray-600">
The essentials for your current Study Buddy account.
</p>
</div>
<div className="min-[1110px]:hidden">
<SignOutButton />
</div>
</div>
<div className="grid gap-3 sm:grid-cols-3">
<div className="rounded-2xl border border-gray-200 bg-white p-5 shadow-sm">
Expand Down
42 changes: 7 additions & 35 deletions components/NavBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,36 +48,15 @@ export default function NavBar({
}: Readonly<NavBarProps>) {
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";

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");
Expand Down Expand Up @@ -134,6 +113,11 @@ export default function NavBar({

<div className="flex shrink-0 items-center gap-3">
{isAuthenticated && showNotifications && <NotificationBell />}
{!isAuthenticated && pathname === "/" && (
<Button href={signInLink ?? "/login"} size="sm" className="min-h-11 min-[1110px]:hidden">
Log In
</Button>
)}
<div className="hidden min-[1110px]:flex items-center space-x-3">
{!isAuthenticated && (
<>
Expand All @@ -159,19 +143,7 @@ export default function NavBar({
</>
)}

{isAuthenticated && (
<>
<Button
variant="outline"
size="sm"
loading={loadingLogOut}
disabled={loadingLogOut}
onClick={handleLogout}
>
Log Out
</Button>
</>
)}
{isAuthenticated && <SignOutButton />}
</div>

</div>
Expand Down
52 changes: 52 additions & 0 deletions components/SignOutButton.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<div className="grid gap-2">
<Button
variant="outline"
size="sm"
className="max-[1109px]:min-h-11"
loading={loading}
onClick={handleSignOut}
ariaDescribedBy={error ? errorId : undefined}
>
Log Out
</Button>
<FormErrorMessage id={errorId} message={error} />
</div>
);
}
3 changes: 3 additions & 0 deletions docs/WEBSITE_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.

Expand Down
35 changes: 35 additions & 0 deletions tests/e2e/account-lifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading