log login events
This commit is contained in:
parent
93bcd9a445
commit
6795fa130b
|
@ -3,6 +3,7 @@ import CredentialProvider from "next-auth/providers/credentials";
|
||||||
import { sha256sum } from "@/lib/util";
|
import { sha256sum } from "@/lib/util";
|
||||||
import { validateCredentials } from "@/lib/db";
|
import { validateCredentials } from "@/lib/db";
|
||||||
import { GRAVATAR_DEFAULT } from "@/lib/constants";
|
import { GRAVATAR_DEFAULT } from "@/lib/constants";
|
||||||
|
import { auditLogRaw } from "@/lib/audit";
|
||||||
|
|
||||||
const authOptions: AuthOptions = {
|
const authOptions: AuthOptions = {
|
||||||
providers: [
|
providers: [
|
||||||
|
@ -17,8 +18,15 @@ const authOptions: AuthOptions = {
|
||||||
|
|
||||||
if (credentials && await validateCredentials(credentials.email, credentials.password)) {
|
if (credentials && await validateCredentials(credentials.email, credentials.password)) {
|
||||||
console.log(`[${credentials.email}] Authentication succeeded`);
|
console.log(`[${credentials.email}] Authentication succeeded`);
|
||||||
|
|
||||||
const emailHash = sha256sum(credentials.email.trim().toLowerCase());
|
const emailHash = sha256sum(credentials.email.trim().toLowerCase());
|
||||||
|
|
||||||
|
await auditLogRaw({
|
||||||
|
user: credentials.email,
|
||||||
|
ts: new Date().toISOString(),
|
||||||
|
action: "login",
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: credentials.email,
|
id: credentials.email,
|
||||||
email: credentials.email,
|
email: credentials.email,
|
||||||
|
|
|
@ -8,7 +8,7 @@ export type AuditLog = {
|
||||||
user?: string | null,
|
user?: string | null,
|
||||||
ts: string,
|
ts: string,
|
||||||
action: AuditLogAction,
|
action: AuditLogAction,
|
||||||
data: any,
|
data?: any,
|
||||||
}
|
}
|
||||||
|
|
||||||
export function auditLog(action: AuditLogAction, data?: any) {
|
export function auditLog(action: AuditLogAction, data?: any) {
|
||||||
|
@ -23,6 +23,14 @@ export function auditLog(action: AuditLogAction, data?: any) {
|
||||||
data: data,
|
data: data,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
await auditLogRaw(log);
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Failed to write to log file:", e);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function auditLogRaw(log: AuditLog) {
|
||||||
console.log("Audit event:", log);
|
console.log("Audit event:", log);
|
||||||
|
|
||||||
if (process.env.AUDIT_FILE_PATH) {
|
if (process.env.AUDIT_FILE_PATH) {
|
||||||
|
@ -31,8 +39,4 @@ export function auditLog(action: AuditLogAction, data?: any) {
|
||||||
JSON.stringify(log) + "\n",
|
JSON.stringify(log) + "\n",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
console.error("Failed to write to log file:", e);
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue