Improve user alias check logic and header usage on resend code

This commit is contained in:
Carlos Monastyrski
2025-08-11 13:24:31 -07:00
parent c2cea8cffc
commit 60b3f5c7c6
3 changed files with 13 additions and 11 deletions

View File

@@ -423,6 +423,7 @@ export const samlConfigServiceFactory = ({
organizationSlug: organization.slug,
authMethod: authProvider,
hasExchangedPrivateKey: true,
aliasId: userAlias.id,
authType: UserAliasType.SAML,
isUserCompleted,
...(relayState

View File

@@ -20,15 +20,12 @@ export const registerUserRouter = async (server: FastifyZodProvider) => {
headers: z.object({
referer: z.string().trim()
}),
body: z.object({
username: z.string().trim()
}),
response: {
200: z.object({})
}
},
handler: async (req) => {
await server.services.user.sendEmailVerificationCode(req.body.username, req.headers.referer);
await server.services.user.sendEmailVerificationCode(req.headers.referer);
return {};
}
});

View File

@@ -54,18 +54,22 @@ export const userServiceFactory = ({
permissionService,
userAliasDAL
}: TUserServiceFactoryDep) => {
const sendEmailVerificationCode = async (username: string, referer: string) => {
const sendEmailVerificationCode = async (referer: string) => {
const url = new URL(referer);
const refererToken = url.searchParams.get("token");
if (!refererToken)
throw new BadRequestError({ name: "Failed to send email verification code due to no token on referer" });
const { authType, aliasId, username } = crypto.jwt().decode(refererToken) as {
authType: string;
aliasId: string;
username: string;
};
// akhilmhdh: case sensitive email resolution
const users = await userDAL.findUserByUsername(username);
const user = users?.length > 1 ? users.find((el) => el.username === username) : users?.[0];
if (!user) throw new NotFoundError({ name: `User with username '${username}' not found` });
let { isEmailVerified } = user;
const url = new URL(referer);
const refererToken = url.searchParams.get("token");
if (!refererToken)
throw new BadRequestError({ name: "Failed to send email verification code due to no token on referer" });
const { authType } = crypto.jwt().decode(refererToken) as { authType: string };
const userAlias = await userAliasDAL.findOne({ userId: user.id, aliasType: authType });
const userAlias = await userAliasDAL.findOne({ userId: user.id, aliasType: authType, id: aliasId });
if (userAlias) {
isEmailVerified = userAlias.isEmailVerified;
}