- Fix pg-boss Job type imports (PgBoss.Job -> Job from pg-boss) - Replace deprecated teamConcurrency with localConcurrency - Add null checks for possibly undefined values (clients, import rows) - Fix tone type narrowing in profile.ts - Fix test type assertions (non-null assertions, explicit Record types) - Extract auth middleware into shared module - Fix rate limiter Map generic type
21 lines
617 B
TypeScript
21 lines
617 B
TypeScript
import { Elysia } from 'elysia';
|
|
import { auth, type User } from '../lib/auth';
|
|
|
|
/**
|
|
* Auth middleware plugin - adds `user` to the Elysia context.
|
|
* Import and `.use(authMiddleware)` in route files that need authentication.
|
|
*/
|
|
export const authMiddleware = new Elysia({ name: 'auth-middleware' })
|
|
.derive({ as: 'scoped' }, async ({ request, set }): Promise<{ user: User }> => {
|
|
const session = await auth.api.getSession({
|
|
headers: request.headers,
|
|
});
|
|
|
|
if (!session?.user) {
|
|
set.status = 401;
|
|
throw new Error('Unauthorized');
|
|
}
|
|
|
|
return { user: session.user as User };
|
|
});
|