revert: UserPassword (#13680)

This commit is contained in:
Alex van Andel authored and GitHub committed 2024-02-14 12:36:29 +00:00
1 parent bb9f8e5d38
commit 0a68c6d048
25 files changed
+89 -190

No files matched your search

+15 -20
View File
@@ -42,8 +42,8 @@ const ORGANIZATIONS_AUTOLINK =
const usernameSlug = (username: string) => `${slugify(username)}-${randomString(6).toLowerCase()}`;
const loginWithTotp = async (email: string) =>
`/auth/login?totp=${await (await import("./signJwt")).default({ email })}`;
const loginWithTotp = async (user: { email: string }) =>
`/auth/login?totp=${await (await import("./signJwt")).default({ email: user.email })}`;
type UserTeams = {
teams: (Membership & {
@@ -126,18 +126,18 @@ const providers: Provider[] = [
if (user.identityProvider !== IdentityProvider.CAL && !credentials.totpCode) {
throw new Error(ErrorCode.ThirdPartyIdentityProviderEnabled);
}
if (!user.password?.hash && user.identityProvider == IdentityProvider.CAL) {
if (!user.password && user.identityProvider == IdentityProvider.CAL) {
throw new Error(ErrorCode.IncorrectEmailPassword);
}
if (!user.password?.hash && user.identityProvider !== IdentityProvider.CAL && !credentials.totpCode) {
if (!user.password && user.identityProvider !== IdentityProvider.CAL && !credentials.totpCode) {
throw new Error(ErrorCode.IncorrectEmailPassword);
}
if (user.password?.hash && !credentials.totpCode) {
if (!user.password?.hash) {
if (user.password && !credentials.totpCode) {
if (!user.password) {
throw new Error(ErrorCode.IncorrectEmailPassword);
}
const isCorrectPassword = await verifyPassword(credentials.password, user.password.hash);
const isCorrectPassword = await verifyPassword(credentials.password, user.password);
if (!isCorrectPassword) {
throw new Error(ErrorCode.IncorrectEmailPassword);
}
@@ -717,7 +717,7 @@ export const AUTH_OPTIONS: AuthOptions = {
}
}
if (existingUser.twoFactorEnabled && existingUser.identityProvider === idP) {
return loginWithTotp(existingUser.email);
return loginWithTotp(existingUser);
} else {
return true;
}
@@ -733,7 +733,7 @@ export const AUTH_OPTIONS: AuthOptions = {
if (!userWithNewEmail) {
await prisma.user.update({ where: { id: existingUser.id }, data: { email: user.email } });
if (existingUser.twoFactorEnabled) {
return loginWithTotp(existingUser.email);
return loginWithTotp(existingUser);
} else {
return true;
}
@@ -752,9 +752,6 @@ export const AUTH_OPTIONS: AuthOptions = {
mode: "insensitive",
},
},
include: {
password: true,
},
});
if (existingUserWithEmail) {
@@ -765,7 +762,7 @@ export const AUTH_OPTIONS: AuthOptions = {
existingUserWithEmail.identityProvider !== IdentityProvider.CAL
) {
if (existingUserWithEmail.twoFactorEnabled) {
return loginWithTotp(existingUserWithEmail.email);
return loginWithTotp(existingUserWithEmail);
} else {
return true;
}
@@ -773,7 +770,7 @@ export const AUTH_OPTIONS: AuthOptions = {
// check if user was invited
if (
!existingUserWithEmail.password?.hash &&
!existingUserWithEmail.password &&
!existingUserWithEmail.emailVerified &&
!existingUserWithEmail.username
) {
@@ -795,7 +792,7 @@ export const AUTH_OPTIONS: AuthOptions = {
});
if (existingUserWithEmail.twoFactorEnabled) {
return loginWithTotp(existingUserWithEmail.email);
return loginWithTotp(existingUserWithEmail);
} else {
return true;
}
@@ -810,16 +807,14 @@ export const AUTH_OPTIONS: AuthOptions = {
where: { email: existingUserWithEmail.email },
// also update email to the IdP email
data: {
password: {
delete: true,
},
password: null,
email: user.email,
identityProvider: idP,
identityProviderId: account.providerAccountId,
},
});
if (existingUserWithEmail.twoFactorEnabled) {
return loginWithTotp(existingUserWithEmail.email);
return loginWithTotp(existingUserWithEmail);
} else {
return true;
}
@@ -858,7 +853,7 @@ export const AUTH_OPTIONS: AuthOptions = {
await calcomAdapter.linkAccount(linkAccountNewUserData);
if (account.twoFactorEnabled) {
return loginWithTotp(newUser.email);
return loginWithTotp(newUser);
} else {
return true;
}
@@ -138,22 +138,18 @@ async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
where: { email },
update: {
username,
password: hashedPassword,
emailVerified: new Date(Date.now()),
identityProvider: IdentityProvider.CAL,
password: {
upsert: {
create: { hash: hashedPassword },
update: { hash: hashedPassword },
},
},
},
create: {
username,
email,
password: hashedPassword,
identityProvider: IdentityProvider.CAL,
password: { create: { hash: hashedPassword } },
},
});
// Wrapping in a transaction as if one fails we want to rollback the whole thing to preventa any data inconsistencies
const { membership } = await createOrUpdateMemberships({
teamMetadata,
@@ -184,7 +180,7 @@ async function handler(req: RequestWithUsernameStatus, res: NextApiResponse) {
data: {
username,
email,
password: { create: { hash: hashedPassword } },
password: hashedPassword,
metadata: {
stripeCustomerId: customer.id,
checkoutSessionId,
@@ -79,19 +79,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
where: { email: userEmail },
update: {
username: correctedUsername,
password: {
upsert: {
create: { hash: hashedPassword },
update: { hash: hashedPassword },
},
},
password: hashedPassword,
emailVerified: new Date(Date.now()),
identityProvider: IdentityProvider.CAL,
},
create: {
username: correctedUsername,
email: userEmail,
password: { create: { hash: hashedPassword } },
password: hashedPassword,
identityProvider: IdentityProvider.CAL,
},
});
@@ -133,19 +128,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
where: { email: userEmail },
update: {
username: correctedUsername,
password: {
upsert: {
create: { hash: hashedPassword },
update: { hash: hashedPassword },
},
},
password: hashedPassword,
emailVerified: new Date(Date.now()),
identityProvider: IdentityProvider.CAL,
},
create: {
username: correctedUsername,
email: userEmail,
password: { create: { hash: hashedPassword } },
password: hashedPassword,
identityProvider: IdentityProvider.CAL,
},
});
@@ -67,7 +67,9 @@ const authedAdminProcedureWithRequestedUser = authedAdminProcedure.use(async ({
return next({
ctx: {
user: ctx.user,
requestedUser: user,
requestedUser:
/** Don't leak the password */
exclude(user, ["password"]),
},
});
});
@@ -82,7 +84,8 @@ export const userAdminRouter = router({
// TODO: Add search, pagination, etc.
const users = await prisma.user.findMany();
return users.map((user) => ({
...user,
/** Don't leak the password */
...exclude(user, ["password"]),
/**
* FIXME: This should be either a prisma extension or middleware
* @see https://www.prisma.io/docs/concepts/components/prisma-client/middleware