Spend login, invite, and email-change links only after a confirm POST so mail scanners cannot use them up.
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.
This commit is contained in:
1 parent
600056499c
commit
a725cf1a32
33 files changed
+1002
-89
No files matched your search
@@ -0,0 +1,107 @@
|
||||
"use client";
|
||||
|
||||
import { useId } from "react";
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import Login from "../../components/modals/Login";
|
||||
import Button from "../../components/buttons/Button";
|
||||
import ContentLockup from "../../components/type/ContentLockup";
|
||||
|
||||
const confirmPageBgClass =
|
||||
"min-h-[100dvh] bg-[var(--color-surface-invert-brand-primary)]";
|
||||
|
||||
export type ConfirmLinkScreenProps = {
|
||||
title: string;
|
||||
description: string;
|
||||
buttonLabel: string;
|
||||
action: string;
|
||||
token: string;
|
||||
backHref: string;
|
||||
backLabel: string;
|
||||
missingTitle: string;
|
||||
missingDescription: string;
|
||||
missingHref: string;
|
||||
missingButtonLabel: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Figma: Overlay / Modal / Login (22078:587823)
|
||||
* One-button confirm so a mail scanner's GET cannot spend a one-time link.
|
||||
*/
|
||||
export function ConfirmLinkScreen({
|
||||
title,
|
||||
description,
|
||||
buttonLabel,
|
||||
action,
|
||||
token,
|
||||
backHref,
|
||||
backLabel,
|
||||
missingTitle,
|
||||
missingDescription,
|
||||
missingHref,
|
||||
missingButtonLabel,
|
||||
}: ConfirmLinkScreenProps) {
|
||||
const router = useRouter();
|
||||
const titleId = useId();
|
||||
const hasToken = token.length >= 10;
|
||||
const heading = hasToken
|
||||
? { title, description }
|
||||
: { title: missingTitle, description: missingDescription };
|
||||
|
||||
return (
|
||||
<div className={confirmPageBgClass}>
|
||||
<Login
|
||||
isOpen
|
||||
usePortal={false}
|
||||
backdropVariant="solid"
|
||||
onClose={() => {
|
||||
router.push(backHref);
|
||||
}}
|
||||
ariaLabelledBy={titleId}
|
||||
belowCard={
|
||||
<Link
|
||||
href={backHref}
|
||||
className="text-small-paragraph text-[var(--color-content-invert-tertiary,#2d2d2d)] text-center hover:opacity-90"
|
||||
>
|
||||
{backLabel}
|
||||
</Link>
|
||||
}
|
||||
>
|
||||
<div className="flex min-h-0 flex-1 flex-col gap-6 pt-2">
|
||||
<ContentLockup
|
||||
titleId={titleId}
|
||||
title={heading.title}
|
||||
description={heading.description}
|
||||
variant="login"
|
||||
alignment="left"
|
||||
/>
|
||||
{hasToken ? (
|
||||
<form method="POST" action={action} className="mt-auto flex flex-col gap-4">
|
||||
<input type="hidden" name="token" value={token} />
|
||||
<Button
|
||||
type="submit"
|
||||
size="large"
|
||||
buttonType="filled"
|
||||
palette="default"
|
||||
autoFocus
|
||||
className="w-full !justify-center text-center px-[var(--space-400)] py-[var(--space-300)]"
|
||||
>
|
||||
{buttonLabel}
|
||||
</Button>
|
||||
</form>
|
||||
) : (
|
||||
<Button
|
||||
href={missingHref}
|
||||
size="large"
|
||||
buttonType="filled"
|
||||
palette="default"
|
||||
className="mt-auto w-full !justify-center text-center px-[var(--space-400)] py-[var(--space-300)]"
|
||||
>
|
||||
{missingButtonLabel}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</Login>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { Metadata } from "next";
|
||||
import type { ReactNode } from "react";
|
||||
import messages from "../../../../messages/en/index";
|
||||
import { NO_INDEX_ROBOTS, routeMetadata } from "../../../../lib/siteMetadata";
|
||||
|
||||
export const metadata: Metadata = routeMetadata("/email-change/confirm", {
|
||||
title: messages.metadata.emailChangeConfirm.title,
|
||||
description: messages.metadata.emailChangeConfirm.description,
|
||||
robots: NO_INDEX_ROBOTS,
|
||||
});
|
||||
|
||||
export default function EmailChangeConfirmLayout({
|
||||
children,
|
||||
}: {
|
||||
children: ReactNode;
|
||||
}) {
|
||||
return children;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
"use client";
|
||||
|
||||
import { Suspense } from "react";
|
||||
import { useSearchParams } from "next/navigation";
|
||||
import { useTranslation } from "../../../contexts/MessagesContext";
|
||||
import { emailChangeVerifyPath } from "../../../../lib/create/confirmLinkPaths";
|
||||
import { ConfirmLinkScreen } from "../../_components/ConfirmLinkScreen";
|
||||
|
||||
function EmailChangeConfirmFallback() {
|
||||
const t = useTranslation("pages.profile");
|
||||
return (
|
||||
<div className="flex min-h-[100dvh] items-center justify-center bg-[var(--color-surface-invert-brand-primary)]">
|
||||
<p className="text-[14px] text-[var(--color-content-default-primary)]">
|
||||
{t("loading")}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function EmailChangeConfirmWithParams() {
|
||||
const t = useTranslation("pages.profile");
|
||||
const tLogin = useTranslation("pages.login");
|
||||
const searchParams = useSearchParams();
|
||||
const token = searchParams.get("token") ?? "";
|
||||
|
||||
return (
|
||||
<ConfirmLinkScreen
|
||||
title={t("emailChangeConfirmTitle")}
|
||||
description={t("emailChangeConfirmDescription")}
|
||||
buttonLabel={t("emailChangeConfirmButton")}
|
||||
action={emailChangeVerifyPath(token)}
|
||||
token={token}
|
||||
backHref="/"
|
||||
backLabel={tLogin("backToHome")}
|
||||
missingTitle={t("emailChangeConfirmMissingTitle")}
|
||||
missingDescription={t("emailChangeConfirmMissingDescription")}
|
||||
missingHref="/profile"
|
||||
missingButtonLabel={t("emailChangeConfirmMissingButton")}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default function EmailChangeConfirmPage() {
|
||||
return (
|
||||
<Suspense fallback={<EmailChangeConfirmFallback />}>
|
||||
<EmailChangeConfirmWithParams />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { Metadata } from "next";
|
||||
import type { ReactNode } from "react";
|
||||
import messages from "../../../../messages/en/index";
|
||||
import { NO_INDEX_ROBOTS, routeMetadata } from "../../../../lib/siteMetadata";
|
||||
|
||||
export const metadata: Metadata = routeMetadata("/invite/confirm", {
|
||||
title: messages.metadata.inviteConfirm.title,
|
||||
description: messages.metadata.inviteConfirm.description,
|
||||
robots: NO_INDEX_ROBOTS,
|
||||
});
|
||||
|
||||
export default function InviteConfirmLayout({
|
||||
children,
|
||||
}: {
|
||||
children: ReactNode;
|
||||
}) {
|
||||
return children;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
"use client";
|
||||
|
||||
import { Suspense } from "react";
|
||||
import { useSearchParams } from "next/navigation";
|
||||
import { useTranslation } from "../../../contexts/MessagesContext";
|
||||
import { inviteVerifyPath } from "../../../../lib/create/confirmLinkPaths";
|
||||
import {
|
||||
INVITE_COPY_QUERY,
|
||||
INVITE_COPY_VALUE,
|
||||
} from "../../../../lib/create/invitePaths";
|
||||
import { ConfirmLinkScreen } from "../../_components/ConfirmLinkScreen";
|
||||
|
||||
function InviteConfirmFallback() {
|
||||
const t = useTranslation("pages.invite");
|
||||
return (
|
||||
<div className="flex min-h-[100dvh] items-center justify-center bg-[var(--color-surface-invert-brand-primary)]">
|
||||
<p className="text-[14px] text-[var(--color-content-default-primary)]">
|
||||
{t("loadingFallback")}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function InviteConfirmWithParams() {
|
||||
const t = useTranslation("pages.invite");
|
||||
const searchParams = useSearchParams();
|
||||
const token = searchParams.get("token") ?? "";
|
||||
const copy = searchParams.get(INVITE_COPY_QUERY) === INVITE_COPY_VALUE;
|
||||
|
||||
return (
|
||||
<ConfirmLinkScreen
|
||||
title={t("confirmTitle")}
|
||||
description={t("confirmDescription")}
|
||||
buttonLabel={t("confirmButton")}
|
||||
action={inviteVerifyPath(token, { copy })}
|
||||
token={token}
|
||||
backHref="/"
|
||||
backLabel={t("backToHome")}
|
||||
missingTitle={t("confirmMissingTitle")}
|
||||
missingDescription={t("confirmMissingDescription")}
|
||||
missingHref="/"
|
||||
missingButtonLabel={t("backToHome")}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default function InviteConfirmPage() {
|
||||
return (
|
||||
<Suspense fallback={<InviteConfirmFallback />}>
|
||||
<InviteConfirmWithParams />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import type { Metadata } from "next";
|
||||
import messages from "../../../../messages/en/index";
|
||||
import { NO_INDEX_ROBOTS, routeMetadata } from "../../../../lib/siteMetadata";
|
||||
|
||||
export const metadata: Metadata = routeMetadata("/login/confirm", {
|
||||
title: messages.metadata.loginConfirm.title,
|
||||
description: messages.metadata.loginConfirm.description,
|
||||
robots: NO_INDEX_ROBOTS,
|
||||
});
|
||||
|
||||
export default function LoginConfirmLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return children;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
"use client";
|
||||
|
||||
import { Suspense } from "react";
|
||||
import { useSearchParams } from "next/navigation";
|
||||
import { useTranslation } from "../../../contexts/MessagesContext";
|
||||
import { magicLinkVerifyPath } from "../../../../lib/create/confirmLinkPaths";
|
||||
import { ConfirmLinkScreen } from "../../_components/ConfirmLinkScreen";
|
||||
|
||||
function LoginConfirmFallback() {
|
||||
const t = useTranslation("pages.login");
|
||||
return (
|
||||
<div className="flex min-h-[100dvh] items-center justify-center bg-[var(--color-surface-invert-brand-primary)]">
|
||||
<p className="text-[14px] text-[var(--color-content-default-primary)]">
|
||||
{t("loadingFallback")}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function LoginConfirmWithParams() {
|
||||
const t = useTranslation("pages.login");
|
||||
const searchParams = useSearchParams();
|
||||
const token = searchParams.get("token") ?? "";
|
||||
|
||||
return (
|
||||
<ConfirmLinkScreen
|
||||
title={t("confirmTitle")}
|
||||
description={t("confirmDescription")}
|
||||
buttonLabel={t("confirmButton")}
|
||||
action={magicLinkVerifyPath(token)}
|
||||
token={token}
|
||||
backHref="/"
|
||||
backLabel={t("backToHome")}
|
||||
missingTitle={t("confirmMissingTitle")}
|
||||
missingDescription={t("confirmMissingDescription")}
|
||||
missingHref="/login"
|
||||
missingButtonLabel={t("confirmMissingButton")}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export default function LoginConfirmPage() {
|
||||
return (
|
||||
<Suspense fallback={<LoginConfirmFallback />}>
|
||||
<LoginConfirmWithParams />
|
||||
</Suspense>
|
||||
);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextRequest } from "next/server";
|
||||
import type { Prisma } from "@prisma/client";
|
||||
import { prisma } from "../../../../../lib/server/db";
|
||||
import {
|
||||
@@ -16,12 +16,25 @@ import {
|
||||
getOrCreateRequestId,
|
||||
logRouteError,
|
||||
} from "../../../../../lib/server/requestId";
|
||||
import { redirectWithRequestId } from "../../../../../lib/server/redirectWithRequestId";
|
||||
import { readOneTimeLinkToken } from "../../../../../lib/server/oneTimeLinkToken";
|
||||
import { safeInternalPath } from "../../../../../lib/safeInternalPath";
|
||||
import { getPublicOrigin } from "../../../../../lib/server/publicOrigin";
|
||||
import { magicLinkConfirmPath } from "../../../../../lib/create/confirmLinkPaths";
|
||||
|
||||
const SCOPE = "auth.magicLink.verify";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
return handleMagicLinkVerify(request, "get");
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
return handleMagicLinkVerify(request, "post");
|
||||
}
|
||||
|
||||
async function handleMagicLinkVerify(
|
||||
request: NextRequest,
|
||||
mode: "get" | "post",
|
||||
) {
|
||||
const requestId = getOrCreateRequestId(request);
|
||||
|
||||
if (!isDatabaseConfigured()) {
|
||||
@@ -31,12 +44,13 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
try {
|
||||
const token = request.nextUrl.searchParams.get("token");
|
||||
if (!token || token.length < 10) {
|
||||
const token = await readOneTimeLinkToken(request);
|
||||
if (!token) {
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
"/login?error=invalid_link",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -45,7 +59,12 @@ export async function GET(request: NextRequest) {
|
||||
pepper = getSessionPepper();
|
||||
} catch (err) {
|
||||
logRouteError(SCOPE, requestId, err, { phase: "getSessionPepper" });
|
||||
return redirectWithRequestId(request, "/login?error=server", requestId);
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
"/login?error=server",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
const tokenHash = hashSessionToken(token, pepper);
|
||||
@@ -59,6 +78,7 @@ export async function GET(request: NextRequest) {
|
||||
request,
|
||||
"/login?error=invalid_link",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -67,6 +87,15 @@ export async function GET(request: NextRequest) {
|
||||
request,
|
||||
"/login?error=expired_link",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
if (mode === "get") {
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
magicLinkConfirmPath(token),
|
||||
requestId,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -97,19 +126,18 @@ export async function GET(request: NextRequest) {
|
||||
await setSessionCookie(sessionToken, expiresAt);
|
||||
|
||||
const dest = safeInternalPath(row.nextPath);
|
||||
return redirectWithRequestId(request, dest, requestId);
|
||||
return redirectWithRequestId(request, dest, requestId, 303);
|
||||
} catch (err) {
|
||||
logRouteError(SCOPE, requestId, err);
|
||||
return redirectWithRequestId(request, "/login?error=server", requestId);
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
"/login?error=server",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function redirectWithRequestId(
|
||||
request: NextRequest,
|
||||
path: string,
|
||||
requestId: string,
|
||||
): NextResponse {
|
||||
const res = NextResponse.redirect(new URL(path, getPublicOrigin(request)));
|
||||
res.headers.set(REQUEST_ID_HEADER, requestId);
|
||||
return res;
|
||||
function redirectStatus(mode: "get" | "post"): 303 | 307 {
|
||||
return mode === "post" ? 303 : 307;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextRequest } from "next/server";
|
||||
import { prisma } from "../../../../../lib/server/db";
|
||||
import {
|
||||
getSessionPepper,
|
||||
@@ -16,7 +16,9 @@ import {
|
||||
getSessionUser,
|
||||
setSessionCookie,
|
||||
} from "../../../../../lib/server/session";
|
||||
import { getPublicOrigin } from "../../../../../lib/server/publicOrigin";
|
||||
import { redirectWithRequestId } from "../../../../../lib/server/redirectWithRequestId";
|
||||
import { readOneTimeLinkToken } from "../../../../../lib/server/oneTimeLinkToken";
|
||||
import { inviteConfirmPath } from "../../../../../lib/create/confirmLinkPaths";
|
||||
import {
|
||||
INVITE_COPY_QUERY,
|
||||
INVITE_COPY_VALUE,
|
||||
@@ -27,6 +29,17 @@ import {
|
||||
const SCOPE = "invites.ruleStakeholder.verify";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
return handleStakeholderInviteVerify(request, "get");
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
return handleStakeholderInviteVerify(request, "post");
|
||||
}
|
||||
|
||||
async function handleStakeholderInviteVerify(
|
||||
request: NextRequest,
|
||||
mode: "get" | "post",
|
||||
) {
|
||||
const requestId = getOrCreateRequestId(request);
|
||||
|
||||
if (!isDatabaseConfigured()) {
|
||||
@@ -36,12 +49,13 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
try {
|
||||
const token = request.nextUrl.searchParams.get("token");
|
||||
if (!token || token.length < 10) {
|
||||
const token = await readOneTimeLinkToken(request);
|
||||
if (!token) {
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
"/login?error=invalid_link",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -50,10 +64,17 @@ export async function GET(request: NextRequest) {
|
||||
pepper = getSessionPepper();
|
||||
} catch (err) {
|
||||
logRouteError(SCOPE, requestId, err, { phase: "getSessionPepper" });
|
||||
return redirectWithRequestId(request, "/login?error=server", requestId);
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
"/login?error=server",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
const tokenHash = hashSessionToken(token, pepper);
|
||||
const copy =
|
||||
request.nextUrl.searchParams.get(INVITE_COPY_QUERY) === INVITE_COPY_VALUE;
|
||||
|
||||
const row = await prisma.ruleStakeholder.findUnique({
|
||||
where: { inviteTokenHash: tokenHash },
|
||||
@@ -62,18 +83,16 @@ export async function GET(request: NextRequest) {
|
||||
email: true,
|
||||
ruleId: true,
|
||||
inviteExpiresAt: true,
|
||||
acceptedAt: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (
|
||||
!row ||
|
||||
!row.inviteExpiresAt ||
|
||||
row.inviteExpiresAt < new Date()
|
||||
) {
|
||||
if (!row) {
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
inviteExpiredPath(token),
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -86,6 +105,43 @@ export async function GET(request: NextRequest) {
|
||||
request,
|
||||
"/login?error=stakeholder_wrong_account",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
const dest = invitedRulePath(row.ruleId, { copy });
|
||||
|
||||
if (row.acceptedAt) {
|
||||
if (existingSession) {
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
dest,
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
inviteExpiredPath(token),
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
if (!row.inviteExpiresAt || row.inviteExpiresAt < new Date()) {
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
inviteExpiredPath(token),
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
if (mode === "get") {
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
inviteConfirmPath(token, { copy }),
|
||||
requestId,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -100,8 +156,6 @@ export async function GET(request: NextRequest) {
|
||||
data: {
|
||||
userId: user.id,
|
||||
acceptedAt: new Date(),
|
||||
inviteTokenHash: null,
|
||||
inviteExpiresAt: null,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -110,22 +164,18 @@ export async function GET(request: NextRequest) {
|
||||
);
|
||||
await setSessionCookie(sessionToken, expiresAt);
|
||||
|
||||
const copy =
|
||||
request.nextUrl.searchParams.get(INVITE_COPY_QUERY) === INVITE_COPY_VALUE;
|
||||
const dest = invitedRulePath(row.ruleId, { copy });
|
||||
return redirectWithRequestId(request, dest, requestId);
|
||||
return redirectWithRequestId(request, dest, requestId, 303);
|
||||
} catch (err) {
|
||||
logRouteError(SCOPE, requestId, err);
|
||||
return redirectWithRequestId(request, "/login?error=server", requestId);
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
"/login?error=server",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function redirectWithRequestId(
|
||||
request: NextRequest,
|
||||
path: string,
|
||||
requestId: string,
|
||||
): NextResponse {
|
||||
const res = NextResponse.redirect(new URL(path, getPublicOrigin(request)));
|
||||
res.headers.set(REQUEST_ID_HEADER, requestId);
|
||||
return res;
|
||||
function redirectStatus(mode: "get" | "post"): 303 | 307 {
|
||||
return mode === "post" ? 303 : 307;
|
||||
}
|
||||
@@ -46,6 +46,7 @@ export const POST = apiRoute<RouteContext>(
|
||||
email: true,
|
||||
inviteTokenHash: true,
|
||||
inviteExpiresAt: true,
|
||||
acceptedAt: true,
|
||||
rule: { select: { userId: true, title: true, summary: true, document: true } },
|
||||
},
|
||||
});
|
||||
@@ -56,7 +57,7 @@ export const POST = apiRoute<RouteContext>(
|
||||
if (row.rule.userId !== user.id) {
|
||||
return forbidden();
|
||||
}
|
||||
if (row.inviteTokenHash === null) {
|
||||
if (row.acceptedAt) {
|
||||
return errorJson(
|
||||
"validation_error",
|
||||
"This stakeholder has already accepted the invite",
|
||||
|
||||
@@ -61,7 +61,6 @@ export const GET = apiRoute<RouteContext>(
|
||||
email: true,
|
||||
invitedAt: true,
|
||||
acceptedAt: true,
|
||||
inviteTokenHash: true,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -72,7 +71,7 @@ export const GET = apiRoute<RouteContext>(
|
||||
invitedAt: r.invitedAt.toISOString(),
|
||||
acceptedAt: r.acceptedAt?.toISOString() ?? null,
|
||||
status:
|
||||
r.inviteTokenHash !== null ? ("pending" as const) : ("accepted" as const),
|
||||
r.acceptedAt != null ? ("accepted" as const) : ("pending" as const),
|
||||
})),
|
||||
});
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { NextRequest } from "next/server";
|
||||
import { prisma } from "../../../../../lib/server/db";
|
||||
import {
|
||||
getSessionPepper,
|
||||
@@ -16,11 +16,24 @@ import {
|
||||
getOrCreateRequestId,
|
||||
logRouteError,
|
||||
} from "../../../../../lib/server/requestId";
|
||||
import { getPublicOrigin } from "../../../../../lib/server/publicOrigin";
|
||||
import { redirectWithRequestId } from "../../../../../lib/server/redirectWithRequestId";
|
||||
import { readOneTimeLinkToken } from "../../../../../lib/server/oneTimeLinkToken";
|
||||
import { emailChangeConfirmPath } from "../../../../../lib/create/confirmLinkPaths";
|
||||
|
||||
const SCOPE = "user.emailChange.verify";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
return handleEmailChangeVerify(request, "get");
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
return handleEmailChangeVerify(request, "post");
|
||||
}
|
||||
|
||||
async function handleEmailChangeVerify(
|
||||
request: NextRequest,
|
||||
mode: "get" | "post",
|
||||
) {
|
||||
const requestId = getOrCreateRequestId(request);
|
||||
|
||||
if (!isDatabaseConfigured()) {
|
||||
@@ -30,12 +43,13 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
try {
|
||||
const token = request.nextUrl.searchParams.get("token");
|
||||
if (!token || token.length < 10) {
|
||||
const token = await readOneTimeLinkToken(request);
|
||||
if (!token) {
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
"/profile?error=email_change_invalid",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -48,6 +62,7 @@ export async function GET(request: NextRequest) {
|
||||
request,
|
||||
"/profile?error=email_change_server",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -61,6 +76,15 @@ export async function GET(request: NextRequest) {
|
||||
request,
|
||||
"/profile?error=email_change_expired",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
|
||||
if (mode === "get") {
|
||||
return redirectWithRequestId(
|
||||
request,
|
||||
emailChangeConfirmPath(token),
|
||||
requestId,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -118,6 +142,7 @@ export async function GET(request: NextRequest) {
|
||||
request,
|
||||
"/profile?error=email_change_taken",
|
||||
requestId,
|
||||
303,
|
||||
);
|
||||
}
|
||||
if (
|
||||
@@ -130,6 +155,7 @@ export async function GET(request: NextRequest) {
|
||||
request,
|
||||
"/profile?error=email_change_expired",
|
||||
requestId,
|
||||
303,
|
||||
);
|
||||
}
|
||||
logRouteError(SCOPE, requestId, err, { phase: "transaction" });
|
||||
@@ -137,6 +163,7 @@ export async function GET(request: NextRequest) {
|
||||
request,
|
||||
"/profile?error=email_change_server",
|
||||
requestId,
|
||||
303,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -151,6 +178,7 @@ export async function GET(request: NextRequest) {
|
||||
request,
|
||||
"/profile?email_change=ok",
|
||||
requestId,
|
||||
303,
|
||||
);
|
||||
} catch (err) {
|
||||
logRouteError(SCOPE, requestId, err);
|
||||
@@ -158,16 +186,11 @@ export async function GET(request: NextRequest) {
|
||||
request,
|
||||
"/profile?error=email_change_server",
|
||||
requestId,
|
||||
redirectStatus(mode),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function redirectWithRequestId(
|
||||
request: NextRequest,
|
||||
path: string,
|
||||
requestId: string,
|
||||
): NextResponse {
|
||||
const res = NextResponse.redirect(new URL(path, getPublicOrigin(request)));
|
||||
res.headers.set(REQUEST_ID_HEADER, requestId);
|
||||
return res;
|
||||
function redirectStatus(mode: "get" | "post"): 303 | 307 {
|
||||
return mode === "post" ? 303 : 307;
|
||||
}
|
||||
@@ -76,6 +76,13 @@ const LoginContainer = memo<LoginProps>(
|
||||
emailField.focus();
|
||||
return;
|
||||
}
|
||||
const submitButton = dialog.querySelector<HTMLButtonElement>(
|
||||
'button[type="submit"]:not([disabled])',
|
||||
);
|
||||
if (submitButton) {
|
||||
submitButton.focus();
|
||||
return;
|
||||
}
|
||||
const focusableElements = dialog.querySelectorAll(focusableSelector);
|
||||
const firstElement = focusableElements[0] as HTMLElement;
|
||||
if (firstElement) {
|
||||
|
||||
Reference in new issue
Block a user