stack/docs/code-examples/setup.ts
Madison 8c805a8b8f
[Docs][Util][Content] - refactor docs to single source (#919)
<!--

Make sure you've read the CONTRIBUTING.md guidelines:
https://github.com/stack-auth/stack-auth/blob/dev/CONTRIBUTING.md

-->

Removes Platform selection, moves docs to single /content folder and no
longer gens docs. Only API docs are generated here.

<!-- RECURSEML_SUMMARY:START -->
## High-level PR Summary
This PR makes significant changes to the documentation structure by
removing platform-specific content organization and consolidating docs
into a single `/content` folder. The primary goal is to simplify the
documentation architecture by eliminating the platform-specific routing
(Next.js, React, JavaScript, Python) and instead organizing content by
topic (guides, SDK, components) regardless of platform. The PR removes
platform selection functionality, platform-specific navigation, and the
automatic generation of platform-specific documentation pages. It
introduces a new docs tree filtering system that organizes content by
section rather than by platform. These changes should make the
documentation more maintainable and easier to navigate while focusing on
the content itself rather than platform-specific variations.

⏱️ Estimated Review Time: 30-90 minutes

<details>
<summary>💡 Review Order Suggestion</summary>

| Order | File Path |
|-------|-----------|
| 1 | `docs/package.json` |
| 2 | `docs/src/lib/docs-tree.ts` |
| 3 | `docs/src/lib/navigation-utils.ts` |
| 4 | `docs/src/components/homepage/iconHover.tsx` |
| 5 | `docs/src/components/sdk/overview.tsx` |
| 6 | `docs/src/components/layouts/shared/section-utils.ts` |
| 7 | `docs/src/components/layout/custom-search-dialog.tsx` |
| 8 | `docs/src/app/api/search/route.ts` |
| 9 | `docs/src/app/docs/[[...slug]]/page.tsx` |
| 10 | `docs/src/components/layouts/docs-header-wrapper.tsx` |
| 11 | `docs/src/components/layouts/docs-layout-router.tsx` |
| 12 | `docs/src/components/layouts/docs.tsx` |
| 13 | `package.json` |
</details>



[![Need help? Join our
Discord](https://img.shields.io/badge/Need%20help%3F%20Join%20our%20Discord-5865F2?style=plastic&logo=discord&logoColor=white)](https://discord.gg/n3SsVDAW6U)

<!-- RECURSEML_SUMMARY:END -->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Documentation**
* Added many new guides (auth providers, OAuth, JWT, API keys, emails,
webhooks, orgs/teams, permissions, onboarding, customization), expanded
SDK & component reference pages, examples, and navigation metadata.
* Switched docs to a simpler section-based, platform-agnostic structure
and improved getting-started and production checklists.

* **Developer Experience**
* Enhanced docs UX: improved code-example UI with platform/framework
selectors, theme-aware highlighted code blocks, image zoom, and a
centralized code-sample registry.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Konstantin Wohlwend <n2d4xc@gmail.com>
2025-10-20 12:42:46 -05:00

675 lines
21 KiB
TypeScript

import { CodeExample } from '../lib/code-examples';
export const setupExamples = {
'setup': {
'env-wizard': [
{
language: 'JavaScript',
framework: 'Next.js',
code: `NEXT_PUBLIC_STACK_PROJECT_ID=<your-project-id>
NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY=<your-publishable-client-key>
STACK_SECRET_SERVER_KEY=<your-secret-server-key>`,
highlightLanguage: 'bash',
filename: '.env.local'
},
{
language: 'JavaScript',
framework: 'React',
code: `// Update the values in stack/client.ts created by the wizard
export const stackClientApp = new StackClientApp({
projectId: "your-project-id",
publishableClientKey: "your-publishable-client-key",
tokenStore: "cookie",
});`,
highlightLanguage: 'typescript',
filename: 'stack/client.ts'
},
{
language: 'JavaScript',
framework: 'Vanilla JavaScript',
code: `STACK_PROJECT_ID=<your-project-id>
STACK_PUBLISHABLE_CLIENT_KEY=<your-publishable-client-key>
STACK_SECRET_SERVER_KEY=<your-secret-server-key>`,
highlightLanguage: 'bash',
filename: '.env'
}
] as CodeExample[],
'install-package': [
{ language: 'JavaScript', framework: 'Next.js', code: 'npm install @stackframe/stack', highlightLanguage: 'bash', filename: 'Terminal' },
{ language: 'JavaScript', framework: 'React', code: 'npm install @stackframe/react', highlightLanguage: 'bash', filename: 'Terminal' },
{ language: 'JavaScript', framework: 'Express', code: 'npm install @stackframe/js', highlightLanguage: 'bash', filename: 'Terminal' },
{ language: 'JavaScript', framework: 'Node.js', code: 'npm install @stackframe/js', highlightLanguage: 'bash', filename: 'Terminal' },
{ language: 'Python', framework: 'Django', code: 'pip install requests', highlightLanguage: 'bash', filename: 'Terminal' },
{ language: 'Python', framework: 'FastAPI', code: 'pip install requests', highlightLanguage: 'bash', filename: 'Terminal' },
{ language: 'Python', framework: 'Flask', code: 'pip install requests', highlightLanguage: 'bash', filename: 'Terminal' },
] as CodeExample[],
'env-config': [
{
language: 'JavaScript',
framework: 'Next.js',
code: `NEXT_PUBLIC_STACK_PROJECT_ID=<your-project-id>
NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY=<your-publishable-client-key>
STACK_SECRET_SERVER_KEY=<your-secret-server-key>`,
highlightLanguage: 'bash',
filename: '.env.local'
},
{
language: 'JavaScript',
framework: 'React',
code: `# Store these in environment variables or directly in the client file during development
VITE_STACK_PROJECT_ID=<your-project-id>
VITE_STACK_PUBLISHABLE_CLIENT_KEY=<your-publishable-client-key>`,
highlightLanguage: 'bash',
filename: '.env'
},
{
language: 'JavaScript',
framework: 'Express',
code: `STACK_PROJECT_ID=<your-project-id>
STACK_PUBLISHABLE_CLIENT_KEY=<your-publishable-client-key>
STACK_SECRET_SERVER_KEY=<your-secret-server-key>`,
highlightLanguage: 'bash',
filename: '.env'
},
{
language: 'JavaScript',
framework: 'Node.js',
code: `STACK_PROJECT_ID=<your-project-id>
STACK_PUBLISHABLE_CLIENT_KEY=<your-publishable-client-key>
STACK_SECRET_SERVER_KEY=<your-secret-server-key>`,
highlightLanguage: 'bash',
filename: '.env'
},
{
language: 'Python',
framework: 'Django',
code: `import os
stack_project_id = os.getenv("STACK_PROJECT_ID")
stack_publishable_client_key = os.getenv("STACK_PUBLISHABLE_CLIENT_KEY")
stack_secret_server_key = os.getenv("STACK_SECRET_SERVER_KEY")`,
highlightLanguage: 'python',
filename: 'settings.py'
},
{
language: 'Python', framework: 'FastAPI',
code: `import os
stack_project_id = os.getenv("STACK_PROJECT_ID")
stack_publishable_client_key = os.getenv("STACK_PUBLISHABLE_CLIENT_KEY")
stack_secret_server_key = os.getenv("STACK_SECRET_SERVER_KEY")`,
highlightLanguage: 'python',
filename: 'main.py'
},
{
language: 'Python',
framework: 'Flask',
code: `import os
stack_project_id = os.getenv("STACK_PROJECT_ID")
stack_publishable_client_key = os.getenv("STACK_PUBLISHABLE_CLIENT_KEY")
stack_secret_server_key = os.getenv("STACK_SECRET_SERVER_KEY")`,
highlightLanguage: 'python',
filename: 'app.py'
}
] as CodeExample[],
'stack-config': [
{
language: 'JavaScript',
framework: 'Next.js',
variant: 'server',
code: `import "server-only";
import { StackServerApp } from "@stackframe/stack";
export const stackServerApp = new StackServerApp({
tokenStore: "nextjs-cookie", // storing auth tokens in cookies
});`,
highlightLanguage: 'typescript',
filename: 'stack/server.ts'
},
{
language: 'JavaScript',
framework: 'Next.js',
variant: 'client',
code: `import { StackClientApp } from "@stackframe/stack";
export const stackClientApp = new StackClientApp({
// Environment variables are automatically read
});`,
highlightLanguage: 'typescript',
filename: 'stack/client.ts'
},
{
language: 'JavaScript',
framework: 'React',
code: `import { StackClientApp } from "@stackframe/react";
// If you use React Router, uncomment the next line and the redirectMethod below
// import { useNavigate } from "react-router-dom";
export const stackClientApp = new StackClientApp({
projectId: process.env.VITE_STACK_PROJECT_ID || "your-project-id",
publishableClientKey: process.env.VITE_STACK_PUBLISHABLE_CLIENT_KEY || "your-publishable-client-key",
tokenStore: "cookie",
// redirectMethod: { useNavigate }, // Optional: only if using react-router-dom
});`,
highlightLanguage: 'typescript',
filename: 'stack/client.ts'
},
{
language: 'JavaScript',
framework: 'Express',
variant: 'server',
code: `import { StackServerApp } from "@stackframe/js";
export const stackServerApp = new StackServerApp({
projectId: process.env.STACK_PROJECT_ID,
publishableClientKey: process.env.STACK_PUBLISHABLE_CLIENT_KEY,
secretServerKey: process.env.STACK_SECRET_SERVER_KEY,
tokenStore: "memory",
});`,
highlightLanguage: 'typescript',
filename: 'stack/server.ts'
},
{
language: 'JavaScript',
framework: 'Express',
variant: 'client',
code: `import { StackClientApp } from "@stackframe/js";
export const stackClientApp = new StackClientApp({
projectId: process.env.STACK_PROJECT_ID,
publishableClientKey: process.env.STACK_PUBLISHABLE_CLIENT_KEY,
tokenStore: "cookie",
});`,
highlightLanguage: 'typescript',
filename: 'stack/client.ts'
},
{
language: 'JavaScript',
framework: 'Node.js',
variant: 'server',
code: `import { StackServerApp } from "@stackframe/js";
export const stackServerApp = new StackServerApp({
projectId: process.env.STACK_PROJECT_ID,
publishableClientKey: process.env.STACK_PUBLISHABLE_CLIENT_KEY,
secretServerKey: process.env.STACK_SECRET_SERVER_KEY,
tokenStore: "memory",
});`,
highlightLanguage: 'javascript',
filename: 'stack/server.js'
},
{
language: 'JavaScript',
framework: 'Node.js',
variant: 'client',
code: `import { StackClientApp } from "@stackframe/js";
export const stackClientApp = new StackClientApp({
projectId: process.env.STACK_PROJECT_ID,
publishableClientKey: process.env.STACK_PUBLISHABLE_CLIENT_KEY,
tokenStore: "cookie",
});`,
highlightLanguage: 'javascript',
filename: 'stack/client.js'
},
{
language: 'Python',
framework: 'Django',
code: `import requests
def stack_auth_request(method, endpoint, **kwargs):
res = requests.request(
method,
f'https://api.stack-auth.com/{endpoint}',
headers={
'x-stack-access-type': 'server', # or 'client' if you're only accessing the client API
'x-stack-project-id': stack_project_id,
'x-stack-publishable-client-key': stack_publishable_client_key,
'x-stack-secret-server-key': stack_secret_server_key, # not necessary if access type is 'client'
**kwargs.pop('headers', {}),
},
**kwargs,
)
if res.status_code >= 400:
raise Exception(f"Stack Auth API request failed with {res.status_code}: {res.text}")
return res.json()`,
highlightLanguage: 'python',
filename: 'views.py'
},
{
language: 'Python',
framework: 'FastAPI',
code: `import requests
def stack_auth_request(method, endpoint, **kwargs):
res = requests.request(
method,
f'https://api.stack-auth.com/{endpoint}',
headers={
'x-stack-access-type': 'server', # or 'client' if you're only accessing the client API
'x-stack-project-id': stack_project_id,
'x-stack-publishable-client-key': stack_publishable_client_key,
'x-stack-secret-server-key': stack_secret_server_key, # not necessary if access type is 'client'
**kwargs.pop('headers', {}),
},
**kwargs,
)
if res.status_code >= 400:
raise Exception(f"Stack Auth API request failed with {res.status_code}: {res.text}")
return res.json()`,
highlightLanguage: 'python',
filename: 'main.py'
},
{
language: 'Python',
framework: 'Flask',
code: `import requests
def stack_auth_request(method, endpoint, **kwargs):
res = requests.request(
method,
f'https://api.stack-auth.com/{endpoint}',
headers={
'x-stack-access-type': 'server', # or 'client' if you're only accessing the client API
'x-stack-project-id': stack_project_id,
'x-stack-publishable-client-key': stack_publishable_client_key,
'x-stack-secret-server-key': stack_secret_server_key, # not necessary if access type is 'client'
**kwargs.pop('headers', {}),
},
**kwargs,
)
if res.status_code >= 400:
raise Exception(f"Stack Auth API request failed with {res.status_code}: {res.text}")
return res.json()`,
highlightLanguage: 'python',
filename: 'app.py'
}
] as CodeExample[],
'auth-handlers': [
{
language: 'JavaScript',
framework: 'Next.js',
code: `import { StackHandler } from "@stackframe/stack";
import { stackServerApp } from "@/stack/server";
export default function Handler(props: unknown) {
return <StackHandler fullPage app={stackServerApp} routeProps={props} />;
}`,
highlightLanguage: 'typescript',
filename: 'app/handler/[...stack]/page.tsx'
},
{
language: 'JavaScript',
framework: 'React',
code: `import { StackHandler, StackProvider, StackTheme } from "@stackframe/react";
import { Suspense } from "react";
import { BrowserRouter, Route, Routes, useLocation } from "react-router-dom";
import { stackClientApp } from "./stack/client";
function HandlerRoutes() {
const location = useLocation();
return (
<StackHandler app={stackClientApp} location={location.pathname} fullPage />
);
}
export default function App() {
return (
<Suspense fallback={null}>
<BrowserRouter>
<StackProvider app={stackClientApp}>
<StackTheme>
<Routes>
<Route path="/handler/*" element={<HandlerRoutes />} />
<Route path="/" element={<div>hello world</div>} />
</Routes>
</StackTheme>
</StackProvider>
</BrowserRouter>
</Suspense>
);
}`,
highlightLanguage: 'typescript',
filename: 'App.tsx'
},
{
language: 'JavaScript',
framework: 'Express',
code: `// Express doesn't use built-in handlers
// Use the REST API or integrate with your frontend`,
highlightLanguage: 'typescript',
filename: 'Note'
},
{
language: 'JavaScript',
framework: 'Node.js',
code: `// Node.js doesn't use built-in handlers
// Use the REST API or integrate with your frontend`,
highlightLanguage: 'javascript',
filename: 'Note'
}
] as CodeExample[],
'app-providers': [
{
language: 'JavaScript',
framework: 'Next.js',
code: `import React from "react";
import { StackProvider, StackTheme } from "@stackframe/stack";
import { stackServerApp } from "@/stack/server";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
<StackProvider app={stackServerApp}>
<StackTheme>
{children}
</StackTheme>
</StackProvider>
</body>
</html>
);
}`,
highlightLanguage: 'typescript',
filename: 'app/layout.tsx'
},
{
language: 'JavaScript',
framework: 'React',
code: `// Already shown in the App.tsx example above
// Make sure to wrap your app with StackProvider and StackTheme`,
highlightLanguage: 'typescript',
filename: 'Note'
}
] as CodeExample[],
'loading-boundary': [
{
language: 'JavaScript',
framework: 'Next.js',
code: `export default function Loading() {
// You can use any loading indicator here
return <>
Loading...
</>;
}`,
highlightLanguage: 'typescript',
filename: 'app/loading.tsx'
}
] as CodeExample[],
'suspense-boundary': [
{
language: 'JavaScript',
framework: 'React',
code: `import { Suspense } from "react";
import { StackProvider } from "@stackframe/react";
import { stackClientApp } from "./stack/client";
export default function App() {
return (
// Wrap your StackProvider with Suspense for async hooks to work
<Suspense fallback={<div>Loading...</div>}>
<StackProvider app={stackClientApp}>
{/* Your app content */}
</StackProvider>
</Suspense>
);
}`,
highlightLanguage: 'typescript',
filename: 'App.tsx'
}
] as CodeExample[],
'test-setup': [
{
language: 'JavaScript',
framework: 'Next.js',
code: `# Start your Next.js app
npm run dev
# Navigate to the sign-up page
# http://localhost:3000/handler/sign-up`,
highlightLanguage: 'bash',
filename: 'Terminal'
},
{
language: 'JavaScript',
framework: 'React',
code: `# Start your React app
npm run dev
# Navigate to the sign-up page
# http://localhost:5173/handler/sign-up`,
highlightLanguage: 'bash',
filename: 'Terminal'
},
{
language: 'JavaScript',
framework: 'Express',
code: `# Start your Express server
npm start
# Use the REST API or integrate with your frontend
# Check the REST API documentation for endpoints`,
highlightLanguage: 'bash',
filename: 'Terminal'
},
{
language: 'JavaScript',
framework: 'Node.js',
code: `# Start your Node.js app
node index.js
# Use the REST API or integrate with your frontend
# Check the REST API documentation for endpoints`,
highlightLanguage: 'bash',
filename: 'Terminal'
},
{
language: 'Python',
framework: 'Django',
code: `# Test the Stack Auth API connection
print(stack_auth_request('GET', '/api/v1/projects/current'))
# Start your Django server
python manage.py runserver`,
highlightLanguage: 'python',
filename: 'Terminal'
},
{
language: 'Python',
framework: 'FastAPI',
code: `# Test the Stack Auth API connection
print(stack_auth_request('GET', '/api/v1/projects/current'))
# Start your FastAPI server
uvicorn main:app --reload`,
highlightLanguage: 'python',
filename: 'Terminal'
},
{
language: 'Python',
framework: 'Flask',
code: `# Test the Stack Auth API connection
print(stack_auth_request('GET', '/api/v1/projects/current'))
# Start your Flask server
flask run`,
highlightLanguage: 'python',
filename: 'Terminal'
}
] as CodeExample[],
'basic-usage': [
{
language: 'JavaScript',
framework: 'Next.js',
variant: 'server',
code: `import { stackServerApp } from "@/stack/server";
// In a Server Component or API route
const user = await stackServerApp.getUser();
if (user) {
console.log("User is signed in:", user.displayName);
} else {
console.log("User is not signed in");
}`,
highlightLanguage: 'typescript',
filename: 'Server Component'
},
{
language: 'JavaScript',
framework: 'Next.js',
variant: 'client',
code: `'use client';
import { useUser } from "@stackframe/stack";
export default function MyComponent() {
const user = useUser();
if (user) {
return <div>Hello, {user.displayName}!</div>;
} else {
return <div>Please sign in</div>;
}
}`,
highlightLanguage: 'typescript',
filename: 'Client Component'
},
{
language: 'JavaScript',
framework: 'React',
code: `import { useUser } from "@stackframe/react";
export default function MyComponent() {
const user = useUser();
if (user) {
return <div>Hello, {user.displayName}!</div>;
} else {
return <div>Please sign in</div>;
}
}`,
highlightLanguage: 'typescript',
filename: 'Component'
},
{
language: 'JavaScript',
framework: 'Express',
code: `import { stackServerApp } from "./stack/server.js";
app.get('/profile', async (req, res) => {
try {
// Get access token from request headers
const accessToken = req.headers['x-stack-access-token'];
const user = await stackServerApp.getUser({ accessToken });
if (user) {
res.json({ message: \`Hello, \${user.displayName}!\` });
} else {
res.status(401).json({ error: 'Not authenticated' });
}
} catch (error) {
res.status(500).json({ error: 'Server error' });
}
});`,
highlightLanguage: 'typescript',
filename: 'server.ts'
},
{
language: 'JavaScript',
framework: 'Node.js',
code: `import { stackServerApp } from "./stack/server.js";
async function checkUser(accessToken) {
try {
const user = await stackServerApp.getUser({ accessToken });
if (user) {
console.log(\`Hello, \${user.displayName}!\`);
} else {
console.log('User not authenticated');
}
} catch (error) {
console.error('Error:', error);
}
}`,
highlightLanguage: 'javascript',
filename: 'index.js'
},
{
language: 'Python',
framework: 'Django',
code: `# In your views.py
def profile_view(request):
# Get access token from request headers
access_token = request.headers.get('X-Stack-Access-Token')
try:
user_data = stack_auth_request('GET', '/api/v1/users/me', headers={
'x-stack-access-token': access_token,
})
return JsonResponse({'message': f"Hello, {user_data['displayName']}!"})
except Exception as e:
return JsonResponse({'error': 'Not authenticated'}, status=401)`,
highlightLanguage: 'python',
filename: 'views.py'
},
{
language: 'Python',
framework: 'FastAPI',
code: `from fastapi import FastAPI, Header, HTTPException
app = FastAPI()
@app.get("/profile")
async def get_profile(x_stack_access_token: str = Header(None)):
if not x_stack_access_token:
raise HTTPException(status_code=401, detail="Access token required")
try:
user_data = stack_auth_request('GET', '/api/v1/users/me', headers={
'x-stack-access-token': x_stack_access_token,
})
return {"message": f"Hello, {user_data['displayName']}!"}
except Exception as e:
raise HTTPException(status_code=401, detail="Not authenticated")`,
highlightLanguage: 'python',
filename: 'main.py'
},
{
language: 'Python',
framework: 'Flask',
code: `from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/profile')
def profile():
access_token = request.headers.get('X-Stack-Access-Token')
if not access_token:
return jsonify({'error': 'Access token required'}), 401
try:
user_data = stack_auth_request('GET', '/api/v1/users/me', headers={
'x-stack-access-token': access_token,
})
return jsonify({'message': f"Hello, {user_data['displayName']}!"})
except Exception as e:
return jsonify({'error': 'Not authenticated'}), 401`,
highlightLanguage: 'python',
filename: 'app.py'
}
] as CodeExample[]
}
};