feat: add dynamic site settings, hero media, admin panels, and database integration
This commit is contained in:
+68
@@ -0,0 +1,68 @@
|
||||
import { SignJWT, jwtVerify } from 'jose';
|
||||
import { cookies } from 'next/headers';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
const secretKey = process.env.JWT_SECRET || 'super-secret-key-replace-me-in-production';
|
||||
const key = new TextEncoder().encode(secretKey);
|
||||
|
||||
export async function encrypt(payload: any) {
|
||||
return await new SignJWT(payload)
|
||||
.setProtectedHeader({ alg: 'HS256' })
|
||||
.setIssuedAt()
|
||||
.setExpirationTime('24h')
|
||||
.sign(key);
|
||||
}
|
||||
|
||||
export async function decrypt(input: string): Promise<any> {
|
||||
try {
|
||||
const { payload } = await jwtVerify(input, key, {
|
||||
algorithms: ['HS256'],
|
||||
});
|
||||
return payload;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function getSession() {
|
||||
const cookieStore = await cookies();
|
||||
const session = cookieStore.get('session')?.value;
|
||||
if (!session) return null;
|
||||
return await decrypt(session);
|
||||
}
|
||||
|
||||
export async function createSession(userId: string) {
|
||||
const expires = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
||||
const session = await encrypt({ userId, expires });
|
||||
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.set('session', session, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
expires: expires,
|
||||
path: '/',
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteSession() {
|
||||
const cookieStore = await cookies();
|
||||
cookieStore.delete('session');
|
||||
}
|
||||
|
||||
export async function updateSession(request: NextRequest) {
|
||||
const session = request.cookies.get('session')?.value;
|
||||
if (!session) return;
|
||||
|
||||
const parsed = await decrypt(session);
|
||||
if (!parsed) return;
|
||||
|
||||
parsed.expires = new Date(Date.now() + 24 * 60 * 60 * 1000);
|
||||
const res = NextResponse.next();
|
||||
res.cookies.set({
|
||||
name: 'session',
|
||||
value: await encrypt(parsed),
|
||||
httpOnly: true,
|
||||
expires: parsed.expires,
|
||||
});
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { v2 as cloudinary } from 'cloudinary';
|
||||
|
||||
export async function uploadImage(file: File): Promise<string> {
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const buffer = Buffer.from(arrayBuffer);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const uploadStream = cloudinary.uploader.upload_stream(
|
||||
{ folder: 'moybeach', resource_type: 'auto' },
|
||||
(error, result) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(result!.secure_url);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
uploadStream.end(buffer);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClient | undefined;
|
||||
};
|
||||
|
||||
export const prisma =
|
||||
globalForPrisma.prisma ??
|
||||
new PrismaClient({
|
||||
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma;
|
||||
Reference in New Issue
Block a user