-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(auth): make email-verification signup completable #3017
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
Open
jakevis
wants to merge
3
commits into
emdash-cms:main
Choose a base branch
from
jakevis:fix/signup-verification-link
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@emdash-cms/auth": patch | ||
| "emdash": patch | ||
| --- | ||
|
|
||
| Fixes email-verification signup, which could not be completed: the verification email linked to the JSON API endpoint instead of the signup page, the signup page itself redirected anonymous visitors to login, and that redirect dropped the `?token=` from the URL. The email now links to `/_emdash/admin/signup?token=…` (as the invite email already did), the page is reachable without a session, and the login redirect preserves the query string of the page it returns to. |
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
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
79 changes: 79 additions & 0 deletions
79
packages/core/tests/unit/middleware/admin-public-routes.test.ts
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,79 @@ | ||
| import { beforeAll, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| vi.mock("virtual:emdash/auth", () => ({ authenticate: vi.fn() })); | ||
| vi.mock("virtual:emdash/config", () => ({ default: {} })); | ||
| vi.mock("astro:middleware", () => ({ | ||
| defineMiddleware: (handler: unknown) => handler, | ||
| })); | ||
| vi.mock("@emdash-cms/auth", () => ({ | ||
| TOKEN_PREFIXES: {}, | ||
| generatePrefixedToken: vi.fn(), | ||
| hashPrefixedToken: vi.fn(), | ||
| VALID_SCOPES: [], | ||
| validateScopes: vi.fn(), | ||
| hasScope: vi.fn(() => false), | ||
| computeS256Challenge: vi.fn(), | ||
| Role: { ADMIN: 50 }, | ||
| })); | ||
| vi.mock("@emdash-cms/auth/adapters/kysely", () => ({ | ||
| createKyselyAdapter: vi.fn(() => ({ | ||
| getUserById: vi.fn(), | ||
| getUserByEmail: vi.fn(), | ||
| })), | ||
| })); | ||
|
|
||
| type AuthMiddlewareModule = typeof import("../../../src/astro/middleware/auth.js"); | ||
|
|
||
| let onRequest: AuthMiddlewareModule["onRequest"]; | ||
|
|
||
| beforeAll(async () => { | ||
| ({ onRequest } = await import("../../../src/astro/middleware/auth.js")); | ||
| }); | ||
|
|
||
| /** An anonymous GET to an admin page. */ | ||
| async function visit( | ||
| pathname: string, | ||
| ): Promise<{ response: Response; next: ReturnType<typeof vi.fn> }> { | ||
| const url = new URL(pathname, "https://site.example.com"); | ||
| const session = { | ||
| get: vi.fn().mockResolvedValue(null), | ||
| set: vi.fn(), | ||
| destroy: vi.fn(), | ||
| }; | ||
| const next = vi.fn(async () => new Response("ok")); | ||
| const response = await onRequest( | ||
| { | ||
| url, | ||
| request: new Request(url, { method: "GET" }), | ||
| locals: { emdash: { db: {}, config: {} } }, | ||
| session, | ||
| redirect: (location: string) => | ||
| new Response(null, { status: 302, headers: { Location: location } }), | ||
| } as Parameters<AuthMiddlewareModule["onRequest"]>[0], | ||
| next, | ||
| ); | ||
| return { response, next }; | ||
| } | ||
|
|
||
| describe("Anonymous access to admin pages", () => { | ||
| it.each([ | ||
| "/_emdash/admin/login", | ||
| "/_emdash/admin/signup?token=abc", | ||
| "/_emdash/admin/invite/accept?token=abc", | ||
| ])("serves %s without a session — emails link there", async (pathname) => { | ||
| const { response, next } = await visit(pathname); | ||
| expect(next).toHaveBeenCalledOnce(); | ||
| expect(response.status).toBe(200); | ||
| }); | ||
|
|
||
| it("redirects other admin pages to login, keeping the full URL to return to", async () => { | ||
| const { response, next } = await visit("/_emdash/admin/content/posts?token=abc&x=1"); | ||
| expect(next).not.toHaveBeenCalled(); | ||
| expect(response.status).toBe(302); | ||
| const location = new URL(response.headers.get("Location")!); | ||
| expect(location.pathname).toBe("/_emdash/admin/login"); | ||
| expect(location.searchParams.get("redirect")).toBe( | ||
| "/_emdash/admin/content/posts?token=abc&x=1", | ||
| ); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[needs fixing] The updated assertion no longer verifies the real email link.
The production caller (
packages/core/src/astro/routes/api/auth/signup/request.ts) passesbaseUrlfromgetSiteBaseUrl, which returns the public origin suffixed with/_emdash(e.g.https://example.com/_emdash). The new code inpackages/auth/src/signup.tsusesnew URL(\${config.baseUrl}/admin/signup`), so the emitted link ishttps://example.com/_emdash/admin/signup?token=…`, matching the PR description and the middleware public route/_emdash/admin/signup.However, the test still passes
baseUrl: "https://example.com"(line 103) and asserts"https://example.com/admin/signup?token=". The test will pass, but it validates a URL shape that is never sent to users and could miss regressions in the mount path.Update the fixture to pass
baseUrl: "https://example.com/_emdash"and assert the production URL: