studio-ai-auth
v3.0.10
Published
A Next.js middleware for handling authentication with Keycloak and JWT.
Maintainers
Readme
studio-ai-auth
A reusable Next.js middleware package for authentication and token validation. This package helps enforce authentication across multiple micro-frontends without code duplication.
🚀 Features
👉 JWT Token Decryption & Validation
👉 Automatic Redirection to Authentication if token is missing/invalid
👉 Configurable Public Routes (Define accessible routes without authentication)
👉 Reusable Across Micro-Frontends (Reduce redundant auth logic)
👉 Easy Integration with Next.js Middleware
📛 Installation
Install the package using npm:
npm i studio-ai-author using yarn:
yarn add studio-ai-auth** Environment Variables**
Ensure you have the required environment variables set in your .env.local file:
PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANB...\n-----END PRIVATE KEY-----"
PRIVATE_KEY="-----BEGIN PUBLIC KEY-----\nMIIEvQIBADSZANB...\n-----END PUBLiC KEY-----"
KEYCLOAK_ISSUER="https://keycloak.example.com/auth/realms/my-realm"
KEYCLOAK_CLIENT_ID="my-client-id"🗜️ How It Works?
1️⃣ Extracts JWT from Cookies
2️⃣ Decrypts & Validates token (expiration, issuer, audience)
3️⃣ Allows or Redirects the request based on validation
4️⃣ Handles Public Routes (Allows users to access without authentication)
📀 Example Project Structure
/my-nextjs-app
├── /app
│ ├── page.tsx
│ ├── dashboard -> page.tsx
│ ├── profile -> page.tsx
├── /middleware.ts ← (Integrate middleware here)
├── /env.local ← (Store private key & env variables)
├── /public
├── package.json
├── tsconfig.json🛠️ Troubleshooting
Issue: Cannot find module 'node-jose'
Ensure you have the required dependencies installed:
npm i node-joseIssue: 403 Forbidden - You do not have permission to publish
Check if you're logged into npm:
npm whoamiIf not logged in, run:
npm login📚 License
This project is licensed under the MIT License.
💡 Contributing
Want to improve this package? Open an issue or submit a PR! 😊
📩 Need Help?
If you have any issues, feel free to open an issue.
