-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat]: Email verification result page between verify and login redirect #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
541d658
feat(auth): add /login/verified success page for email verification
FireTable b36bba7
chore: gitignore .playwright-screenshots
FireTable 711937d
fix(auth): drop verified-page token guard and adopt session helper
FireTable 8523375
refactor(auth): remove dead hasSession branch and update verify-email…
FireTable File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { redirect } from "next/navigation"; | ||
|
|
||
| import { getSessionFromHeaders } from "@/lib/auth/queries"; | ||
|
|
||
| import { VerifiedView } from "./verified-view"; | ||
|
|
||
| // Better Auth's email verification lands here via callbackURL=/login/verified | ||
| // (rewritten from the default "/" in lib/auth/config.ts). The token is | ||
| // consumed server-side by Better Auth and never forwarded in the 302, so | ||
| // `searchParams` has no verification signal we can trust — the only reliable | ||
| // signal is `session` (autoSignInAfterVerification is on, see | ||
| // lib/auth/config.ts). Anyone landing here without a session is a random | ||
| // visitor / bookmark, not a verified user, and gets redirected to /login. | ||
| export default async function VerifiedPage() { | ||
| const session = await getSessionFromHeaders(); | ||
|
|
||
| if (!session) { | ||
| redirect("/login"); | ||
| } | ||
|
|
||
| return <VerifiedView />; | ||
|
FireTable marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| "use client"; | ||
|
|
||
| import { CircleCheckIcon, MessagesSquareIcon } from "lucide-react"; | ||
| import Link from "next/link"; | ||
| import { useRouter } from "next/navigation"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| import { Button } from "@/components/ui/button"; | ||
| import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; | ||
| import { FieldDescription } from "@/components/ui/field"; | ||
|
|
||
| const REDIRECT_SECONDS = 5; | ||
|
|
||
| // The server page at app/login/verified/page.tsx redirects to /login | ||
| // whenever there's no session, so by the time this component renders the | ||
| // user is verified and signed in. No `hasSession` prop needed — the page | ||
| // is the gate. | ||
| export function VerifiedView() { | ||
| const router = useRouter(); | ||
| const [remaining, setRemaining] = useState(REDIRECT_SECONDS); | ||
|
|
||
| useEffect(() => { | ||
|
FireTable marked this conversation as resolved.
|
||
| if (remaining <= 0) { | ||
| router.replace("/chat"); | ||
| return; | ||
| } | ||
| const t = setTimeout(() => setRemaining((s) => s - 1), 1000); | ||
| return () => clearTimeout(t); | ||
| }, [remaining, router]); | ||
|
|
||
| return ( | ||
| <div className="bg-muted/30 flex min-h-dvh items-center justify-center p-4"> | ||
| <Card className="w-full max-w-sm"> | ||
| <CardHeader className="flex flex-col items-center gap-3 text-center"> | ||
| <CircleCheckIcon className="size-10 text-green-600" aria-hidden /> | ||
| <CardTitle className="text-xl font-semibold">Email verified</CardTitle> | ||
| </CardHeader> | ||
|
|
||
| <CardContent> | ||
| <div className="flex flex-col gap-4"> | ||
| <FieldDescription className="text-center"> | ||
| Your email is confirmed. You're signed in. | ||
| </FieldDescription> | ||
|
|
||
| <FieldDescription className="text-center" aria-live="polite"> | ||
| {remaining > 0 ? `Redirecting in ${remaining}s…` : "Redirecting…"} | ||
| </FieldDescription> | ||
|
|
||
| <Button asChild size="lg" className="w-full"> | ||
| <Link href="/chat" replace> | ||
| <MessagesSquareIcon /> | ||
| Chat Now | ||
| </Link> | ||
| </Button> | ||
| </div> | ||
| </CardContent> | ||
| </Card> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { verificationRedirectUrl } from "@/lib/auth/config"; | ||
|
|
||
| // Better Auth constructs the verification link as | ||
|
FireTable marked this conversation as resolved.
|
||
| // `${baseURL}/api/auth/verify-email?token=...&callbackURL=...` (the | ||
| // `/api/auth` prefix is Better Auth's default `basePath`; the catch-all | ||
| // route at `app/api/auth/[...all]/route.ts` mounts the handler there). | ||
| // After the user clicks the link, Better Auth consumes the token at that | ||
| // endpoint and 302s to the callbackURL. Default callbackURL is `/` — | ||
| // silently bounces the user. We only want to swap the callbackURL value; | ||
| // the path must stay /api/auth/verify-email or the token never gets | ||
| // consumed and verification never happens. | ||
| describe("verificationRedirectUrl", () => { | ||
| it("rewrites callbackURL=/ to /login/verified", () => { | ||
| const out = verificationRedirectUrl( | ||
| "http://localhost:3000/api/auth/verify-email?token=abc&callbackURL=%2F", | ||
| ); | ||
| const u = new URL(out); | ||
| expect(u.searchParams.get("callbackURL")).toBe("/login/verified"); | ||
| }); | ||
|
|
||
| it("rewrites callbackURL=/login/sign-in to /login/verified", () => { | ||
| const out = verificationRedirectUrl( | ||
| "http://localhost:3000/api/auth/verify-email?token=abc&callbackURL=%2Flogin%2Fsign-in", | ||
| ); | ||
| const u = new URL(out); | ||
| expect(u.searchParams.get("callbackURL")).toBe("/login/verified"); | ||
| }); | ||
|
|
||
| it("adds callbackURL when missing", () => { | ||
| const out = verificationRedirectUrl("http://localhost:3000/api/auth/verify-email?token=abc"); | ||
| const u = new URL(out); | ||
| expect(u.searchParams.get("callbackURL")).toBe("/login/verified"); | ||
| }); | ||
|
|
||
| it("preserves the verification endpoint path", () => { | ||
| const out = verificationRedirectUrl( | ||
| "http://localhost:3000/api/auth/verify-email?token=abc&callbackURL=%2F", | ||
| ); | ||
| const u = new URL(out); | ||
| // CRITICAL: replacing the path would skip verification — the token | ||
| // would never be consumed and the user would land on a success page | ||
| // for an unverified email. | ||
| expect(u.pathname).toBe("/api/auth/verify-email"); | ||
| }); | ||
|
|
||
| it("preserves the token query param", () => { | ||
| const out = verificationRedirectUrl( | ||
| "http://localhost:3000/api/auth/verify-email?token=long-token-string&callbackURL=%2F", | ||
| ); | ||
| const u = new URL(out); | ||
| expect(u.searchParams.get("token")).toBe("long-token-string"); | ||
| }); | ||
|
|
||
| it("URL-encodes the new callbackURL value", () => { | ||
| const out = verificationRedirectUrl( | ||
| "http://localhost:3000/api/auth/verify-email?token=abc&callbackURL=%2F", | ||
| ); | ||
| // /login/verified → %2Flogin%2Fverified in the raw query string. | ||
| expect(out).toContain("callbackURL=%2Flogin%2Fverified"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { describe, it, expect, vi, afterEach } from "vitest"; | ||
| import { render, screen, cleanup, act } from "@testing-library/react"; | ||
| import "@testing-library/jest-dom/vitest"; | ||
|
|
||
| const replace = vi.fn(); | ||
| vi.mock("next/navigation", () => ({ | ||
| useRouter: () => ({ replace }), | ||
| })); | ||
|
|
||
| import { VerifiedView } from "@/app/login/verified/verified-view"; | ||
|
|
||
| // Mirrors the better-auth-ui card pattern: same Card + CardHeader + | ||
| // CardTitle + CardContent primitives, same outer `bg-muted/30` shell, same | ||
| // FieldDescription style for secondary text. The page itself is a server | ||
| // component (tested via Playwright); these tests pin the visible surface of | ||
| // the client view so a refactor that drops the success message or rewrites | ||
| // the manual link target surfaces here. | ||
|
|
||
| describe("VerifiedView", () => { | ||
| afterEach(() => { | ||
| cleanup(); | ||
| replace.mockReset(); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| it("renders the success heading, signed-in copy, and chat-bound CTA", () => { | ||
| render(<VerifiedView />); | ||
|
|
||
| expect(screen.getByText(/email verified/i)).toBeInTheDocument(); | ||
| expect(screen.getByText(/you're signed in/i)).toBeInTheDocument(); | ||
| expect(screen.getByRole("link", { name: /chat now/i })).toHaveAttribute("href", "/chat"); | ||
| expect(screen.getByText(/redirecting in 5s/i)).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders the success icon", () => { | ||
| const { container } = render(<VerifiedView />); | ||
| expect(container.querySelector("svg")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("redirects via router.replace to /chat at the 5-second mark", async () => { | ||
| vi.useFakeTimers(); | ||
| render(<VerifiedView />); | ||
|
|
||
| // Initial render schedules setTimeout — replace must not fire yet. | ||
| expect(replace).not.toHaveBeenCalled(); | ||
|
|
||
| // Advance in 1s ticks. Each tick fires the setRemaining callback, then | ||
| // we await act() so React re-renders and re-runs the effect with the | ||
| // new remaining value (the effect's dep is `[remaining, router]`). | ||
| // Without draining React between advances, all 5 timers fire against | ||
| // stale `remaining=5` state and the final router.replace never triggers. | ||
| // | ||
| // 5 ticks exactly — the 5th decrements remaining to 0 and the effect's | ||
| // `if (remaining <= 0)` branch fires router.replace. Pinning this | ||
| // count catches a regression where the redirect drifts to 6s. | ||
| for (let i = 0; i < 5; i++) { | ||
| await act(async () => { | ||
| await vi.advanceTimersByTimeAsync(1000); | ||
| }); | ||
| } | ||
|
|
||
| expect(replace).toHaveBeenCalledTimes(1); | ||
| expect(replace).toHaveBeenCalledWith("/chat"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.