bagsy/apps/api/lib/auth-middleware.ts
bananabas 4fe4a98b1c feat: Migrate from self-hosted to Vercel + Neon + Clerk
- Move database from self-hosted Postgres to Neon (free tier)
- Replace better-auth with Clerk authentication (email/password + OAuth)
- Integrate Ably for real-time sync (replaces custom WebSocket)
- Remove passkey authentication (pending Apple Developer account)
- Add Clerk Organizations for household management
- Implement custom household join-code flow with Clerk
- Add secure Ably key fetching via backend API
- Update iOS app for Clerk auth and Ably real-time sync
- Configure Vercel deployment settings
- Update environment configuration for new infrastructure

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-06-27 18:59:00 +01:00

42 lines
No EOL
825 B
TypeScript

import { auth } from '@clerk/nextjs/server';
/**
* Get the current Clerk user from request
*/
export async function getCurrentUser() {
const { userId } = await auth();
return userId;
}
/**
* Validate that user is authenticated
* Throws 401 if not authenticated
*/
export async function requireAuth() {
const { userId } = await auth();
if (!userId) {
throw new Error('Unauthorized');
}
return userId;
}
/**
* Get the user's active organization (household) from Clerk
*/
export async function getActiveHousehold() {
const { userId, orgId } = await auth();
if (!userId) {
throw new Error('Unauthorized');
}
if (!orgId) {
throw new Error('No active household');
}
return {
id: orgId,
userId,
name: 'My Household', // TODO: Fetch actual org name from Clerk
};
}