|
| 1 | +<p align="center"> |
| 2 | + <a href="https://clerk.com?utm_source=github&utm_medium=clerk_hono" target="_blank" rel="noopener noreferrer"> |
| 3 | + <picture> |
| 4 | + <source media="(prefers-color-scheme: dark)" srcset="https://images.clerk.com/static/logo-dark-mode-400x400.png"> |
| 5 | + <img src="https://images.clerk.com/static/logo-light-mode-400x400.png" height="64"> |
| 6 | + </picture> |
| 7 | + </a> |
| 8 | + <br /> |
| 9 | +</p> |
| 10 | + |
| 11 | +# @clerk/hono |
| 12 | + |
| 13 | +<div align="center"> |
| 14 | + |
| 15 | +[](https://clerk.com/discord) |
| 16 | +[](https://clerk.com/docs?utm_source=github&utm_medium=clerk_hono) |
| 17 | +[](https://twitter.com/intent/follow?screen_name=ClerkDev) |
| 18 | + |
| 19 | +[Changelog](https://github.com/clerk/javascript/blob/main/packages/hono/CHANGELOG.md) |
| 20 | +· |
| 21 | +[Report a Bug](https://github.com/clerk/javascript/issues/new?assignees=&labels=needs-triage&projects=&template=BUG_REPORT.yml) |
| 22 | +· |
| 23 | +[Request a Feature](https://feedback.clerk.com/roadmap) |
| 24 | +· |
| 25 | +[Get help](https://clerk.com/contact/support?utm_source=github&utm_medium=clerk_hono) |
| 26 | + |
| 27 | +</div> |
| 28 | + |
| 29 | +## Getting Started |
| 30 | + |
| 31 | +[Clerk](https://clerk.com/?utm_source=github&utm_medium=clerk_hono) is the easiest way to add authentication and user management to your Hono application. Add sign up, sign in, and profile management to your application in minutes. |
| 32 | + |
| 33 | +### Prerequisites |
| 34 | + |
| 35 | +- Hono 4+ |
| 36 | +- Node.js 20+ |
| 37 | + |
| 38 | +### Installation |
| 39 | + |
| 40 | +```sh |
| 41 | +npm install @clerk/hono |
| 42 | +``` |
| 43 | + |
| 44 | +### Configuration |
| 45 | + |
| 46 | +Set your Clerk API keys as environment variables: |
| 47 | + |
| 48 | +```sh |
| 49 | +CLERK_SECRET_KEY=sk_**** |
| 50 | +CLERK_PUBLISHABLE_KEY=pk_**** |
| 51 | +``` |
| 52 | + |
| 53 | +### Usage |
| 54 | + |
| 55 | +```typescript |
| 56 | +import { Hono } from 'hono'; |
| 57 | +import { clerkMiddleware, getAuth } from '@clerk/hono'; |
| 58 | + |
| 59 | +const app = new Hono(); |
| 60 | + |
| 61 | +// Apply Clerk middleware to all routes |
| 62 | +app.use('*', clerkMiddleware()); |
| 63 | + |
| 64 | +// Public route |
| 65 | +app.get('/', c => { |
| 66 | + return c.json({ message: 'Hello!' }); |
| 67 | +}); |
| 68 | + |
| 69 | +// Protected route |
| 70 | +app.get('/protected', c => { |
| 71 | + const { userId } = getAuth(c); |
| 72 | + |
| 73 | + if (!userId) { |
| 74 | + return c.json({ error: 'Unauthorized' }, 401); |
| 75 | + } |
| 76 | + |
| 77 | + return c.json({ message: 'Hello authenticated user!', userId }); |
| 78 | +}); |
| 79 | + |
| 80 | +export default app; |
| 81 | +``` |
| 82 | + |
| 83 | +### Accessing the Clerk Client |
| 84 | + |
| 85 | +You can access the Clerk Backend API client directly from the context: |
| 86 | + |
| 87 | +```typescript |
| 88 | +app.get('/user/:id', async c => { |
| 89 | + const clerkClient = c.get('clerk'); |
| 90 | + const user = await clerkClient.users.getUser(c.req.param('id')); |
| 91 | + return c.json({ user }); |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +### Using `acceptsToken` for Machine Auth |
| 96 | + |
| 97 | +```typescript |
| 98 | +app.get('/api', c => { |
| 99 | + const auth = getAuth(c, { acceptsToken: 'api_key' }); |
| 100 | + |
| 101 | + if (!auth.userId) { |
| 102 | + return c.json({ error: 'Unauthorized' }, 401); |
| 103 | + } |
| 104 | + |
| 105 | + return c.json({ message: 'API access granted' }); |
| 106 | +}); |
| 107 | +``` |
| 108 | + |
| 109 | +### Webhook Verification |
| 110 | + |
| 111 | +```typescript |
| 112 | +import { Hono } from 'hono'; |
| 113 | +import { verifyWebhook } from '@clerk/hono/webhooks'; |
| 114 | + |
| 115 | +const app = new Hono(); |
| 116 | + |
| 117 | +app.post('/webhooks/clerk', async c => { |
| 118 | + const evt = await verifyWebhook(c); |
| 119 | + |
| 120 | + switch (evt.type) { |
| 121 | + case 'user.created': |
| 122 | + console.log('User created:', evt.data.id); |
| 123 | + break; |
| 124 | + // Handle other event types... |
| 125 | + } |
| 126 | + |
| 127 | + return c.json({ received: true }); |
| 128 | +}); |
| 129 | +``` |
| 130 | + |
| 131 | +## Support |
| 132 | + |
| 133 | +You can get in touch with us in any of the following ways: |
| 134 | + |
| 135 | +- Join our official community [Discord server](https://clerk.com/discord) |
| 136 | +- On [our support page](https://clerk.com/contact/support?utm_source=github&utm_medium=clerk_hono) |
| 137 | + |
| 138 | +## Contributing |
| 139 | + |
| 140 | +We're open to all community contributions! If you'd like to contribute in any way, please read [our contribution guidelines](https://github.com/clerk/javascript/blob/main/docs/CONTRIBUTING.md) and [code of conduct](https://github.com/clerk/javascript/blob/main/docs/CODE_OF_CONDUCT.md). |
| 141 | + |
| 142 | +## Security |
| 143 | + |
| 144 | +`@clerk/hono` follows good practices of security, but 100% security cannot be assured. |
| 145 | + |
| 146 | +`@clerk/hono` is provided **"as is"** without any **warranty**. Use at your own risk. |
| 147 | + |
| 148 | +_For more information and to report security issues, please refer to our [security documentation](https://github.com/clerk/javascript/blob/main/docs/SECURITY.md)._ |
| 149 | + |
| 150 | +## License |
| 151 | + |
| 152 | +This project is licensed under the **MIT license**. |
| 153 | + |
| 154 | +See [LICENSE](https://github.com/clerk/javascript/blob/main/packages/hono/LICENSE) for more information. |
0 commit comments