From 0ba1d41881cd738ae860a9705b5a5c54e8bc36fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Tue, 7 Nov 2023 05:01:16 +0100 Subject: [PATCH] docs: add proxy example (#9077) * chore: add Preview Deployment example * simplify package.json * chore(examples): bump to `next-auth@beta` --- apps/proxy/.env.example | 8 ++++++++ apps/proxy/.gitignore | 15 +++++++++++++++ apps/proxy/README.md | 1 + apps/proxy/api/[auth].ts | 27 +++++++++++++++++++++++++++ apps/proxy/api/callback/[auth].ts | 2 ++ apps/proxy/package.json | 9 +++++++++ apps/proxy/tsconfig.json | 21 +++++++++++++++++++++ 7 files changed, 83 insertions(+) create mode 100644 apps/proxy/.env.example create mode 100644 apps/proxy/.gitignore create mode 100644 apps/proxy/README.md create mode 100644 apps/proxy/api/[auth].ts create mode 100644 apps/proxy/api/callback/[auth].ts create mode 100644 apps/proxy/package.json create mode 100644 apps/proxy/tsconfig.json diff --git a/apps/proxy/.env.example b/apps/proxy/.env.example new file mode 100644 index 0000000000..af469ab360 --- /dev/null +++ b/apps/proxy/.env.example @@ -0,0 +1,8 @@ +VERCEL="1" +# https://vercel.com/changelog/corepack-experimental-is-now-available +ENABLE_EXPERIMENTAL_COREPACK="1" + +# https://authjs.dev/getting-started/deployment#securing-a-preview-deployment +AUTH_SECRET= # openssl rand -base64 64 +AUTH_GITHUB_ID= +AUTH_GITHUB_SECRET= \ No newline at end of file diff --git a/apps/proxy/.gitignore b/apps/proxy/.gitignore new file mode 100644 index 0000000000..a8c7d2e355 --- /dev/null +++ b/apps/proxy/.gitignore @@ -0,0 +1,15 @@ +# dependencies +/node_modules + +# misc +.DS_Store +*.pem + +# local env files +.env*.local + +# vercel +.vercel + +# typescript +*.tsbuildinfo diff --git a/apps/proxy/README.md b/apps/proxy/README.md new file mode 100644 index 0000000000..1333ed77b7 --- /dev/null +++ b/apps/proxy/README.md @@ -0,0 +1 @@ +TODO diff --git a/apps/proxy/api/[auth].ts b/apps/proxy/api/[auth].ts new file mode 100644 index 0000000000..bdfa8e0abc --- /dev/null +++ b/apps/proxy/api/[auth].ts @@ -0,0 +1,27 @@ +import { Auth, AuthConfig } from "@auth/core" +import GitHub from "@auth/core/providers/github" + +export default function handler(req: Request) { + return Auth(req, getDefaults({ providers: [GitHub] })) +} + +export const config = { runtime: "edge" } + +export function getDefaults(config: AuthConfig) { + config.secret ??= process.env.AUTH_SECRET + config.trustHost ??= !!process.env.VERCEL + config.redirectProxyUrl ??= process.env.AUTH_REDIRECT_PROXY_URL + config.providers = config.providers.map((p) => { + const finalProvider = typeof p === "function" ? p() : p + if (finalProvider.type === "oauth" || finalProvider.type === "oidc") { + const ID = finalProvider.id.toUpperCase() + finalProvider.clientId ??= process.env[`AUTH_${ID}_ID`] + finalProvider.clientSecret ??= process.env[`AUTH_${ID}_SECRET`] + if (finalProvider.type === "oidc") { + finalProvider.issuer ??= process.env[`AUTH_${ID}_ISSUER`] + } + } + return finalProvider + }) + return config +} diff --git a/apps/proxy/api/callback/[auth].ts b/apps/proxy/api/callback/[auth].ts new file mode 100644 index 0000000000..182b360e66 --- /dev/null +++ b/apps/proxy/api/callback/[auth].ts @@ -0,0 +1,2 @@ +export { default } from "../[auth].js" +export const config = { runtime: "edge" } diff --git a/apps/proxy/package.json b/apps/proxy/package.json new file mode 100644 index 0000000000..03c18004f8 --- /dev/null +++ b/apps/proxy/package.json @@ -0,0 +1,9 @@ +{ + "name": "proxy", + "description": "Proxy for Auth.js hosted examples", + "private": true, + "type": "module", + "dependencies": { + "@auth/core": "latest" + } +} diff --git a/apps/proxy/tsconfig.json b/apps/proxy/tsconfig.json new file mode 100644 index 0000000000..8ff03a7adb --- /dev/null +++ b/apps/proxy/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "target": "ES2020", + "strict": true, + "noEmit": true, + "esModuleInterop": true, + "module": "NodeNext", + "moduleResolution": "NodeNext", + "resolveJsonModule": true, + "isolatedModules": true, + "jsx": "preserve", + "incremental": true, + "skipLibCheck": true + }, + "include": [ + "**/*.ts", + ], + "exclude": [ + "node_modules", + ] +} \ No newline at end of file