feat: migrate from PocketBase to PostgreSQL with Drizzle ORM
- Add @project-e/db package with Drizzle schema and migrations - Replace PocketBase client with PostgreSQL-based database client - Migrate auth from custom to NextAuth.js - Add Docker Compose with PostgreSQL container - Update worker to use new database client - Remove PocketBase-specific files and migrations - Add drizzle config and initial migration
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import NextAuth from 'next-auth';
|
||||
import { authOptions } from '@/lib/auth-config';
|
||||
|
||||
const handler = NextAuth(authOptions);
|
||||
|
||||
export { handler as GET, handler as POST };
|
||||
@@ -1,52 +0,0 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { createPocketBaseClient } from '@/lib/pocketbase';
|
||||
import { z } from 'zod';
|
||||
|
||||
const loginSchema = z.object({
|
||||
email: z.string().email(),
|
||||
password: z.string().min(1),
|
||||
});
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { email, password } = loginSchema.parse(body);
|
||||
|
||||
const pb = createPocketBaseClient();
|
||||
|
||||
// Authenticate with PocketBase
|
||||
const authData = await pb.collection('users').authWithPassword(email, password);
|
||||
|
||||
// Set auth token in httpOnly cookie
|
||||
const response = NextResponse.json({
|
||||
user: {
|
||||
id: authData.record.id,
|
||||
email: authData.record.email,
|
||||
name: authData.record.name || authData.record.email,
|
||||
},
|
||||
token: authData.token,
|
||||
});
|
||||
|
||||
response.cookies.set('pb_auth', authData.token, {
|
||||
httpOnly: true,
|
||||
secure: process.env.COOKIE_SECURE === 'true',
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
maxAge: 60 * 60 * 24 * 7, // 7 days
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json(
|
||||
{ error: { code: 'VALIDATION_ERROR', message: 'Invalid input', details: error.issues } },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{ error: { code: 'AUTH_ERROR', message: 'Invalid email or password' } },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function POST() {
|
||||
const response = NextResponse.json({ success: true });
|
||||
|
||||
// Clear auth cookie
|
||||
response.cookies.set('pb_auth', '', {
|
||||
httpOnly: true,
|
||||
secure: process.env.COOKIE_SECURE === 'true',
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
maxAge: 0, // Expire immediately
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
@@ -1,29 +1,17 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { createPocketBaseClient } from '@/lib/pocketbase';
|
||||
import { getAuthUser } from '@/lib/auth';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const token = request.cookies.get('pb_auth')?.value;
|
||||
|
||||
if (!token) {
|
||||
const user = await getAuthUser(request);
|
||||
if (!user) {
|
||||
return NextResponse.json(
|
||||
{ error: { code: 'UNAUTHORIZED', message: 'Not authenticated' } },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const pb = createPocketBaseClient(token);
|
||||
|
||||
// Get current user
|
||||
const authData = await pb.collection('users').authRefresh();
|
||||
|
||||
return NextResponse.json({
|
||||
user: {
|
||||
id: authData.record.id,
|
||||
email: authData.record.email,
|
||||
name: authData.record.name || authData.record.email,
|
||||
},
|
||||
});
|
||||
return NextResponse.json({ user });
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: { code: 'AUTH_ERROR', message: 'Invalid or expired token' } },
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { createPocketBaseClient } from '@/lib/pocketbase';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const token = request.cookies.get('pb_auth')?.value;
|
||||
|
||||
if (!token) {
|
||||
return NextResponse.json(
|
||||
{ error: { code: 'UNAUTHORIZED', message: 'No auth token' } },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const pb = createPocketBaseClient(token);
|
||||
|
||||
// Refresh the auth token
|
||||
await pb.collection('users').authRefresh();
|
||||
|
||||
const newToken = pb.authStore.token;
|
||||
|
||||
const response = NextResponse.json({
|
||||
token: newToken,
|
||||
});
|
||||
|
||||
response.cookies.set('pb_auth', newToken, {
|
||||
httpOnly: true,
|
||||
secure: process.env.COOKIE_SECURE === 'true',
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
maxAge: 60 * 60 * 24 * 7, // 7 days
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch {
|
||||
return NextResponse.json(
|
||||
{ error: { code: 'AUTH_ERROR', message: 'Token refresh failed' } },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user