mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +00:00
Clicking an invitation link while signed out dropped the visitor on a bare login form, even though most invitees have no account yet and nothing on screen told them to create one. Signed-out visitors now get the invitation itself: who invited them, which workspace/project, which role, and which address it was sent to. The primary call to action follows whether an account already exists for that address — "Create your account" when it does not, "Sign in to accept" when it does. The sign-up path carries the invitation forward, so a new account lands back on the invitation and from there on the shared workspace/project instead of the onboarding wizard: - the register link passes invitationToken, the invited email and a callbackUrl - the register form locks the email to the invited address and shows what is being joined - the verification email round-trips the destination through a sanitized `next` parameter - login and verify-email keep the pending destination in their sign-in links Signing in with a different address than the one invited now explains the mismatch instead of silently redirecting to the dashboard. Callback sanitization moves to lib/safe-redirect.ts so login, register, verify-email and the verification route share one open-redirect guard.
86 lines
2.6 KiB
TypeScript
86 lines
2.6 KiB
TypeScript
import { redirect } from 'next/navigation';
|
|
import { auth } from '@/lib/auth';
|
|
import { db } from '@/lib/db';
|
|
import { acceptInvitationTokenForUser, getInvitationPreviewByToken } from '@/lib/invitations';
|
|
import { InvitationAccountMismatch, InvitationLanding } from './invitation-landing';
|
|
|
|
interface InvitationAcceptPageProps {
|
|
searchParams: Promise<{
|
|
token?: string;
|
|
}>;
|
|
}
|
|
|
|
export default async function InvitationAcceptPage({ searchParams }: InvitationAcceptPageProps) {
|
|
const resolvedSearchParams = await searchParams;
|
|
const token = resolvedSearchParams.token?.trim();
|
|
|
|
if (!token) {
|
|
redirect('/login?error=invalid_invitation');
|
|
}
|
|
|
|
const session = await auth();
|
|
if (!session?.user?.id) {
|
|
// Signed-out visitors get the invitation itself instead of a bare login form:
|
|
// most of them have no account yet and need to be told to create one.
|
|
const preview = await getInvitationPreviewByToken(token);
|
|
return <InvitationLanding token={token} preview={preview} />;
|
|
}
|
|
|
|
const invitation = await db.invitation.findUnique({
|
|
where: { token },
|
|
select: {
|
|
id: true,
|
|
email: true,
|
|
status: true,
|
|
scope: true,
|
|
workspaceId: true,
|
|
projectId: true,
|
|
},
|
|
});
|
|
|
|
function redirectToInvitationTarget(inviteStatus: string) {
|
|
if (invitation?.scope === 'WORKSPACE' && invitation.workspaceId) {
|
|
redirect(`/workspaces/${invitation.workspaceId}?invite=${inviteStatus}`);
|
|
}
|
|
if (invitation?.scope === 'PROJECT' && invitation.projectId) {
|
|
redirect(`/projects/${invitation.projectId}?invite=${inviteStatus}`);
|
|
}
|
|
}
|
|
|
|
const userEmail = session.user.email?.toLowerCase().trim();
|
|
if (!userEmail) {
|
|
redirect('/dashboard?invite=invalid_email');
|
|
}
|
|
|
|
const result = await acceptInvitationTokenForUser({
|
|
token,
|
|
userId: session.user.id,
|
|
email: userEmail,
|
|
});
|
|
|
|
if (result === 'accepted') {
|
|
redirectToInvitationTarget('accepted');
|
|
redirect('/dashboard?invite=accepted');
|
|
}
|
|
if (result === 'expired') {
|
|
redirectToInvitationTarget('expired');
|
|
redirect('/dashboard?invite=expired');
|
|
}
|
|
if (result === 'forbidden') {
|
|
// Signed in with a different address than the one invited — say so instead of
|
|
// dropping the user on the dashboard with no explanation.
|
|
return (
|
|
<InvitationAccountMismatch
|
|
invitedEmail={invitation?.email ?? 'another address'}
|
|
signedInEmail={userEmail}
|
|
/>
|
|
);
|
|
}
|
|
|
|
if (result === 'not_found' && invitation?.status === 'ACCEPTED') {
|
|
redirectToInvitationTarget('already_accepted');
|
|
}
|
|
|
|
redirect('/dashboard?invite=not_found');
|
|
}
|