Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pwd to zk-socials demo #45

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions zk-socials/src/app/robots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { MetadataRoute } from 'next'

export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: '*',
disallow: '/',
},
}
}
2 changes: 2 additions & 0 deletions zk-socials/src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const env = createEnv({
BONSAI_API_KEY: z.string(),
IMAGE_ID: z.string(),
NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
PASSWORD: z.string().optional(),
},

/**
Expand All @@ -36,6 +37,7 @@ const env = createEnv({
BONSAI_API_KEY: process.env.BONSAI_API_KEY,
IMAGE_ID: process.env.IMAGE_ID,
NODE_ENV: process.env.NODE_ENV,
PASSWORD: process.env.PASSWORD,
},
/**
* Makes it so that empty strings are treated as undefined.
Expand Down
35 changes: 35 additions & 0 deletions zk-socials/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import env from "./env"

export function middleware(request: NextRequest) {
if (env.NODE_ENV === 'development') {
// disable auth in dev
return NextResponse.next()
}

const basicAuth = request.headers.get('authorization')

if (basicAuth) {
const authParts = basicAuth.split(' ')
if (authParts.length === 2) {
const auth = authParts[1]

if (typeof auth === 'string') {
const decodedAuth = atob(auth)
const [user, pwd] = decodedAuth.split(':')

if (user === 'root' && pwd === env.PASSWORD) {
return NextResponse.next()
}
}
}
}

return new Response('Authentication required', {
status: 401,
headers: {
'WWW-Authenticate': 'Basic realm="Enter root as username and password"',
},
})
}