Add user login and select organization audit logs

This commit is contained in:
Carlos Monastyrski
2025-11-13 21:27:32 -03:00
parent 731a0d5cd0
commit 09ac5c8df5
2 changed files with 112 additions and 1 deletions
@@ -454,6 +454,32 @@ export const authLoginServiceFactory = ({
});
}
if (organizationId) {
await auditLogService.createAuditLog({
orgId: organizationId,
ipAddress: ip,
userAgent,
userAgentType: getUserAgentType(userAgent),
actor: {
type: ActorType.USER,
metadata: {
email: userEnc.email,
userId: userEnc.userId,
username: userEnc.username
}
},
event: {
type: EventType.USER_LOGIN,
metadata: {
email,
userAgent,
ipAddress: ip,
authMethod
}
}
});
}
return {
tokens: {
accessToken: token.access,
@@ -646,6 +672,32 @@ export const authLoginServiceFactory = ({
}
}
await auditLogService.createAuditLog({
orgId: organizationId,
ipAddress,
userAgent,
userAgentType: getUserAgentType(userAgent),
actor: {
type: ActorType.USER,
metadata: {
email: user.email,
userId: user.id,
username: user.username
}
},
event: {
type: EventType.USER_SELECT_ORGANIZATION,
metadata: {
email: user.email || "",
userAgent,
ipAddress,
organizationId,
organizationName: selectedOrg.name,
authMethod: decodedToken.authMethod
}
}
});
return {
...tokens,
user,
@@ -1039,6 +1091,36 @@ export const authLoginServiceFactory = ({
organizationId
});
if (organizationId) {
await auditLogService.createAuditLog({
orgId: organizationId,
ipAddress: ip,
userAgent,
userAgentType: getUserAgentType(userAgent),
actor: {
type: ActorType.USER,
metadata: {
email: userEnc.email,
userId: userEnc.userId,
username: userEnc.username
}
},
event: {
type: EventType.USER_LOGIN,
metadata: {
email,
userAgent,
ipAddress: ip,
authMethod: decodedProviderToken.authMethod,
organizationId,
...(isAuthMethodSaml(decodedProviderToken.authMethod) && {
authProvider: decodedProviderToken.authMethod
})
}
}
});
}
return { token, isMfaEnabled: false, user: userEnc, decodedProviderToken } as const;
};