feat: Implement the ability to login via multiple means when signed up via google/non-cal providers (#14082)

Co-authored-by: zomars <[email protected]>
This commit is contained in:
Anwar Sadathandzomars authored and GitHub committed 2024-04-10 14:38:39 -07:00
1 parent e30b41a85a
commit 4565c3311c
21 files changed
+270 -120

No files matched your search

@@ -72,7 +72,6 @@ export default function CalComAdapter(prismaClient: PrismaClient) {
}
},
linkAccount: (data: Prisma.AccountCreateInput) => prismaClient.account.create({ data }),
// @NOTE: All methods below here are not being used but leaved if they are required
unlinkAccount: (provider_providerAccountId: Prisma.AccountProviderProviderAccountIdCompoundUniqueInput) =>
prismaClient.account.delete({ where: { provider_providerAccountId } }),
};
@@ -1,5 +1,4 @@
import type { Membership, Team, UserPermissionRole } from "@prisma/client";
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
import type { AuthOptions, Session } from "next-auth";
import type { JWT } from "next-auth/jwt";
import { encode } from "next-auth/jwt";
@@ -130,13 +129,10 @@ const providers: Provider[] = [
identifier: user.email,
});
if (user.identityProvider !== IdentityProvider.CAL && !credentials.totpCode) {
throw new Error(ErrorCode.ThirdPartyIdentityProviderEnabled);
}
if (!user.password?.hash && user.identityProvider == IdentityProvider.CAL) {
if (!user.password?.hash && user.identityProvider !== IdentityProvider.CAL && !credentials.totpCode) {
throw new Error(ErrorCode.IncorrectEmailPassword);
}
if (!user.password?.hash && user.identityProvider !== IdentityProvider.CAL && !credentials.totpCode) {
if (!user.password?.hash && user.identityProvider == IdentityProvider.CAL) {
throw new Error(ErrorCode.IncorrectEmailPassword);
}
@@ -725,7 +721,11 @@ export const AUTH_OPTIONS: AuthOptions = {
try {
// If old user without Account entry we link their google account
if (existingUser.accounts.length === 0) {
const linkAccountWithUserData = { ...account, userId: existingUser.id };
const linkAccountWithUserData = {
...account,
userId: existingUser.id,
providerEmail: user.email,
};
await calcomAdapter.linkAccount(linkAccountWithUserData);
}
} catch (error) {
@@ -824,7 +824,7 @@ export const AUTH_OPTIONS: AuthOptions = {
existingUserWithEmail.identityProvider === IdentityProvider.CAL &&
(idP === IdentityProvider.GOOGLE || idP === IdentityProvider.SAML)
) {
const updatedUser = await prisma.user.update({
await prisma.user.update({
where: { email: existingUserWithEmail.email },
// also update email to the IdP email
data: {
@@ -834,22 +834,6 @@ export const AUTH_OPTIONS: AuthOptions = {
},
});
// safely delete password from UserPassword table if it exists
try {
await prisma.userPassword.delete({
where: { userId: updatedUser.id },
});
} catch (err) {
if (
err instanceof PrismaClientKnownRequestError &&
(err.code === "P2025" || err.code === "P2016")
) {
log.warn("UserPassword not found for user", safeStringify(existingUserWithEmail));
} else {
log.warn("Could not delete UserPassword for user", safeStringify(existingUserWithEmail));
}
}
if (existingUserWithEmail.twoFactorEnabled) {
return loginWithTotp(existingUserWithEmail.email);
} else {
@@ -886,7 +870,7 @@ export const AUTH_OPTIONS: AuthOptions = {
},
});
const linkAccountNewUserData = { ...account, userId: newUser.id };
const linkAccountNewUserData = { ...account, userId: newUser.id, providerEmail: user.email };
await calcomAdapter.linkAccount(linkAccountNewUserData);
if (account.twoFactorEnabled) {