Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix: handle updating emial cases

Tre Dubrava01fbdde00e87
diff --git a/backend/src/services/auth/auth-login-service.ts b/backend/src/services/auth/auth-login-service.ts
index ca5f71fa49..b720c6709d 100644
--- a/backend/src/services/auth/auth-login-service.ts
+++ b/backend/src/services/auth/auth-login-service.ts
@@ -63,7 +63,7 @@ import {
type TAuthLoginServiceFactoryDep = {
userDAL: TUserDALFactory;
- userAliasDAL: Pick<TUserAliasDALFactory, "findOne" | "create">;
+ userAliasDAL: Pick<TUserAliasDALFactory, "findOne" | "create" | "updateById">;
orgDAL: TOrgDALFactory;
tokenService: TAuthTokenServiceFactory;
smtpService: TSmtpService;
@@ -1180,6 +1180,45 @@ export const authLoginServiceFactory = ({
lastName: !user.isAccepted ? lastName : undefined
});
}
+
+ // Sync email/username if the provider email has changed (user found by alias)
+ const normalizedProviderEmail = email.trim().toLowerCase();
+ if (existingAlias && user.email !== normalizedProviderEmail) {
+ const conflictingUsers = await userDAL.findUserByUsername(normalizedProviderEmail);
+ const conflictingUser =
+ conflictingUsers?.length > 1
+ ? conflictingUsers.find((el) => el.username === normalizedProviderEmail)
+ : conflictingUsers?.[0];
+
+ if (conflictingUser && conflictingUser.id !== user.id) {
+ throw new BadRequestError({
+ message:
+ "Unable to complete login: the email associated with your SSO account is already in use by another Infisical user.",
+ name: "Oauth 2 login"
+ });
+ }
+
+ user = await userDAL.transaction(async (tx) => {
+ const updatedUser = await userDAL.updateById(
+ user!.id,
+ {
+ username: normalizedProviderEmail,
+ email: normalizedProviderEmail
+ },
+ tx
+ );
+
+ await userAliasDAL.updateById(
+ existingAlias.id,
+ {
+ emails: [normalizedProviderEmail]
+ },
+ tx
+ );
+
+ return updatedUser;
+ });
+ }
}
if (!orgId && orgSlug) {