Skip to content
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions lib/user-data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
import { db } from "@/lib/db";

const SENSITIVE_KEYS = ["email", "phone", "ssn", "password", "token", "secret", "key", "auth", "credential"];

const redactParams = (params: Record<string, unknown>): Record<string, unknown> => {
const redacted: Record<string, unknown> = {};
for (const [key, value] of Object.entries(params)) {
const lowerKey = key.toLowerCase();
if (SENSITIVE_KEYS.some((sensitive) => lowerKey.includes(sensitive))) {
redacted[key] = "[REDACTED]";
} else {
redacted[key] = value;
}
}
return redacted;
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.

const logError = (functionName: string, params: Record<string, unknown>, error: unknown) => {
console.error(
JSON.stringify({
timestamp: new Date().toISOString(),
level: "error",
context: functionName,
params: redactParams(params),
error: error instanceof Error ? error.message : "Unknown error",
stack: error instanceof Error ? error.stack : undefined,
})
);
};

export const getUserById = async (id: string) => {
try {
const user = await db.user.findUnique({
Expand All @@ -9,9 +37,9 @@ export const getUserById = async (id: string) => {
}
});
return user;
} catch (error) {
console.log(error);
return null;
} catch (err) {
logError("getUserById", { id }, err);
throw err;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
};

Expand All @@ -21,9 +49,9 @@ export const getUserByEmail = async (email: string) => {
where: { email }
});
return user;
} catch (error) {
console.log(error);
return null;
} catch (err) {
logError("getUserByEmail", { email }, err);
throw err;
}
};

Expand All @@ -35,8 +63,8 @@ export const getAccountByUserId = async (userId: string) => {
}
});
return account;
} catch (error) {
console.log(error);
return null;
} catch (err) {
logError("getAccountByUserId", { userId }, err);
throw err;
}
};
};