try refactor auth
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// src/pages/api/ai/query.ts
|
||||
import type { APIRoute } from 'astro';
|
||||
import { getSessionFromRequest, verifySession } from '../../../utils/auth.js';
|
||||
import { withAPIAuth, createAuthErrorResponse } from '../../../utils/auth.js';
|
||||
import { getCompressedToolsDataForAI } from '../../../utils/dataService.js';
|
||||
|
||||
export const prerender = false;
|
||||
@@ -275,30 +275,13 @@ Antworte NUR mit validen JSON. Keine zusätzlichen Erklärungen außerhalb des J
|
||||
|
||||
export const POST: APIRoute = async ({ request }) => {
|
||||
try {
|
||||
// Check if authentication is required
|
||||
const authRequired = process.env.AUTHENTICATION_NECESSARY !== 'false';
|
||||
let userId = 'test-user';
|
||||
|
||||
if (authRequired) {
|
||||
// Authentication check
|
||||
const sessionToken = getSessionFromRequest(request);
|
||||
if (!sessionToken) {
|
||||
return new Response(JSON.stringify({ error: 'Authentication required' }), {
|
||||
status: 401,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
}
|
||||
|
||||
const session = await verifySession(sessionToken);
|
||||
if (!session) {
|
||||
return new Response(JSON.stringify({ error: 'Invalid session' }), {
|
||||
status: 401,
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
}
|
||||
|
||||
userId = session.userId;
|
||||
// CONSOLIDATED: Replace 20+ lines with single function call
|
||||
const authResult = await withAPIAuth(request);
|
||||
if (!authResult.authenticated) {
|
||||
return createAuthErrorResponse();
|
||||
}
|
||||
|
||||
const userId = authResult.userId;
|
||||
|
||||
// Rate limiting
|
||||
if (!checkRateLimit(userId)) {
|
||||
|
||||
@@ -1,30 +1,17 @@
|
||||
---
|
||||
// src/pages/contribute/index.astro - Updated for Phase 3
|
||||
// src/pages/contribute/index.astro - Consolidated Auth
|
||||
import BaseLayout from '../../layouts/BaseLayout.astro';
|
||||
import { getSessionFromRequest, verifySession } from '../../utils/auth.js';
|
||||
import { getAuthContext, requireAuth } from '../../utils/serverAuth.js';
|
||||
import { withAuth } from '../../utils/auth.js'; // Note: .js extension!
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
// Check authentication
|
||||
const authRequired = import.meta.env.AUTHENTICATION_NECESSARY !== 'false';
|
||||
let isAuthenticated = false;
|
||||
let userEmail = '';
|
||||
|
||||
if (authRequired) {
|
||||
const sessionToken = getSessionFromRequest(Astro.request);
|
||||
if (sessionToken) {
|
||||
const session = await verifySession(sessionToken);
|
||||
if (session) {
|
||||
isAuthenticated = true;
|
||||
userEmail = session.email;
|
||||
}
|
||||
}
|
||||
|
||||
const authContext = await getAuthContext(Astro);
|
||||
const authRedirect = requireAuth(authContext, Astro.url.toString());
|
||||
if (authRedirect) return authRedirect;
|
||||
// CONSOLIDATED: Replace 15+ lines with single function call
|
||||
const authResult = await withAuth(Astro);
|
||||
if (authResult instanceof Response) {
|
||||
return authResult; // Redirect to login
|
||||
}
|
||||
|
||||
const { authenticated, userEmail, userId } = authResult;
|
||||
---
|
||||
|
||||
<BaseLayout title="Contribute" description="Contribute tools, methods, concepts, and knowledge articles to CC24-Guide">
|
||||
|
||||
@@ -1,32 +1,19 @@
|
||||
---
|
||||
// src/pages/contribute/knowledgebase.astro
|
||||
import BaseLayout from '../../layouts/BaseLayout.astro';
|
||||
import { getSessionFromRequest, verifySession } from '../../utils/auth.js';
|
||||
import { getAuthContext, requireAuth } from '../../utils/serverAuth.js';
|
||||
import { withAuth } from '../../utils/auth.js';
|
||||
import { getToolsData } from '../../utils/dataService.js';
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
// Check authentication
|
||||
const authRequired = import.meta.env.AUTHENTICATION_NECESSARY !== 'false';
|
||||
let isAuthenticated = false;
|
||||
let userEmail = '';
|
||||
|
||||
if (authRequired) {
|
||||
const sessionToken = getSessionFromRequest(Astro.request);
|
||||
if (sessionToken) {
|
||||
const session = await verifySession(sessionToken);
|
||||
if (session) {
|
||||
isAuthenticated = true;
|
||||
userEmail = session.email;
|
||||
}
|
||||
}
|
||||
|
||||
const authContext = await getAuthContext(Astro);
|
||||
const authRedirect = requireAuth(authContext, Astro.url.toString());
|
||||
if (authRedirect) return authRedirect;
|
||||
const authResult = await withAuth(Astro);
|
||||
if (authResult instanceof Response) {
|
||||
return authResult; // Redirect to login
|
||||
}
|
||||
|
||||
const { authenticated, userEmail, userId } = authResult;
|
||||
|
||||
const data = await getToolsData();
|
||||
const sortedTools = data.tools.sort((a: any, b: any) => a.name.localeCompare(b.name));
|
||||
---
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
---
|
||||
// src/pages/contribute/tool.astro
|
||||
import BaseLayout from '../../layouts/BaseLayout.astro';
|
||||
import { getAuthContext, requireAuth } from '../../utils/serverAuth.js';
|
||||
import { withAuth } from '../../utils/auth.js';
|
||||
import { getToolsData } from '../../utils/dataService.js';
|
||||
|
||||
// Check authentication
|
||||
const authContext = await getAuthContext(Astro);
|
||||
const authRedirect = requireAuth(authContext, Astro.url.toString());
|
||||
if (authRedirect) return authRedirect;
|
||||
const authResult = await withAuth(Astro);
|
||||
if (authResult instanceof Response) {
|
||||
return authResult; // Redirect to login
|
||||
}
|
||||
|
||||
const { authenticated, userEmail, userId } = authResult;
|
||||
|
||||
// Load existing data for validation and editing
|
||||
const data = await getToolsData();
|
||||
|
||||
@@ -119,7 +119,8 @@ const tools = data.tools;
|
||||
navigateToMatrix: (toolName: string) => void;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
import { requireClientAuth } from '../utils/auth.js';
|
||||
// Handle view changes and filtering
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const toolsContainer = document.getElementById('tools-container') as HTMLElement;
|
||||
@@ -177,17 +178,11 @@ const tools = data.tools;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// AI Query Button Handler
|
||||
if (aiQueryBtn) {
|
||||
aiQueryBtn.addEventListener('click', async () => {
|
||||
const authStatus = await checkAuthentication();
|
||||
|
||||
if (authStatus.authRequired && !authStatus.authenticated) {
|
||||
const returnUrl = `${window.location.pathname}?view=ai`;
|
||||
window.location.href = `/api/auth/login?returnTo=${encodeURIComponent(returnUrl)}`;
|
||||
} else {
|
||||
switchToView('ai');
|
||||
}
|
||||
await requireClientAuth(() => switchToView('ai'), `${window.location.pathname}?view=ai`);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user