Removed next-auth and refactored authentication

This commit is contained in:
DerTyp7
2025-10-09 14:04:34 +02:00
parent a12d607565
commit 67e986ab07
18 changed files with 174 additions and 247 deletions
+3 -3
View File
@@ -1,14 +1,14 @@
import ImageManager from '@/components/ImageManager';
import ImageUpload from '@/components/ImageUpload';
import { ImagesResponse, TagsResponse } from '@/interfaces/api';
import { getAuthStatus } from '@/lib/auth-utils';
import { getSession } from '@/lib/session';
import styles from '@/styles/AdminPage.module.scss';
import { redirect } from 'next/navigation';
export default async function AdminPage() {
const { isAuthenticated } = await getAuthStatus();
const session = await getSession();
if (!isAuthenticated) {
if (!session.isAuthenticated) {
redirect('/login');
}
+15
View File
@@ -0,0 +1,15 @@
import { getSession } from '@/lib/session';
import { NextResponse } from 'next/server';
export async function POST(request: Request) {
const session = await getSession();
const { password } = await request.json();
if (password === process.env.ADMIN_TOKEN) {
session.isAuthenticated = true;
await session.save();
return NextResponse.json({ success: true });
} else {
return NextResponse.json({ error: 'Invalid password' }, { status: 401 });
}
}
+10
View File
@@ -0,0 +1,10 @@
import { getSession } from '@/lib/session';
import { NextResponse } from 'next/server';
export async function POST() {
const session = await getSession();
session.isAuthenticated = false;
await session.save();
return NextResponse.json({ success: true });
}
+7
View File
@@ -0,0 +1,7 @@
import { getSession } from '@/lib/session';
import { NextResponse } from 'next/server';
export async function GET() {
const session = await getSession();
return NextResponse.json({ isAuthenticated: session.isAuthenticated });
}
+4 -2
View File
@@ -1,9 +1,11 @@
import { isAuthenticated } from '@/lib/auth-utils';
import { deleteImageById } from '@/lib/data';
import { getSession } from '@/lib/session';
import { NextRequest, NextResponse } from 'next/server';
export async function DELETE(request: NextRequest, context: { params: Promise<{ id: string }> }) {
if (!(await isAuthenticated(request))) {
const session = await getSession();
if (!session.isAuthenticated) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
+9 -4
View File
@@ -1,10 +1,11 @@
import { imagesDir } from '@/const/api';
import { ImagesResponse } from '@/interfaces/api';
import { ImageMeta } from '@/interfaces/image';
import { isAuthenticated } from '@/lib/auth-utils';
import { addImage, getImageData, stringToTags } from '@/lib/data';
import { getSession } from '@/lib/session';
import { promises as fs } from 'fs';
import { NextRequest, NextResponse } from 'next/server';
import path from 'path';
import sharp from 'sharp';
import { v4 as uuidv4 } from 'uuid';
@@ -52,8 +53,9 @@ export async function GET(request: NextRequest) {
}
export async function POST(request: NextRequest) {
console.log('upload file', request);
if (!(await isAuthenticated(request))) {
const session = await getSession();
if (!session.isAuthenticated) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
@@ -83,6 +85,8 @@ export async function POST(request: NextRequest) {
await fs.mkdir(imagesDir, { recursive: true });
}
await fs.writeFile(path.join(imagesDir, uuidFilename), buffer);
const imageInfo = await sharp(buffer).metadata();
const newImage: ImageMeta = {
@@ -95,8 +99,9 @@ export async function POST(request: NextRequest) {
};
await addImage(newImage);
return NextResponse.json({ message: 'File deleted successfully' }, { status: 201 });
return NextResponse.json({ message: 'File uploaded successfully' }, { status: 201 });
} catch (error) {
console.error(error);
return NextResponse.json({ error: error }, { status: 500 });
}
}
+4 -2
View File
@@ -1,9 +1,11 @@
import { isAuthenticated } from '@/lib/auth-utils';
import { stringToTags, updateTagsOfImageId } from '@/lib/data';
import { getSession } from '@/lib/session';
import { NextRequest, NextResponse } from 'next/server';
export async function PUT(request: NextRequest, context: { params: Promise<{ imageId: string }> }) {
if (!(await isAuthenticated(request))) {
const session = await getSession();
if (!session.isAuthenticated) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
+3 -3
View File
@@ -1,11 +1,11 @@
import LoginForm from '@/components/LoginForm';
import { getAuthStatus } from '@/lib/auth-utils';
import { getSession } from '@/lib/session';
import { redirect } from 'next/navigation';
export default async function LoginPage() {
const { isAuthenticated } = await getAuthStatus();
const session = await getSession();
if (isAuthenticated) {
if (session.isAuthenticated) {
redirect('/admin');
}