- Build complete Next.js CRM for commercial real estate - Add authentication with JWT sessions and role-based access - Add GoHighLevel API integration for contacts, conversations, opportunities - Add AI-powered Control Center with tool calling - Add Setup page with onboarding checklist (/setup) - Add sidebar navigation with Setup menu item - Fix type errors in onboarding API, GHL services, and control center tools - Add Prisma schema with SQLite for local development - Add UI components with clay morphism design system Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
74 lines
1.7 KiB
TypeScript
74 lines
1.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { z } from 'zod';
|
|
import { prisma } from '@/lib/db';
|
|
import { verifyPassword, signToken, setSessionCookie } from '@/lib/auth';
|
|
import { Role } from '@/types';
|
|
|
|
const loginSchema = z.object({
|
|
email: z.string().email(),
|
|
password: z.string(),
|
|
});
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json();
|
|
const validated = loginSchema.parse(body);
|
|
|
|
// Find user
|
|
const user = await prisma.user.findUnique({
|
|
where: { email: validated.email },
|
|
});
|
|
|
|
if (!user) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid email or password' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Verify password
|
|
const isValid = await verifyPassword(validated.password, user.passwordHash);
|
|
if (!isValid) {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid email or password' },
|
|
{ status: 401 }
|
|
);
|
|
}
|
|
|
|
// Generate token
|
|
const token = signToken({
|
|
userId: user.id,
|
|
email: user.email,
|
|
role: user.role as Role,
|
|
});
|
|
|
|
// Set session cookie
|
|
await setSessionCookie(token);
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
user: {
|
|
id: user.id,
|
|
email: user.email,
|
|
firstName: user.firstName,
|
|
lastName: user.lastName,
|
|
role: user.role,
|
|
ghlLocationId: user.ghlLocationId,
|
|
},
|
|
token,
|
|
});
|
|
} catch (error) {
|
|
if (error instanceof z.ZodError) {
|
|
return NextResponse.json(
|
|
{ error: 'Validation failed', details: error.issues },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
console.error('Login error:', error);
|
|
return NextResponse.json(
|
|
{ error: 'Internal server error' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|