Skip to content

Commit

Permalink
fix: retrieve internal user id instead and pass it to the UserSignIn …
Browse files Browse the repository at this point in the history
…audit log instead of using the Cognito sub id (#3266)
  • Loading branch information
craigzour authored Feb 22, 2024
1 parent 2c2985c commit 981336f
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,31 @@ export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(prisma),
events: {
async signIn({ user }) {
logEvent(user.id, { type: "User", id: user.id }, "UserSignIn");
if (!user.email) {
throw new Error(
"Could not produce UserSignIn audit log because of undefined email information"
);
}

const internalUser = await prisma.user.findUnique({
where: {
email: user.email,
},
select: {
id: true,
},
});

if (internalUser === null) {
throw new Error("Could not produce UserSignIn audit log because user does not exist");
}

logEvent(
internalUser.id,
{ type: "User", id: internalUser.id },
"UserSignIn",
`Cognito user unique identifier (sub): ${user.id}`
);
},
async signOut({ token }) {
logEvent(token.userId, { type: "User", id: token.userId }, "UserSignOut");
Expand Down

0 comments on commit 981336f

Please sign in to comment.