Documentation Index
Fetch the complete documentation index at: https://stackauth-e0affa27-apps-support.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Setting up with AI? Use this single prompt:
Choose your tech stack
Choose all that apply.
Select a tool to show setup instructions.
- Unified AI Prompt
- Next.js
- React
- JS/TS
- Tanstack Start
- Node.js
- Bun
- Convex
- Supabase
- CLI
Next.js SDK Setup Instructions
Follow these instructions in order to set up and get started with the Stack Auth SDK for Next.js .Install dependencies
@stackframe/stack npm package with your preferred package manager:npm i @stackframe/stack
# or: pnpm i @stackframe/stack
# or: yarn add @stackframe/stack
# or: bun add @stackframe/stack
Initializing the Stack App
StackClientApp constructor:import { StackClientApp } from "@stackframe/stack";
export const stackClientApp = new StackClientApp({
tokenStore: "cookie", // "nextjs-cookie" for Next.js, "cookie" for other web frontends, null for backend environments
});
StackServerApp, which provides access to more sensitive APIs compared to StackClientApp:import { StackServerApp } from "@stackframe/stack";
import { stackClientApp } from "./client";
export const stackServerApp = new StackServerApp({
inheritsFrom: stackClientApp,
});
Setting up the project
Option 1: Running Stack Auth locally (recommended)
Option 1: Running Stack Auth locally (recommended)
stack.config.ts configuration file in the root directory of the workspace (or anywhere else):import type { StackConfig } from "@stackframe/stack";
// default: show-onboarding, which shows the onboarding flow for this project when Stack Auth starts
export const config: StackConfig = "show-onboarding";
emulator run CLI command does both of these, so you can simply wrap your existing dev script in your package.json:{
// ...
"scripts": {
// ...
"dev": "npx @stackframe/stack-cli emulator run --config-file ./stack.config.ts -- <your-existing-dev-script>"
}
}
Option 2: Connecting a cloud project
Option 2: Connecting a cloud project
Frontend
Go to your project’s dashboard on app.stack-auth.com and get the project ID. You can find it in the URL after the/projects/ part. Copy-paste it into your .env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if available, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
stack/client.ts file:export const stackClientApp = new StackClientApp({
// ...
projectId: "your-project-id",
});
Backend (or both frontend and backend)
First, navigate to the Project Keys page in the Stack Auth dashboard and generate a new set of keys.Then, copy-paste them into your.env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if desired, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
STACK_SECRET_SERVER_KEY=<your-secret-server-key>
StackServerApp constructor.Creating a <StackProvider /> and <StackTheme />
StackProvider and StackTheme components that should wrap your entire app at the root level.You can do this in the layout.tsx file in the app directory:import { Suspense } from "react";
import { StackProvider, StackTheme } from "@stackframe/stack";
import { stackServerApp } from "@/stack/server";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<StackProvider app={stackServerApp}>
<StackTheme>
{children}
</StackTheme>
</StackProvider>
);
}
Add Suspense boundary
useXyz React hooks for getXyz/listXyz functions. For example, useUser is like getUser, but as a suspending React hook.To support the suspension, you need to add a suspense boundary around your app.In Next.js, this can be easily done by adding a loading.tsx file in the app directory:export default function Loading() {
return <div>Loading...</div>;
}
React SDK Setup Instructions
Follow these instructions in order to set up and get started with the Stack Auth SDK for React .Install dependencies
@stackframe/react npm package with your preferred package manager:npm i @stackframe/react
# or: pnpm i @stackframe/react
# or: yarn add @stackframe/react
# or: bun add @stackframe/react
Initializing the Stack App
StackClientApp constructor:import { StackClientApp } from "@stackframe/react";
export const stackClientApp = new StackClientApp({
tokenStore: "cookie", // "nextjs-cookie" for Next.js, "cookie" for other web frontends, null for backend environments
});
Setting up the project
Option 1: Running Stack Auth locally (recommended)
Option 1: Running Stack Auth locally (recommended)
stack.config.ts configuration file in the root directory of the workspace (or anywhere else):import type { StackConfig } from "@stackframe/react";
// default: show-onboarding, which shows the onboarding flow for this project when Stack Auth starts
export const config: StackConfig = "show-onboarding";
emulator run CLI command does both of these, so you can simply wrap your existing dev script in your package.json:{
// ...
"scripts": {
// ...
"dev": "npx @stackframe/stack-cli emulator run --config-file ./stack.config.ts -- <your-existing-dev-script>"
}
}
Option 2: Connecting a cloud project
Option 2: Connecting a cloud project
Frontend
Go to your project’s dashboard on app.stack-auth.com and get the project ID. You can find it in the URL after the/projects/ part. Copy-paste it into your .env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if available, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
stack/client.ts file:export const stackClientApp = new StackClientApp({
// ...
projectId: "your-project-id",
});
Backend (or both frontend and backend)
First, navigate to the Project Keys page in the Stack Auth dashboard and generate a new set of keys.Then, copy-paste them into your.env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if desired, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
STACK_SECRET_SERVER_KEY=<your-secret-server-key>
StackServerApp constructor.Creating a <StackProvider /> and <StackTheme />
StackProvider and StackTheme components that should wrap your entire app at the root level.For example, if you have an App.tsx file, update it as follows:import { StackProvider, StackTheme } from "@stackframe/react";
import { stackClientApp } from "./stack/client";
export default function App() {
return (
<StackProvider app={stackClientApp}>
<StackTheme>
{/* your app content */}
</StackTheme>
</StackProvider>
);
}
Add Suspense boundary
useXyz React hooks for getXyz/listXyz functions. For example, useUser is like getUser, but as a suspending React hook.To support the suspension, you need to add a suspense boundary around your app.The easiest way to do this is to just wrap your entire app in a Suspense component:import { Suspense } from "react";
import { StackProvider, StackTheme } from "@stackframe/react";
import { stackClientApp } from "./stack/client";
export default function App() {
return (
<Suspense fallback={<div>Loading...</div>}>
<StackProvider app={stackClientApp}>
<StackTheme>
{/* your app content */}
</StackTheme>
</StackProvider>
</Suspense>
);
}
Other JS/TS SDK Setup Instructions
Follow these instructions in order to set up and get started with the Stack Auth SDK for Other JS/TS .Install dependencies
@stackframe/js npm package with your preferred package manager:npm i @stackframe/js
# or: pnpm i @stackframe/js
# or: yarn add @stackframe/js
# or: bun add @stackframe/js
Initializing the Stack App
StackClientApp constructor:import { StackClientApp } from "@stackframe/js";
export const stackClientApp = new StackClientApp({
tokenStore: "cookie", // "nextjs-cookie" for Next.js, "cookie" for other web frontends, null for backend environments
});
StackServerApp, which provides access to more sensitive APIs compared to StackClientApp:import { StackServerApp } from "@stackframe/js";
export const stackServerApp = new StackServerApp({
tokenStore: null,
});
StackServerApp from a StackClientApp object:import { StackServerApp } from "@stackframe/js";
import { stackClientApp } from "./client";
export const stackServerApp = new StackServerApp({
inheritsFrom: stackClientApp,
});
Setting up the project
Option 1: Running Stack Auth locally (recommended)
Option 1: Running Stack Auth locally (recommended)
stack.config.ts configuration file in the root directory of the workspace (or anywhere else):import type { StackConfig } from "@stackframe/js";
// default: show-onboarding, which shows the onboarding flow for this project when Stack Auth starts
export const config: StackConfig = "show-onboarding";
emulator run CLI command does both of these, so you can simply wrap your existing dev script in your package.json:{
// ...
"scripts": {
// ...
"dev": "npx @stackframe/stack-cli emulator run --config-file ./stack.config.ts -- <your-existing-dev-script>"
}
}
Option 2: Connecting a cloud project
Option 2: Connecting a cloud project
Frontend
Go to your project’s dashboard on app.stack-auth.com and get the project ID. You can find it in the URL after the/projects/ part. Copy-paste it into your .env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if available, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
stack/client.ts file:export const stackClientApp = new StackClientApp({
// ...
projectId: "your-project-id",
});
Backend (or both frontend and backend)
First, navigate to the Project Keys page in the Stack Auth dashboard and generate a new set of keys.Then, copy-paste them into your.env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if desired, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
STACK_SECRET_SERVER_KEY=<your-secret-server-key>
StackServerApp constructor.Backend: Update callers with header & get user
stackClientApp.getAuthorizationHeader() as the Authorization header into your backend endpoints when the user is signed in:// NOTE: This is your frontend's code
const authorizationHeader = await stackClientApp.getAuthorizationHeader();
const response = await fetch("/my-backend-endpoint", {
headers: {
...(authorizationHeader ? { Authorization: authorizationHeader } : {}),
},
});
// ...
tokenStore of the functions that access the user object:// NOTE: This is your backend's code
const user = await stackServerApp.getUser({ tokenStore: request });
return new Response("Hello, " + user.displayName, { headers: { "Cache-Control": "private, no-store" } });
request is an object that follows the shape { headers: Record<string, string | null> | { get: (name: string) => string | null } }.
Make sure that HTTP caching is disabled with Cache-Control: private, no-store for authenticated backend endpoints.
If you cannot use getAuthorizationHeader(), for example because you are using a protocol other than HTTP, you can use getAuthJson() instead:// Frontend:
await rpcCall("my-rpc-endpoint", {
data: {
auth: await stackClientApp.getAuthJson(),
},
});
// Backend:
const user = await stackServerApp.getUser({ tokenStore: data.auth });
return new RpcResponse("Hello, " + user.displayName);
Tanstack Start SDK Setup Instructions
Follow these instructions in order to set up and get started with the Stack Auth SDK for Tanstack Start .Install dependencies
@stackframe/react npm package with your preferred package manager:npm i @stackframe/react
# or: pnpm i @stackframe/react
# or: yarn add @stackframe/react
# or: bun add @stackframe/react
Initializing the Stack App
StackClientApp constructor:import { StackClientApp } from "@stackframe/react";
export const stackClientApp = new StackClientApp({
tokenStore: "cookie", // "nextjs-cookie" for Next.js, "cookie" for other web frontends, null for backend environments
});
Setting up the project
Option 1: Running Stack Auth locally (recommended)
Option 1: Running Stack Auth locally (recommended)
stack.config.ts configuration file in the root directory of the workspace (or anywhere else):import type { StackConfig } from "@stackframe/react";
// default: show-onboarding, which shows the onboarding flow for this project when Stack Auth starts
export const config: StackConfig = "show-onboarding";
emulator run CLI command does both of these, so you can simply wrap your existing dev script in your package.json:{
// ...
"scripts": {
// ...
"dev": "npx @stackframe/stack-cli emulator run --config-file ./stack.config.ts -- <your-existing-dev-script>"
}
}
Option 2: Connecting a cloud project
Option 2: Connecting a cloud project
Frontend
Go to your project’s dashboard on app.stack-auth.com and get the project ID. You can find it in the URL after the/projects/ part. Copy-paste it into your .env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if available, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
stack/client.ts file:export const stackClientApp = new StackClientApp({
// ...
projectId: "your-project-id",
});
Backend (or both frontend and backend)
First, navigate to the Project Keys page in the Stack Auth dashboard and generate a new set of keys.Then, copy-paste them into your.env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if desired, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
STACK_SECRET_SERVER_KEY=<your-secret-server-key>
StackServerApp constructor.Creating a <StackProvider /> and <StackTheme />
StackProvider and StackTheme components that should wrap your entire app at the root level.For example, if you have an App.tsx file, update it as follows:import { StackProvider, StackTheme } from "@stackframe/react";
import { stackClientApp } from "./stack/client";
export default function App() {
return (
<StackProvider app={stackClientApp}>
<StackTheme>
{/* your app content */}
</StackTheme>
</StackProvider>
);
}
Add Suspense boundary
useXyz React hooks for getXyz/listXyz functions. For example, useUser is like getUser, but as a suspending React hook.To support the suspension, you need to add a suspense boundary around your app.The easiest way to do this is to just wrap your entire app in a Suspense component:import { Suspense } from "react";
import { StackProvider, StackTheme } from "@stackframe/react";
import { stackClientApp } from "./stack/client";
export default function App() {
return (
<Suspense fallback={<div>Loading...</div>}>
<StackProvider app={stackClientApp}>
<StackTheme>
{/* your app content */}
</StackTheme>
</StackProvider>
</Suspense>
);
}
Node.js SDK Setup Instructions
Follow these instructions in order to set up and get started with the Stack Auth SDK for Node.js .Install dependencies
@stackframe/js npm package with your preferred package manager:npm i @stackframe/js
# or: pnpm i @stackframe/js
# or: yarn add @stackframe/js
# or: bun add @stackframe/js
Initializing the Stack App
StackServerApp, which provides access to more sensitive APIs compared to StackClientApp:import { StackServerApp } from "@stackframe/js";
export const stackServerApp = new StackServerApp({
tokenStore: null,
});
Setting up the project
Option 1: Running Stack Auth locally (recommended)
Option 1: Running Stack Auth locally (recommended)
stack.config.ts configuration file in the root directory of the workspace (or anywhere else):import type { StackConfig } from "@stackframe/js";
// default: show-onboarding, which shows the onboarding flow for this project when Stack Auth starts
export const config: StackConfig = "show-onboarding";
emulator run CLI command does both of these, so you can simply wrap your existing dev script in your package.json:{
// ...
"scripts": {
// ...
"dev": "npx @stackframe/stack-cli emulator run --config-file ./stack.config.ts -- <your-existing-dev-script>"
}
}
Option 2: Connecting a cloud project
Option 2: Connecting a cloud project
Frontend
Go to your project’s dashboard on app.stack-auth.com and get the project ID. You can find it in the URL after the/projects/ part. Copy-paste it into your .env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if available, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
stack/client.ts file:export const stackClientApp = new StackClientApp({
// ...
projectId: "your-project-id",
});
Backend (or both frontend and backend)
First, navigate to the Project Keys page in the Stack Auth dashboard and generate a new set of keys.Then, copy-paste them into your.env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if desired, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
STACK_SECRET_SERVER_KEY=<your-secret-server-key>
StackServerApp constructor.Update callers with header & get user
stackClientApp.getAuthorizationHeader() as the Authorization header into your backend endpoints when the user is signed in:// NOTE: This is your frontend's code
const authorizationHeader = await stackClientApp.getAuthorizationHeader();
const response = await fetch("/my-backend-endpoint", {
headers: {
...(authorizationHeader ? { Authorization: authorizationHeader } : {}),
},
});
// ...
tokenStore of the functions that access the user object:// NOTE: This is your backend's code
const user = await stackServerApp.getUser({ tokenStore: request });
return new Response("Hello, " + user.displayName, { headers: { "Cache-Control": "private, no-store" } });
request is an object that follows the shape { headers: Record<string, string | null> | { get: (name: string) => string | null } }.
Make sure that HTTP caching is disabled with Cache-Control: private, no-store for authenticated backend endpoints.
If you cannot use getAuthorizationHeader(), for example because you are using a protocol other than HTTP, you can use getAuthJson() instead:// Frontend:
await rpcCall("my-rpc-endpoint", {
data: {
auth: await stackClientApp.getAuthJson(),
},
});
// Backend:
const user = await stackServerApp.getUser({ tokenStore: data.auth });
return new RpcResponse("Hello, " + user.displayName);
Bun SDK Setup Instructions
Follow these instructions in order to set up and get started with the Stack Auth SDK for Bun .Install dependencies
@stackframe/js npm package with your preferred package manager:npm i @stackframe/js
# or: pnpm i @stackframe/js
# or: yarn add @stackframe/js
# or: bun add @stackframe/js
Initializing the Stack App
StackServerApp, which provides access to more sensitive APIs compared to StackClientApp:import { StackServerApp } from "@stackframe/js";
export const stackServerApp = new StackServerApp({
tokenStore: null,
});
Setting up the project
Option 1: Running Stack Auth locally (recommended)
Option 1: Running Stack Auth locally (recommended)
stack.config.ts configuration file in the root directory of the workspace (or anywhere else):import type { StackConfig } from "@stackframe/js";
// default: show-onboarding, which shows the onboarding flow for this project when Stack Auth starts
export const config: StackConfig = "show-onboarding";
emulator run CLI command does both of these, so you can simply wrap your existing dev script in your package.json:{
// ...
"scripts": {
// ...
"dev": "npx @stackframe/stack-cli emulator run --config-file ./stack.config.ts -- <your-existing-dev-script>"
}
}
Option 2: Connecting a cloud project
Option 2: Connecting a cloud project
Frontend
Go to your project’s dashboard on app.stack-auth.com and get the project ID. You can find it in the URL after the/projects/ part. Copy-paste it into your .env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if available, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
stack/client.ts file:export const stackClientApp = new StackClientApp({
// ...
projectId: "your-project-id",
});
Backend (or both frontend and backend)
First, navigate to the Project Keys page in the Stack Auth dashboard and generate a new set of keys.Then, copy-paste them into your.env.local file (or wherever your environment variables are stored):STACK_PROJECT_ID=<your-project-id> # if desired, prefix with your framework's convention for client-exposed variables (e.g. NEXT_PUBLIC_, VITE_, etc.)
STACK_SECRET_SERVER_KEY=<your-secret-server-key>
StackServerApp constructor.Update callers with header & get user
stackClientApp.getAuthorizationHeader() as the Authorization header into your backend endpoints when the user is signed in:// NOTE: This is your frontend's code
const authorizationHeader = await stackClientApp.getAuthorizationHeader();
const response = await fetch("/my-backend-endpoint", {
headers: {
...(authorizationHeader ? { Authorization: authorizationHeader } : {}),
},
});
// ...
tokenStore of the functions that access the user object:// NOTE: This is your backend's code
const user = await stackServerApp.getUser({ tokenStore: request });
return new Response("Hello, " + user.displayName, { headers: { "Cache-Control": "private, no-store" } });
request is an object that follows the shape { headers: Record<string, string | null> | { get: (name: string) => string | null } }.
Make sure that HTTP caching is disabled with Cache-Control: private, no-store for authenticated backend endpoints.
If you cannot use getAuthorizationHeader(), for example because you are using a protocol other than HTTP, you can use getAuthJson() instead:// Frontend:
await rpcCall("my-rpc-endpoint", {
data: {
auth: await stackClientApp.getAuthJson(),
},
});
// Backend:
const user = await stackServerApp.getUser({ tokenStore: data.auth });
return new RpcResponse("Hello, " + user.displayName);
Convex Setup
Follow these instructions to integrate Stack Auth with Convex.Create or identify the Convex app
npm create convex@latest
npx convex dev
npm run dev
Install and configure Stack Auth
npx @stackframe/stack-cli@latest init
.env.local file.Also add the same Stack Auth environment variables to the Convex deployment environment in the Convex dashboard.Configure Convex auth providers
convex/auth.config.ts:import { getConvexProvidersConfig } from "@stackframe/js";
// or: import { getConvexProvidersConfig } from "@stackframe/react";
// or: import { getConvexProvidersConfig } from "@stackframe/stack";
export default {
providers: getConvexProvidersConfig({
projectId: process.env.STACK_PROJECT_ID, // or process.env.NEXT_PUBLIC_STACK_PROJECT_ID
}),
};
Connect Convex clients to Stack Auth
convexClient.setAuth(stackClientApp.getConvexClientAuth({}));
convexReactClient.setAuth(stackClientApp.getConvexClientAuth({}));
convexHttpClient.setAuth(stackClientApp.getConvexHttpClientAuth({ tokenStore: requestObject }));
Use Stack Auth user data in Convex functions
import { query } from "./_generated/server";
import { stackServerApp } from "../src/stack/server";
export const myQuery = query({
handler: async (ctx, args) => {
const user = await stackServerApp.getPartialUser({ from: "convex", ctx });
return user;
},
});
Supabase Setup
This setup covers Supabase Row Level Security (RLS) with Stack Auth JWTs. It does not sync user data between Supabase and Stack Auth. Use Stack Auth webhooks if you need data sync.
Create Supabase RLS policies
CREATE TABLE data (
id bigint PRIMARY KEY,
text text NOT NULL,
user_id UUID
);
INSERT INTO data (id, text, user_id) VALUES
(1, 'Everyone can see this', NULL),
(2, 'Only authenticated users can see this', NULL),
(3, 'Only user with specific id can see this', NULL);
ALTER TABLE data ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Public read" ON "public"."data" TO public
USING (id = 1);
CREATE POLICY "Authenticated access" ON "public"."data" TO authenticated
USING (id = 2);
CREATE POLICY "User access" ON "public"."data" TO authenticated
USING (id = 3 AND auth.uid() = user_id);
Install Stack Auth and Supabase dependencies
npx create-next-app@latest -e with-supabase stack-supabase
cd stack-supabase
npx @stackframe/stack-cli@latest init
.env.local:NEXT_PUBLIC_SUPABASE_URL=<your-supabase-url>
NEXT_PUBLIC_SUPABASE_ANON_KEY=<your-supabase-anon-key>
SUPABASE_JWT_SECRET=<your-supabase-jwt-secret>
NEXT_PUBLIC_STACK_PROJECT_ID=<your-stack-project-id>
NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY=<your-publishable-client-key>
STACK_SECRET_SERVER_KEY=<your-secret-server-key>
Mint Supabase JWTs from Stack Auth users
'use server';
import { stackServerApp } from "@/stack/server";
import * as jose from "jose";
export const getSupabaseJwt = async () => {
const user = await stackServerApp.getUser();
if (!user) {
return null;
}
const token = await new jose.SignJWT({
sub: user.id,
role: "authenticated",
})
.setProtectedHeader({ alg: "HS256" })
.setIssuedAt()
.setExpirationTime("1h")
.sign(new TextEncoder().encode(process.env.SUPABASE_JWT_SECRET));
return token;
};
Create a Supabase client that uses the Stack Auth JWT
import { createBrowserClient } from "@supabase/ssr";
import { getSupabaseJwt } from "./actions";
export const createSupabaseClient = () => {
return createBrowserClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{ accessToken: async () => await getSupabaseJwt() || "" },
);
};
Fetch Supabase data
'use client';
import { createSupabaseClient } from "@/utils/supabase-client";
import { useStackApp, useUser } from "@stackframe/stack";
import Link from "next/link";
import { useEffect, useState } from "react";
export default function Page() {
const app = useStackApp();
const user = useUser();
const supabase = createSupabaseClient();
const [data, setData] = useState<null | any[]>(null);
useEffect(() => {
supabase.from("data").select().then(({ data }) => setData(data ?? []));
}, []);
const listContent = data === null
? <p>Loading...</p>
: data.length === 0
? <p>No notes found</p>
: data.map((note) => <li key={note.id}>{note.text}</li>);
return (
<div>
{user ? (
<>
<p>You are signed in</p>
<p>User ID: {user.id}</p>
<Link href={app.urls.signOut}>Sign Out</Link>
</>
) : (
<Link href={app.urls.signIn}>Sign In</Link>
)}
<h3>Supabase data</h3>
<ul>{listContent}</ul>
</div>
);
}
CLI Setup
Follow these instructions to authenticate users in a command line application with Stack Auth.Add the CLI auth template
stack_auth_cli_template.py.Example project layout:my-python-app/
├─ main.py
└─ stack_auth_cli_template.py
Prompt the user to log in
prompt_cli_login. It opens the browser, lets the user authenticate, and returns a refresh token.from stack_auth_cli_template import prompt_cli_login
refresh_token = prompt_cli_login(
app_url="https://your-app-url.example.com",
project_id="your-project-id-here",
publishable_client_key="your-publishable-client-key-here",
)
if refresh_token is None:
print("User cancelled the login process. Exiting")
exit(1)
Exchange the refresh token for an access token
def get_access_token(refresh_token):
access_token_response = stack_auth_request(
"post",
"/api/v1/auth/sessions/current/refresh",
headers={
"x-stack-refresh-token": refresh_token,
},
)
return access_token_response["access_token"]
Fetch the current user
def get_user_object(access_token):
return stack_auth_request(
"get",
"/api/v1/users/me",
headers={
"x-stack-access-token": access_token,
},
)
user = get_user_object(get_access_token(refresh_token))
print("The user is logged in as", user["display_name"] or user["primary_email"])