Skip to content

Scheduling infrastructure for absolutely everyone.

fix: get user loggedin if accouont is found + add better error handling for new user creation (#19229)

Benny Joocdb57eaa595e
diff --git a/apps/web/app/(use-page-wrapper)/auth/error/page.tsx b/apps/web/app/(use-page-wrapper)/auth/error/page.tsx
index cd550ded4b8..c8803a5d94e 100644
--- a/apps/web/app/(use-page-wrapper)/auth/error/page.tsx
+++ b/apps/web/app/(use-page-wrapper)/auth/error/page.tsx
@@ -21,7 +21,7 @@ const querySchema = z.object({
const ServerPage = async ({ searchParams }: PageProps) => {
const t = await getTranslate();
const { error } = querySchema.parse({ error: searchParams?.error || undefined });
- const errorMsg = error || t("error_during_login");
+ const errorMsg = t("error_during_login") + (error ? ` Error code: ${error}` : "");
return (
<AuthContainer title="" description="" isAppDir={true}>
<div>
diff --git a/packages/features/auth/lib/next-auth-options.ts b/packages/features/auth/lib/next-auth-options.ts
index c9d3bad441d..33f0553e6b5 100644
--- a/packages/features/auth/lib/next-auth-options.ts
+++ b/packages/features/auth/lib/next-auth-options.ts
@@ -944,6 +944,12 @@ export const getOptions = ({
identityProviderId: account.providerAccountId,
},
});
+
+ if (existingUserWithEmail.twoFactorEnabled) {
+ return loginWithTotp(existingUserWithEmail.email);
+ } else {
+ return true;
+ }
}
return "/auth/error?error=use-identity-login";
@@ -952,35 +958,39 @@ export const getOptions = ({
// Associate with organization if enabled by flag and idP is Google (for now)
const { orgUsername, orgId } = await checkIfUserShouldBelongToOrg(idP, user.email);
- const newUser = await prisma.user.create({
- data: {
- // Slugify the incoming name and append a few random characters to
- // prevent conflicts for users with the same name.
- username: orgId ? slugify(orgUsername) : usernameSlug(user.name),
- emailVerified: new Date(Date.now()),
- name: user.name,
- ...(user.image && { avatarUrl: user.image }),
- email: user.email,
- identityProvider: idP,
- identityProviderId: account.providerAccountId,
- ...(orgId && {
- verified: true,
- organization: { connect: { id: orgId } },
- teams: {
- create: { role: MembershipRole.MEMBER, accepted: true, team: { connect: { id: orgId } } },
- },
- }),
- creationSource: CreationSource.WEBAPP,
- },
- });
-
- const linkAccountNewUserData = { ...account, userId: newUser.id, providerEmail: user.email };
- await calcomAdapter.linkAccount(linkAccountNewUserData);
+ try {
+ const newUser = await prisma.user.create({
+ data: {
+ // Slugify the incoming name and append a few random characters to
+ // prevent conflicts for users with the same name.
+ username: orgId ? slugify(orgUsername) : usernameSlug(user.name),
+ emailVerified: new Date(Date.now()),
+ name: user.name,
+ ...(user.image && { avatarUrl: user.image }),
+ email: user.email,
+ identityProvider: idP,
+ identityProviderId: account.providerAccountId,
+ ...(orgId && {
+ verified: true,
+ organization: { connect: { id: orgId } },
+ teams: {
+ create: { role: MembershipRole.MEMBER, accepted: true, team: { connect: { id: orgId } } },
+ },
+ }),
+ creationSource: CreationSource.WEBAPP,
+ },
+ });
+ const linkAccountNewUserData = { ...account, userId: newUser.id, providerEmail: user.email };
+ await calcomAdapter.linkAccount(linkAccountNewUserData);
- if (account.twoFactorEnabled) {
- return loginWithTotp(newUser.email);
- } else {
- return true;
+ if (account.twoFactorEnabled) {
+ return loginWithTotp(newUser.email);
+ } else {
+ return true;
+ }
+ } catch (err) {
+ log.error("Error creating a new user", err);
+ return "/auth/error?error=use-identity-login";
}
}