first commit

This commit is contained in:
DerTyp7
2025-10-06 19:22:47 +02:00
commit aecc4cf549
75 changed files with 9303 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
'use server';
import { signIn } from '@/auth';
import { AuthError } from 'next-auth';
import { cookies } from 'next/headers';
export async function authenticate(_prevState: string | undefined, formData: FormData): Promise<string> {
try {
await signIn('credentials', formData);
return 'success';
} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
case 'CredentialsSignin':
return 'Invalid token';
default:
return 'Something went wrong';
}
}
throw error;
}
}
export async function logout() {
const c = await cookies();
c.delete('authjs.session-token');
c.delete('authjs.csrf-token');
c.delete('authjs.callback-url');
}
+28
View File
@@ -0,0 +1,28 @@
'use server';
import { getToken } from 'next-auth/jwt';
import { NextRequest } from 'next/server';
export async function validateAdminToken(token: string): Promise<boolean> {
return token === process.env.ADMIN_TOKEN;
}
export async function getAuthStatus() {
const { auth } = await import('@/auth');
const session = await auth();
return {
isAuthenticated: !!session?.user,
user: session?.user,
};
}
export async function isAuthenticated(request: NextRequest): Promise<boolean> {
const token = await getToken({ req: request, secret: process.env.AUTH_SECRET });
if (!token || (token.exp && Date.now() / 1000 > token.exp)) {
return false;
}
return true;
}
+72
View File
@@ -0,0 +1,72 @@
import { imagesDir, jsonPath } from '@/const/api';
import { ImageMeta } from '@/interfaces/image';
import fs from 'fs/promises';
import path from 'path';
async function ensureDataDirectoryExists(): Promise<void> {
try {
await fs.access(path.dirname(jsonPath));
} catch {
await fs.mkdir(path.dirname(jsonPath), { recursive: true });
}
}
export async function getImageData(): Promise<ImageMeta[]> {
await ensureDataDirectoryExists();
try {
const data = await fs.readFile(jsonPath, 'utf8');
return JSON.parse(data);
} catch {
await updateImageData([]);
return [];
}
}
export function stringToTags(string: string): string[] {
return string
? string
.split(',')
.map((tag) => tag.trim())
.filter((t) => t !== 'All' && t.length > 0)
: [];
}
export async function updateTagsOfImageId(id: string, tags: string[]): Promise<number> {
const imagesData: ImageMeta[] = await getImageData();
const indexOfImage = imagesData.findIndex((i) => i.id === id);
if (indexOfImage === -1) {
return -1;
}
imagesData[indexOfImage].tags = tags;
updateImageData(imagesData);
return 0;
}
export async function deleteImageById(id: string): Promise<number> {
const imagesData: ImageMeta[] = await getImageData();
try {
const imagePath = imagesData.find((i) => i.id === id)?.relative_path;
if (imagePath) {
fs.rm(path.join(imagesDir, imagePath));
await updateImageData(imagesData.filter((i) => i.id !== id));
return 0;
}
} catch (e) {
console.log('Could not delete image', e);
}
return -1;
}
export async function addImage(newImage: ImageMeta): Promise<void> {
updateImageData([newImage].concat(await getImageData()));
}
async function updateImageData(newData: ImageMeta[]): Promise<void> {
await ensureDataDirectoryExists();
await fs.writeFile(jsonPath, JSON.stringify(newData, null, 2));
}