bagsy/apps/api/lib/auth-middleware.ts

42 lines
1 KiB
TypeScript

import { headers } from 'next/headers';
import { auth } from './auth';
/**
* Get the current session from request headers
* Used in API routes to validate authentication
*/
export async function getSession() {
const headerList = await headers();
const session = await auth.api.getSession({
headers: headerList,
});
return session;
}
/**
* Validate that user is authenticated
* Throws 401 if not authenticated
*/
export async function requireAuth() {
const session = await getSession();
if (!session?.user) {
throw new Error('Unauthorized');
}
return session;
}
/**
* Get the user's active organization (household)
* For now, returns a placeholder household with the user ID
* In production, this would fetch from better-auth organizations
*/
export async function getActiveHousehold() {
const session = await requireAuth();
// TODO: Implement proper household lookup from better-auth organizations
// For now, return a mock household with user ID
return {
id: session.user.id,
name: 'My Household',
};
}