Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions apps/web/app/api/auth/signup/handlers/calcomSignupHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,22 @@ const handler: CustomNextApiHandler = async (body, usernameStatus, query) => {
isSignup: true,
});

if (foundToken?.teamId) {
const existingUser = await prisma.user.findUnique({
where: { email },
select: { invitedTo: true },
});
if (existingUser && existingUser.invitedTo !== foundToken.teamId) {
return NextResponse.json({ message: SIGNUP_ERROR_CODES.USER_ALREADY_EXISTS }, { status: 409 });
if (foundToken?.teamId) {
const existingUser = await prisma.user.findUnique({
where: { email },
select: { invitedTo: true, emailVerified: true, password: { select: { hash: true } } },
});
if (existingUser) {
const isInvitedPlaceholderAccount =
existingUser.invitedTo === foundToken.teamId &&
!existingUser.password?.hash &&
!existingUser.emailVerified;

if (!isInvitedPlaceholderAccount) {
return NextResponse.json({ message: SIGNUP_ERROR_CODES.USER_ALREADY_EXISTS }, { status: 409 });
}
}
}
}
} else {
const usernameAndEmailValidation = await validateAndGetCorrectedUsernameAndEmail({
username,
Expand Down
13 changes: 10 additions & 3 deletions apps/web/app/api/auth/signup/handlers/selfHostedHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ export default async function handler(body: Record<string, string>) {
if (foundToken?.teamId) {
const existingUser = await prisma.user.findUnique({
where: { email: userEmail },
select: { invitedTo: true },
select: { invitedTo: true, emailVerified: true, password: { select: { hash: true } } },
});
if (existingUser && existingUser.invitedTo !== foundToken.teamId) {
return NextResponse.json({ message: SIGNUP_ERROR_CODES.USER_ALREADY_EXISTS }, { status: 409 });
if (existingUser) {
const isInvitedPlaceholderAccount =
existingUser.invitedTo === foundToken.teamId &&
!existingUser.password?.hash &&
!existingUser.emailVerified;

if (!isInvitedPlaceholderAccount) {
return NextResponse.json({ message: SIGNUP_ERROR_CODES.USER_ALREADY_EXISTS }, { status: 409 });
}
}
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/features/auth/lib/next-auth-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -998,10 +998,10 @@ export const getOptions = ({
}

// check if user was invited
// Invited placeholder users may already have a generated username; password/email verification
// are the reliable signals that account setup is incomplete and should be completed here.
if (
!existingUserWithEmail.password?.hash &&
!existingUserWithEmail.emailVerified &&
!existingUserWithEmail.username
!existingUserWithEmail.password?.hash && !existingUserWithEmail.emailVerified
) {
await prisma.user.update({
where: {
Expand Down
Loading