Skip to content

Commit a282559

Browse files
authored
Merge pull request #1 from EveryInc/fix/cloudflare-env-vars
Fix: Access Cloudflare runtime env vars correctly
2 parents 0a22493 + cdc0361 commit a282559

5 files changed

Lines changed: 36 additions & 7 deletions

File tree

.vscode/extensions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"recommendations": ["astro-build.astro-vscode"],
3+
"unwantedRecommendations": []
4+
}

.vscode/launch.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"command": "./node_modules/.bin/astro dev",
6+
"name": "Development server",
7+
"request": "launch",
8+
"type": "node-terminal"
9+
}
10+
]
11+
}

src/pages/api/auth.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ import type { APIRoute } from 'astro';
22

33
export const prerender = false;
44

5-
export const POST: APIRoute = async ({ request, cookies, redirect }) => {
5+
export const POST: APIRoute = async ({ request, cookies, redirect, locals }) => {
6+
// Access Cloudflare runtime env vars
7+
const runtime = locals.runtime as { env: Record<string, string> };
8+
const SUBMIT_PASSWORD = runtime?.env?.SUBMIT_PASSWORD || import.meta.env.SUBMIT_PASSWORD;
9+
const SUBMIT_SECRET = runtime?.env?.SUBMIT_SECRET || import.meta.env.SUBMIT_SECRET;
10+
611
const formData = await request.formData();
712
const password = formData.get('password');
813

9-
if (password === import.meta.env.SUBMIT_PASSWORD) {
10-
cookies.set('skill-submit-token', import.meta.env.SUBMIT_SECRET, {
14+
if (password === SUBMIT_PASSWORD) {
15+
cookies.set('skill-submit-token', SUBMIT_SECRET, {
1116
httpOnly: true,
1217
secure: true,
1318
sameSite: 'lax',

src/pages/api/submit.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { Octokit } from '@octokit/rest';
33

44
export const prerender = false;
55

6-
export const POST: APIRoute = async ({ request, cookies }) => {
6+
export const POST: APIRoute = async ({ request, cookies, locals }) => {
7+
// Access Cloudflare runtime env vars
8+
const runtime = locals.runtime as { env: Record<string, string> };
9+
const SUBMIT_SECRET = runtime?.env?.SUBMIT_SECRET || import.meta.env.SUBMIT_SECRET;
10+
const GITHUB_TOKEN = runtime?.env?.GITHUB_TOKEN || import.meta.env.GITHUB_TOKEN;
11+
712
// Verify auth cookie
8-
if (cookies.get('skill-submit-token')?.value !== import.meta.env.SUBMIT_SECRET) {
13+
if (cookies.get('skill-submit-token')?.value !== SUBMIT_SECRET) {
914
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
1015
status: 401,
1116
headers: { 'Content-Type': 'application/json' }
@@ -46,7 +51,7 @@ export const POST: APIRoute = async ({ request, cookies }) => {
4651
}
4752

4853
// Create PR via GitHub API
49-
const octokit = new Octokit({ auth: import.meta.env.GITHUB_TOKEN });
54+
const octokit = new Octokit({ auth: GITHUB_TOKEN });
5055
const owner = 'EveryInc';
5156
const repo = 'everyskill';
5257
const branch = `skill/${name}-${Date.now()}`;

src/pages/submit.astro

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ import Base from '../layouts/Base.astro';
33
44
export const prerender = false;
55
6+
// Access Cloudflare runtime env vars
7+
const runtime = Astro.locals.runtime as { env: Record<string, string> } | undefined;
8+
const SUBMIT_SECRET = runtime?.env?.SUBMIT_SECRET || import.meta.env.SUBMIT_SECRET;
9+
610
// Check auth cookie
711
const authToken = Astro.cookies.get('skill-submit-token')?.value;
8-
const isAuthed = authToken === import.meta.env.SUBMIT_SECRET;
12+
const isAuthed = authToken === SUBMIT_SECRET;
913
1014
// Handle auth error from redirect
1115
const authError = Astro.url.searchParams.get('error');

0 commit comments

Comments
 (0)