Descope Authentication
Integrate secure, passwordless authentication using Descope Flows and SDKs.
Framework Detection
Detect the user's framework and use the appropriate reference:
| If project has... | Use reference |
|-------------------|---------------|
| next in package.json | references/nextjs.md |
| react (no Next.js) | references/react.md |
| @angular/core in package.json | references/angular.md |
| No frontend framework (plain HTML/JS) | references/vanilla-js.md |
| Native Android app (com.android.application in build.gradle(.kts), not Flutter/React Native) | references/kotlin.md |
| Native iOS app (.xcodeproj/.xcworkspace or iOS Package.swift, not Flutter/React Native) | references/swift.md |
| Python/Node.js backend only | references/backend.md |
Quick Start (all frameworks)
- Get a Project ID. Go in this order and stop at the first step that works:
- Already configured: if the app already has a Descope Project ID (e.g.
NEXT_PUBLIC_DESCOPE_PROJECT_ID,DESCOPE_PROJECT_ID,VITE_DESCOPE_PROJECT_IDin env files), reuse it. Do not create a new project. - Descope MCP Server connected (it exposes a
sessiontool): callsessionwith actionlistProjects, show the user the projects' names and IDs, and use the one they pick. Create a new project only when the user asks for one. Name that project on every later call:projectIdon the*_read/*_writetools,args.projectIdonsessionactions. If thesessiontool lists aselectProjectaction, also select the project with it. - Not connected: lead with the Descope MCP Server, not the Console: tell the user to connect it at
https://mcp.descope.comand say that you will then pick or create the project and configure it for them (other regions and per-client setup: https://docs.descope.com/mcp/mcp-server). The user signs in once in the browser; users without a Descope account can sign up there and get a project. Some clients need a restart to pick up a new MCP server. Once connected, go back to sub-step 2 above (listProjects). - User prefers not to use the MCP: ask for the Project ID from https://app.descope.com/settings/project.
- Already configured: if the app already has a Descope Project ID (e.g.
- Configure the Project ID per the framework reference (e.g.
NEXT_PUBLIC_DESCOPE_PROJECT_ID=<your-id>for Next.js,Descope.setup(...)for Android and iOS) - Follow framework-specific reference
- Verify: After setup, confirm the
<Descope>component renders the login form. Check the browser console — a missing or invalid Project ID produces a clearCould not load flowserror.
When the Descope MCP Server is connected, use it for project configuration (e.g. enabling Google login, MFA settings, editing flows) instead of sending the user to the Console. Before any change, make sure the call targets the app's project: name it in projectId and confirm the ID matches the Project ID the app is configured with. Then get the user's approval for that change. If the session tool lists the selectProject and elevate actions, also select the project before the first change and call session with action elevate after the user approves. If the user declines, skip the change.
Minimal Inline Example (Next.js)
// src/app/login/page.tsx
import { Descope } from '@descope/nextjs-sdk';
export default function LoginPage() {
return (
<Descope
flowId="sign-up-or-in"
onSuccess={(e) => console.log('Authenticated:', e.detail.user)}
onError={(e) => console.error('Auth failed:', e.detail)}
/>
);
}
This shows only the component. A real sign-in page must also handle returning users, as described in the next section: see section 2 of references/nextjs.md.
For React SPA, Android, iOS, or backend-only setups, see the framework-specific references below.
Session Lifecycle (web apps, REQUIRED)
Descope issues a short-lived session token (minutes) and a long-lived refresh token (weeks). Only the browser SDK turns the refresh token into a new session token. An integration that checks only the session token signs users out after every idle period. Every web integration (Next.js, React) must:
- Keep the requested page. When an unauthenticated user is sent to sign-in, pass the page they asked for (e.g.
/sign-in?next=/projects/1) and return them there after sign-in. - Let the SDK renew before showing login. On the sign-in page, wait for
useSession()to finish loading. If the user is authenticated (the refresh token was still valid), send them tonextwithout showing the flow. - Refresh on every page. Call
useSession()in a component rendered on every page. The SDK refreshes only once something calls it, and then also renews an expired token when the user returns to the tab. Before a server action or API call from the client, wait for an expired token to renew. - Allow only in-app
nextpaths. Parsenextwithnew URL(next, base)and accept it only if the origin is unchanged and the path doesn't start with//. Otherwise use/. String checks such asstartsWith('/')are not enough, because browsers strip tabs and newlines from URLs (/%09/evil.com). Anything else is an open redirect.
The framework references show the exact code. Follow them.
Valid Flow IDs (CRITICAL - do not invent others)
| Flow ID | Purpose |
|---------|---------|
| sign-up-or-in | Combined signup/login (RECOMMENDED) |
| sign-up | Registration only |
| sign-in | Login only |
| step-up | MFA step-up authentication |
| update-user | Profile updates, add auth methods |
Authentication Methods
| Method | When to use | |--------|-------------| | OTP (Email/SMS) | Quick verification codes | | Magic Link | Passwordless email links | | Passkeys | Biometric/WebAuthn (most secure) | | OAuth | Social login (Google, GitHub, etc.) | | SSO | Enterprise SAML/OIDC | | Passwords | Traditional auth (not recommended) |
DO NOT (Security Guardrails)
- DO NOT parse JWTs manually - always use SDK's
validateSession() - DO NOT store tokens in localStorage (web), plain SharedPreferences (Android), or UserDefaults (iOS) - SDK handles this securely
- DO NOT invent flow IDs - only use IDs from the table above
- DO NOT skip server-side validation - always validate on backend
- DO NOT expose DESCOPE_MANAGEMENT_KEY in client code
- DO NOT show the login flow before
useSession()finishes loading, and DO NOT send every signed-in user to a fixed page such as/or/dashboard. Return them tonext - DO NOT redirect to a
nextvalue without checking it is an in-app path
References
references/nextjs.md- Next.js App Router & Pages Router integrationreferences/react.md- React SPA integrationreferences/angular.md- Angular integrationreferences/vanilla-js.md- Vanilla JavaScript / plain HTML integrationreferences/kotlin.md- Kotlin / Android integration (Native Flows)references/swift.md- Swift / iOS integration (Native Flows, SwiftUI and UIKit)references/backend.md- Backend session validation

微信扫一扫