A simple Next.js app that shows how to add secure sign-in with Scalekit (OIDC). You can use it as a starting point or as a reference to integrate enterprise-grade authentication.
What this example includes:
- The app signs users in with Scalekit using the OpenID Connect (OIDC) authorization flow.
- The
/dashboardpage is protected and redirects unauthenticated users to the login flow. - The configuration shows how to register an OAuth 2.0 client and wire login, callback, and logout endpoints.
- The pages use Bootstrap classes so pages render well on desktop and mobile.
- After login, the dashboard displays selected ID token claims to demonstrate how to access user information.
- Node.js 18.0 or later is installed.
- npm or yarn is installed.
- You have a Scalekit account with an OIDC application. Sign up
Pick one method below.
Method A — .env.local file (recommended for local dev):
Create or update .env.local in the project root:
# Replace placeholders with your values
SCALEKIT_ENV_URL=https://your-env.scalekit.io
SCALEKIT_CLIENT_ID=YOUR_CLIENT_ID
SCALEKIT_CLIENT_SECRET=YOUR_CLIENT_SECRET
SCALEKIT_REDIRECT_URI=http://localhost:3000/auth/callback
# Optional server config
NEXT_PUBLIC_APP_URL=http://localhost:3000Method B — environment variables:
export SCALEKIT_ENV_URL=https://your-env.scalekit.io
export SCALEKIT_CLIENT_ID=YOUR_CLIENT_ID
export SCALEKIT_CLIENT_SECRET=YOUR_CLIENT_SECRET
export SCALEKIT_REDIRECT_URI=http://localhost:3000/auth/callback
export NEXT_PUBLIC_APP_URL=http://localhost:3000Important:
- Never commit secrets to source control.
- Ensure the redirect URI exactly matches what is configured in Scalekit.
# Install dependencies
npm install
# or
yarn install
# or
pnpm install
# Run the development server
npm run dev
# or
yarn dev
# or
pnpm devThe application will start at http://localhost:3000
To find your required values:
-
Visit Scalekit Dashboard and proceed to Settings
-
Copy the API credentials
- Environment URL (e.g.,
https://your-env.scalekit.dev) - Client ID
- Client Secret
- Environment URL (e.g.,
-
Authentication > Redirect URLs > Allowed redirect URIs:
- Add
http://localhost:3000/auth/callback(no trailing slash) - Optionally add
http://localhost:3000as a post-logout redirect
- Add
| Route | Description | Auth required |
|---|---|---|
/ |
Home page with login option | No |
/login |
Custom login page | No |
/auth/callback |
OIDC callback (API route) | No |
/dashboard |
Protected dashboard | Yes |
/sessions |
Session management | Yes |
/sessions/validate-token |
Validate token (API) | Yes |
/sessions/refresh-token |
Refresh token (API) | Yes |
/organization/settings |
Protected settings page | Yes (permission) |
/logout |
Logout and end session | Yes |
- Start the app (see Quick start)
- Visit
http://localhost:3000 - Click Sign in with Scalekit
- Authenticate with your provider
- Open the dashboard and then try logout
Stuck? Contact us.
nextjs-scalekit-example/
├── app/ # Next.js App Router pages
│ ├── api/ # API routes
│ │ └── auth/ # Authentication API routes
│ │ ├── login/ # Login initiation
│ │ ├── callback/ # OAuth callback handler
│ │ ├── logout/ # Logout handler
│ │ ├── refresh/ # Token refresh
│ │ └── validate/ # Token validation
│ ├── dashboard/ # Dashboard page
│ ├── sessions/ # Session management page
│ ├── organization/ # Organization settings
│ ├── login/ # Login page
│ ├── error/ # Error page
│ └── permission-denied/ # Permission denied page
├── components/ # React components
│ ├── LoginButton.tsx
│ ├── LogoutButton.tsx
│ └── SessionActions.tsx
├── lib/ # Utility libraries
│ ├── scalekit.ts # Scalekit client initialization
│ ├── cookies.ts # Session cookie management
│ └── auth.ts # Authentication utilities
├── package.json # Dependencies
├── tsconfig.json # TypeScript configuration
├── next.config.js # Next.js configuration
└── README.md # This file
- Next.js 14+ (App Router)
- @scalekit-sdk/node (Official Scalekit Node SDK)
- React 18+
- TypeScript
- date-fns (for date formatting)
- js-cookie (for cookie management)
See package.json for exact versions.
This application uses the official Scalekit Node SDK (@scalekit-sdk/node) for all authentication operations:
ScalekitClient.getAuthorizationUrl()- Generate OAuth authorization URLScalekitClient.authenticateWithCode()- Exchange code for tokensScalekitClient.validateToken()- Validate tokens and extract permissionsScalekitClient.refreshAccessToken()- Refresh expired tokensScalekitClient.getLogoutUrl()- Generate logout URL
- Server-Side Authentication: Uses Next.js API routes for secure token handling
- Cookie-Based Sessions: Secure HTTP-only cookies for session storage
- Automatic Token Refresh: Middleware can be added to refresh tokens automatically
- Permission-Based Access Control: Route protection based on token permissions
- TypeScript Support: Full type safety throughout the application
- Read the Scalekit docs: Documentation.
- Read the Next.js docs: Documentation.
This project is for demonstration and learning. Refer to dependency licenses for production use.