Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const SECURITY_CONFIG = {
WEBHOOK_RATE_LIMIT_MS: 1000,
JWT_EXPIRATION: "1m",
REFRESH_TOKEN_EXPIRATION_DAYS: 30,
BCRYPT_ROUNDS: 12,
} as const;

// HTTP Status codes commonly used
Expand Down
7 changes: 4 additions & 3 deletions services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import prisma from "../prisma/client";
import bcrypt from "bcrypt";
import crypto from "crypto";
import { SECURITY_CONFIG } from "../constants";
import { RefreshTokenRecord } from "../types/webhook.types";

export class AuthService {
static async createUser(email: string, password: string) {
const hashed = await bcrypt.hash(password, 10);
const hashed = await bcrypt.hash(password, SECURITY_CONFIG.BCRYPT_ROUNDS);
return await prisma.user.create({
data: { email, password: hashed }
});
Expand Down Expand Up @@ -47,7 +48,7 @@ export class AuthService {
return await prisma.refreshToken.delete({ where: { token } });
}

static async isRefreshTokenValid(refreshToken: any) {
return refreshToken && new Date() < new Date(refreshToken.expiresAt);
static async isRefreshTokenValid(refreshToken: RefreshTokenRecord | null): Promise<boolean> {
return refreshToken !== null && new Date() < new Date(refreshToken.expiresAt);
}
}
19 changes: 19 additions & 0 deletions types/webhook.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface WebhookPayload {
action: string;
note?: any;
userId: number;
timestamp: string;
[key: string]: any;
}

export interface RefreshTokenRecord {
id: number;
token: string;
userId: number;
expiresAt: Date;
createdAt: Date;
user: {
id: number;
email: string;
};
}
9 changes: 8 additions & 1 deletion utils/triggerWebhook.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import prisma from "../prisma/client";
import { SECURITY_CONFIG } from "../constants";
import { WebhookPayload } from "../types/webhook.types";

const webhookCache = new Map<string, number>();

export async function triggerWebhook(
userId: number,
action: string,
payload: any
noteData: any
) {
const payload: WebhookPayload = {
action,
note: noteData,
userId,
timestamp: new Date().toISOString()
};
const webhooks = await prisma.webhook.findMany({
where: { userId, action },
});
Expand Down