initial version of the app

This commit is contained in:
mikicvi
2025-09-21 17:11:02 +01:00
commit c8062cf96b
101 changed files with 23061 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { users } from '@/lib/db/schema';
import { eq } from 'drizzle-orm';
import bcrypt from 'bcryptjs';
import { createSession } from '@/lib/session';
import { logActivity, ACTIONS, ENTITY_TYPES } from '@/lib/activity-logger';
export async function POST(request: NextRequest) {
try {
const { email, password } = await request.json();
if (!email || !password) {
return NextResponse.json({ error: 'Email and password are required' }, { status: 400 });
}
// Find user by email
const user = await db.select().from(users).where(eq(users.email, email)).limit(1);
if (user.length === 0) {
return NextResponse.json({ error: 'Invalid credentials' }, { status: 401 });
}
// Verify password
const isValid = await bcrypt.compare(password, user[0].password);
if (!isValid) {
return NextResponse.json({ error: 'Invalid credentials' }, { status: 401 });
}
// Create session
await createSession({
userId: user[0].id,
email: user[0].email,
role: user[0].role as 'user' | 'admin',
});
// Log the login activity
await logActivity({
userId: user[0].id,
action: ACTIONS.USER_LOGIN,
entityType: ENTITY_TYPES.USER,
entityId: user[0].id,
details: {
email: user[0].email,
role: user[0].role,
},
request,
});
return NextResponse.json({
user: {
id: user[0].id,
email: user[0].email,
name: user[0].name,
surname: user[0].surname,
role: user[0].role,
},
message: 'Login successful',
});
} catch (error) {
console.error('Login error:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}
+7
View File
@@ -0,0 +1,7 @@
import { NextResponse } from 'next/server';
import { deleteSession } from '@/lib/session';
export async function POST() {
await deleteSession();
return NextResponse.json({ message: 'Logout successful' });
}
+70
View File
@@ -0,0 +1,70 @@
import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { users } from '@/lib/db/schema';
import { eq } from 'drizzle-orm';
import bcrypt from 'bcryptjs';
import { createSession } from '@/lib/session';
import { z } from 'zod';
const registerSchema = z.object({
email: z.string().email(),
name: z.string().min(1),
surname: z.string().min(1),
password: z.string().min(6),
});
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const validatedData = registerSchema.parse(body);
// Check if user already exists
const existingUser = await db.select().from(users).where(eq(users.email, validatedData.email)).limit(1);
if (existingUser.length > 0) {
return NextResponse.json({ error: 'User with this email already exists' }, { status: 400 });
}
// Hash password
const hashedPassword = await bcrypt.hash(validatedData.password, 10);
// Create new user
const [newUser] = await db
.insert(users)
.values({
id: crypto.randomUUID(),
email: validatedData.email,
name: validatedData.name,
surname: validatedData.surname,
password: hashedPassword,
role: 'user',
createdAt: new Date(),
updatedAt: new Date(),
})
.returning();
// Create session
await createSession({
userId: newUser.id,
email: newUser.email,
role: newUser.role as 'user' | 'admin',
});
return NextResponse.json({
user: {
id: newUser.id,
email: newUser.email,
name: newUser.name,
surname: newUser.surname,
role: newUser.role,
},
message: 'User created successfully',
});
} catch (error) {
if (error instanceof z.ZodError) {
return NextResponse.json({ error: 'Invalid input data', details: error.errors }, { status: 400 });
}
console.error('Registration error:', error);
return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
}
}