Quickstart: Stack Auth
Setup Stack Auth
git clone https://github.com/stack-auth/stack-auth.git && cd stack-auth
pnpm install && pnpm build:packages && pnpm codegen
pnpm restart-deps && pnpm devProject ID: internal
Publishable Client Key: this-publishable-client-key-is-for-local-development-only
Secret Server Key: this-secret-server-key-is-for-local-development-onlyStep-by-Step
1
uv init --bare
uv add --extra-index-url "https://oauth2accesstoken:$(gcloud auth print-access-token)@glsdk.gdplabs.id/gen-ai-internal/simple/" "gl-iam[fastapi,stackauth]"
uv add python-dotenv uvicorn2
STACKAUTH_BASE_URL=https://api.stack-auth.com
STACKAUTH_PROJECT_ID=your-project-id
STACKAUTH_PUBLISHABLE_CLIENT_KEY=pck_your_key
STACKAUTH_SECRET_SERVER_KEY=ssk_your_keySTACKAUTH_BASE_URL=http://localhost:8102
STACKAUTH_PROJECT_ID=internal
STACKAUTH_PUBLISHABLE_CLIENT_KEY=this-publishable-client-key-is-for-local-development-only
STACKAUTH_SECRET_SERVER_KEY=this-secret-server-key-is-for-local-development-only3
import os
from contextlib import asynccontextmanager
from dotenv import load_dotenv
from fastapi import Depends, FastAPI
from gl_iam import IAMGateway, User
from gl_iam.fastapi import get_current_user, require_org_member, set_iam_gateway
from gl_iam.providers.stackauth import StackAuthConfig, StackAuthProvider
load_dotenv()
@asynccontextmanager
async def lifespan(app: FastAPI):
config = StackAuthConfig(
base_url=os.getenv("STACKAUTH_BASE_URL"),
project_id=os.getenv("STACKAUTH_PROJECT_ID"),
publishable_client_key=os.getenv("STACKAUTH_PUBLISHABLE_CLIENT_KEY"),
secret_server_key=os.getenv("STACKAUTH_SECRET_SERVER_KEY"),
)
provider = StackAuthProvider(config)
gateway = IAMGateway.from_fullstack_provider(provider)
set_iam_gateway(gateway, default_organization_id=os.getenv("STACKAUTH_PROJECT_ID"))
yield
await provider.close()
app = FastAPI(lifespan=lifespan)
@app.get("/me")
async def get_me(user: User = Depends(get_current_user)):
return {"email": user.email, "roles": user.roles}
@app.get("/member-area")
async def member_area(
user: User = Depends(get_current_user),
_: None = Depends(require_org_member()),
):
return {"message": f"Welcome {user.email}!", "access_level": "member"}4
uv run uvicorn main:app --reload5
curl -s -X POST https://api.stack-auth.com/api/v1/auth/password/sign-up \
-H "Content-Type: application/json" \
-H "x-stack-access-type: client" \
-H "x-stack-project-id: $STACKAUTH_PROJECT_ID" \
-H "x-stack-publishable-client-key: $STACKAUTH_PUBLISHABLE_CLIENT_KEY" \
-d '{"email": "test@example.com", "password": "SecurePass123", "verification_callback_url": "http://localhost:3000/verify"}'curl -s -X POST http://localhost:8102/api/v1/auth/password/sign-up \
-H "Content-Type: application/json" \
-H "x-stack-access-type: client" \
-H "x-stack-project-id: $STACKAUTH_PROJECT_ID" \
-H "x-stack-publishable-client-key: $STACKAUTH_PUBLISHABLE_CLIENT_KEY" \
-d '{"email": "test@example.com", "password": "SecurePass123", "verification_callback_url": "http://localhost:3000/verify"}'TOKEN="<paste access_token here>"
curl http://localhost:8000/me -H "Authorization: Bearer $TOKEN"
# {"email":"test@example.com","roles":["admin"]}
curl http://localhost:8000/member-area -H "Authorization: Bearer $TOKEN"
# {"message":"Welcome test@example.com!","access_level":"member"}curl -s -X POST https://api.stack-auth.com/api/v1/auth/password/sign-in \
-H "Content-Type: application/json" \
-H "x-stack-access-type: client" \
-H "x-stack-project-id: $STACKAUTH_PROJECT_ID" \
-H "x-stack-publishable-client-key: $STACKAUTH_PUBLISHABLE_CLIENT_KEY" \
-d '{"email": "test@example.com", "password": "SecurePass123"}'curl -s -X POST http://localhost:8102/api/v1/auth/password/sign-in \
-H "Content-Type: application/json" \
-H "x-stack-access-type: client" \
-H "x-stack-project-id: $STACKAUTH_PROJECT_ID" \
-H "x-stack-publishable-client-key: $STACKAUTH_PUBLISHABLE_CLIENT_KEY" \
-d '{"email": "test@example.com", "password": "SecurePass123"}'How Roles Work
Stack Auth
GL IAM
Passes
Last updated
Was this helpful?