feat: add prisma support, admin panel and auth
This commit is contained in:
39
app/lib/auth.ts
Normal file
39
app/lib/auth.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { SignJWT, jwtVerify } from 'jose';
|
||||
import { cookies } from 'next/headers';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const secretKey = process.env.JWT_SECRET || 'fallback_secret';
|
||||
const key = new TextEncoder().encode(secretKey);
|
||||
|
||||
export async function encrypt(payload: any) {
|
||||
return await new SignJWT(payload)
|
||||
.setProtectedHeader({ alg: 'HS256' })
|
||||
.setIssuedAt()
|
||||
.setExpirationTime('2h')
|
||||
.sign(key);
|
||||
}
|
||||
|
||||
export async function decrypt(input: string): Promise<any> {
|
||||
const { payload } = await jwtVerify(input, key, {
|
||||
algorithms: ['HS256'],
|
||||
});
|
||||
return payload;
|
||||
}
|
||||
|
||||
export async function login(formData: FormData) {
|
||||
// Real login logic will be in a server action, this is just for session management
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
(await cookies()).set('session', '', { expires: new Date(0) });
|
||||
}
|
||||
|
||||
export async function getSession() {
|
||||
const session = (await cookies()).get('session')?.value;
|
||||
if (!session) return null;
|
||||
try {
|
||||
return await decrypt(session);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
15
app/lib/prisma.ts
Normal file
15
app/lib/prisma.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { PrismaPg } from '@prisma/adapter-pg';
|
||||
import { Pool } from 'pg';
|
||||
|
||||
const globalForPrisma = global as unknown as { prisma: PrismaClient };
|
||||
|
||||
const connectionString = process.env.DATABASE_URL;
|
||||
const pool = new Pool({ connectionString });
|
||||
const adapter = new PrismaPg(pool);
|
||||
|
||||
export const prisma =
|
||||
globalForPrisma.prisma ||
|
||||
new PrismaClient({ adapter });
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
|
||||
Reference in New Issue
Block a user