GET still shows a one-button page, a used invitation can resend or reopen for the signed-in invitee, and an email-change GET no longer signs the account out.
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import type { NextRequest } from "next/server";
|
|
import { readLimitedJson } from "./validation/requestBody";
|
|
|
|
export const ONE_TIME_LINK_TOKEN_MIN_LENGTH = 10;
|
|
|
|
function usableToken(value: unknown): string | null {
|
|
if (typeof value !== "string") return null;
|
|
if (value.length < ONE_TIME_LINK_TOKEN_MIN_LENGTH) return null;
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* One-time email links put the secret in the query string. Confirm pages POST
|
|
* that same URL (and a hidden field) so a GET from a mail scanner spends nothing.
|
|
*/
|
|
export async function readOneTimeLinkToken(
|
|
request: NextRequest,
|
|
): Promise<string | null> {
|
|
const fromQuery = usableToken(request.nextUrl.searchParams.get("token"));
|
|
if (fromQuery) return fromQuery;
|
|
|
|
if (request.method === "GET" || request.method === "HEAD") {
|
|
return null;
|
|
}
|
|
|
|
const contentType = request.headers.get("content-type") ?? "";
|
|
if (contentType.includes("application/json")) {
|
|
const parsed = await readLimitedJson(request);
|
|
if (parsed.ok === false) return null;
|
|
if (parsed.value && typeof parsed.value === "object" && "token" in parsed.value) {
|
|
return usableToken((parsed.value as { token: unknown }).token);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
const form = await request.formData();
|
|
return usableToken(form.get("token"));
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|