adonis-clerk
v1.0.3
Published
Clerk authentication plugin for AdonisJS v6 - Sign in, sign out, and user management
Maintainers
Readme
adonis-clerk
A Clerk authentication plugin for AdonisJS v6. Provides seamless integration with Clerk for handling sign-in, sign-out, and user management in your AdonisJS applications.
Features
- 🔐 Session Token Verification - Verify Clerk session tokens from cookies or Authorization headers
- 🛡️ Middleware Protection - Protect routes with authentication and authorization middleware
- 👤 User Management - Fetch, update, and manage users via the Clerk Backend API
- 🏢 Organization Support - Role-based access control with organizations
- 🔗 Webhook Support - Verify and handle Clerk webhooks
Installation
```bash npm install adonis-clerk @clerk/backend ```
Configuration
Run the configure command to set up the package:
```bash node ace configure adonis-clerk ```
This will:
- Create `config/clerk.ts` configuration file
- Add environment variables to `.env`
- Register the provider in `adonisrc.ts`
Environment Variables
Add your Clerk keys to `.env`:
```env CLERKPUBLISHABLE_KEY=pk_test... CLERKSECRET_KEY=sk_test... ```
Get your keys from the Clerk Dashboard → API Keys.
Usage
Protecting Routes
```typescript import router from "@adonisjs/core/services/router" import app from "@adonisjs/core/services/app" import { clerkMiddleware, clerkRequireAuth } from "adonis-clerk"
// Get clerk service const clerk = await app.container.make("clerk")
// Apply global middleware router.use([clerkMiddleware(clerk)])
// Protect routes router.get("/dashboard", async ({ clerk }) => { return { userId: clerk.userId } }).use(clerkRequireAuth(clerk)) ```
Accessing Auth State
```typescript router.get("/api/me", async ({ clerk, response }) => { if (!clerk.userId) { return response.unauthorized({ error: "Not authenticated" }) } return { userId: clerk.userId, sessionId: clerk.sessionId, } }) ```
Sign Out (Revoke Sessions)
```typescript // Revoke a single session await clerk.revokeSession("sess_xxx")
// Sign out everywhere await clerk.revokeAllUserSessions("user_xxx") ```
API Reference
ClerkService Methods
| Method | Description | | ---------------------------------------- | --------------------------- | | `verifyToken(token)` | Verify a session token | | `getUser(userId)` | Get a user by ID | | `revokeSession(sessionId)` | Revoke a session (sign out) | | `revokeAllUserSessions(userId)` | Sign out everywhere | | `updateUserMetadata(userId, metadata)` | Update user metadata |
Middleware Functions
| Middleware | Description | | ----------------------------------- | ----------------------------------- | | `clerkMiddleware(clerk)` | Attaches auth state to all requests | | `clerkRequireAuth(clerk)` | Requires authentication | | `clerkRequireOrg(clerk, options)` | Requires organization membership | | `clerkLoadUser(clerk)` | Loads full user data |
License
MIT License - see LICENSE.md
