An example Electron app with WorkOS AuthKit authentication using the @workos-inc/node SDK with PKCE.
git clone <repo-url>
cd electron-authkit-example
pnpm installCreate .env:
MAIN_VITE_WORKOS_CLIENT_ID=client_xxx
MAIN_VITE_WORKOS_ENCRYPTION_SECRET=<32+ character secret>Add workos-auth://callback as a redirect URI in your WorkOS Dashboard.
pnpm devElectron apps can't use traditional cookie-based auth flows. This example shows how to implement WorkOS AuthKit authentication using:
- PKCE (Proof Key for Code Exchange) — Secure OAuth flow for public clients that can't safely store a client secret
- Deep link handling — Registers
workos-auth://protocol to receive OAuth callbacks from the system browser - Encrypted session storage — Uses
electron-storewith encryption for secure token persistence
Renderer (React) Main Process
┌──────────────┐ ┌─────────────────────────┐
│ useAuth() │──IPC───▶│ IPC Handlers │
│ signIn() │ │ │ │
│ signOut() │◀─────── │ ▼ │
└──────────────┘ │ auth.ts │
│ (@workos-inc/node) │
│ │ │
│ ▼ │
│ electron-store │
│ (encrypted) │
└─────────────────────────┘
| File | Purpose |
|---|---|
src/main/auth/auth.ts |
PKCE flow, token exchange, session storage, refresh logic |
src/main/auth/deep-link-handler.ts |
Registers workos-auth:// protocol, handles OAuth callback |
src/main/auth/ipc-handlers.ts |
IPC handlers for sign-in, sign-out, get-user |
src/preload/index.ts |
Exposes window.auth API to renderer |
src/renderer/src/hooks/useAuth.ts |
React hook for auth state |
PKCE (Proof Key for Code Exchange) is required for Electron apps because they're "public clients"—the app binary can be decompiled, so a client secret can't be safely embedded.
- Sign-in initiated — App generates a random
code_verifierand derives acode_challenge(SHA256 hash) - Authorization request — User is sent to WorkOS with the
code_challenge; thecode_verifieris stored locally - User authenticates — WorkOS redirects to
workos-auth://callback?code=xxx - Token exchange — App sends the
code+ originalcode_verifierto WorkOS - Verification — WorkOS hashes the
code_verifierand confirms it matches the originalcode_challenge - Tokens issued — Access and refresh tokens are returned and stored encrypted
This ensures that even if an attacker intercepts the authorization code, they can't exchange it without the code_verifier that never left the app.
Token refresh is automatic when calling getUser().
pnpm build:mac # macOS
pnpm build:win # Windows
pnpm build:linux # Linux@workos-inc/node— WorkOS Node.js SDK- WorkOS AuthKit Docs — User management documentation
- PKCE (RFC 7636) — Proof Key for Code Exchange specification
- Electron Deep Links — Protocol handling in Electron