try refactor auth
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user