hono-crud 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,8 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ %b
9
+ %b
8
10
  %b
9
11
  %b
10
12
  %b
@@ -32,3 +34,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
32
34
  [0.1.1]: https://github.com/ksh-us/hono-crud/compare/v0.0.0...v0.1.1
33
35
  [0.1.2]: https://github.com/ksh-us/hono-crud/compare/v0.1.1...v0.1.2
34
36
  [0.1.3]: https://github.com/ksh-us/hono-crud/compare/v0.1.2...v0.1.3
37
+ [0.1.4]: https://github.com/kshdotdev/hono-crud/compare/v0.1.3...v0.1.4
38
+ [0.2.0]: https://github.com/kshdotdev/hono-crud/compare/v0.1.4...v0.2.0
package/README.md CHANGED
@@ -111,6 +111,77 @@ OpenAPI documentation is automatically generated. Access it at:
111
111
  - **Scalar**: `/reference`
112
112
  - **OpenAPI JSON**: `/openapi.json`
113
113
 
114
+ ## Authentication with better-auth
115
+
116
+ hono-crud integrates seamlessly with [better-auth](https://www.better-auth.com) for comprehensive authentication.
117
+
118
+ ### Setup
119
+
120
+ 1. **Mount better-auth handler** for authentication routes:
121
+
122
+ ```typescript
123
+ import { Hono } from "hono";
124
+ import { auth } from "./auth"; // your better-auth instance
125
+ import { cors } from "hono/cors";
126
+
127
+ const app = new Hono<{
128
+ Variables: {
129
+ user: typeof auth.$Infer.Session.user | null;
130
+ session: typeof auth.$Infer.Session.session | null;
131
+ };
132
+ }>();
133
+
134
+ // CORS (must be before routes)
135
+ app.use("/api/auth/*", cors({
136
+ origin: "http://localhost:3000",
137
+ credentials: true,
138
+ }));
139
+
140
+ // Mount better-auth
141
+ app.on(["POST", "GET"], "/api/auth/*", (c) => auth.handler(c.req.raw));
142
+ ```
143
+
144
+ 2. **Add session middleware** to inject user into context:
145
+
146
+ ```typescript
147
+ app.use("*", async (c, next) => {
148
+ const session = await auth.api.getSession({ headers: c.req.raw.headers });
149
+ c.set("user", session?.user || null);
150
+ c.set("session", session?.session || null);
151
+ await next();
152
+ });
153
+ ```
154
+
155
+ 3. **Protect CRUD routes** with hono-crud guards:
156
+
157
+ ```typescript
158
+ import { registerCrud, requireAuth, requireRoles } from "hono-crud";
159
+
160
+ registerCrud(app, "/api/users", {
161
+ list: UserList,
162
+ read: UserRead,
163
+ update: UserUpdate,
164
+ delete: UserDelete,
165
+ }, {
166
+ middlewares: [requireAuth()],
167
+ endpointMiddlewares: {
168
+ delete: [requireRoles(["admin"])],
169
+ },
170
+ });
171
+ ```
172
+
173
+ ### Architecture
174
+
175
+ ```
176
+ Request → CORS → Session MW → Auth Guards → CRUD Endpoint
177
+
178
+ better-auth
179
+ (validates session)
180
+ ```
181
+
182
+ - **better-auth** handles: login, signup, OAuth, 2FA, session management
183
+ - **hono-crud** handles: CRUD operations with authorization guards
184
+
114
185
  ## Examples
115
186
 
116
187
  Check out the [examples](./examples) directory for complete working examples: