Skip to content
Draft
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
18 changes: 10 additions & 8 deletions apps/api/src/lib/functions/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { auth } from "../auth";
import { logInfo } from "./database";
import { nanoid } from "nanoid";
import type { ApiContext } from "../types";
import { API_ERROR_MESSAGES } from "shared";
import { API_ERROR_MESSAGES, API_MIDDLEWARE_PUBLIC_ROUTES } from "shared";

export const MIDDLEWARE_PUBLIC_ROUTES = ["/health", "/api/auth"];
/**
* Middleware to set user and session context for each request. This middleware checks the authentication status of the incoming request, retrieves the user session if it exists, and sets relevant information in the context for downstream handlers to use. It also logs the request path and authentication status for monitoring purposes.
* @param c - The Hono context object
Expand All @@ -26,11 +25,11 @@ export async function setUserSessionContextMiddleware(c: Context, next: Next) {
c.set("user", null);
c.set("session", null);
c.set("teamId", null);
return next();
} else {
c.set("user", session.user);
c.set("session", session.session);
}

c.set("user", session.user);
c.set("session", session.session);
await next();
}

Expand All @@ -40,9 +39,12 @@ export async function setUserSessionContextMiddleware(c: Context, next: Next) {
* @param next - The next middleware function in the chain
*/
export async function authenticatedMiddleware(c: ApiContext, next: Next) {
const isPublicRoute = MIDDLEWARE_PUBLIC_ROUTES.some((route) =>
c.req.path.startsWith(route),
);
const isPublicRoute = API_MIDDLEWARE_PUBLIC_ROUTES.some((route) => {
if (route instanceof RegExp) {
return route.test(c.req.path);
}
return c.req.path.startsWith(route);
});
if (isPublicRoute) {
return next();
}
Expand Down
7 changes: 6 additions & 1 deletion apps/web/src/lib/functions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { redirect } from "@tanstack/react-router";
* @returns True if the pathname is a public route, false otherwise
*/
export function isPublicRoute(pathname: string) {
return PUBLIC_ROUTES.includes(pathname);
return PUBLIC_ROUTES.some((route) => {
if (route instanceof RegExp) {
return route.test(pathname);
}
return pathname === route;
});
}

/**
Expand Down
12 changes: 11 additions & 1 deletion packages/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,17 @@ export const AUTH_CONFIG = {
},
};

export const PUBLIC_ROUTES = ["/", "/sign-in", "/sign-up", "/forgot-password"];
export const PUBLIC_ROUTES = [
/^\/sign-in(\/.*)?$/,
/^\/sign-up(\/.*)?$/,
/^\/forgot-password(\/.*)?$/,
"/",
];

export const API_MIDDLEWARE_PUBLIC_ROUTES = [
/^\/health/,
/^\/api\/auth(\/.*)?$/,
];

export const THEME_CONFIG = {
accessKey: "fallback-theme",
Expand Down