@authcore/fastify 0.5.1 → 0.5.3

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.
Files changed (2) hide show
  1. package/README.md +70 -0
  2. package/package.json +5 -4
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # @authcore/fastify
2
+
3
+ > Fastify adapter for AuthCore - plugin with auth routes and request hooks.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @authcore/fastify @authcore/prisma-adapter
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import Fastify from 'fastify'
15
+ import { createAuth } from '@authcore/fastify'
16
+ import { prismaAdapter } from '@authcore/prisma-adapter'
17
+ import { PrismaClient } from '@prisma/client'
18
+
19
+ const prisma = new PrismaClient()
20
+ const app = Fastify()
21
+ const auth = createAuth({
22
+ db: prismaAdapter(prisma),
23
+ session: { strategy: 'jwt', secret: process.env.AUTH_SECRET! },
24
+ })
25
+
26
+ // Register auth plugin - adds all auth routes under /auth
27
+ await app.register(auth.plugin(), { prefix: '/auth' })
28
+
29
+ // Protect routes
30
+ app.get('/dashboard', { preHandler: auth.authRequired() }, async (request) => {
31
+ return { user: request.user }
32
+ })
33
+
34
+ // Optional auth
35
+ app.get('/public', { preHandler: auth.authOptional() }, async (request) => {
36
+ return { user: request.user ?? null }
37
+ })
38
+
39
+ await app.listen({ port: 3000 })
40
+ ```
41
+
42
+ ## API
43
+
44
+ ### `createAuth(config)`
45
+
46
+ Creates a Fastify auth instance. See [`@authcore/core`](https://www.npmjs.com/package/@authcore/core) for the full config reference.
47
+
48
+ Returns:
49
+
50
+ - **`auth.plugin(options?)`** - Fastify plugin that registers all auth routes
51
+ - **`auth.authRequired()`** - `preHandler` hook that requires authentication, attaches `request.user`
52
+ - **`auth.authOptional()`** - `preHandler` hook that optionally attaches `request.user`
53
+
54
+ ### Routes
55
+
56
+ Same endpoints as the Express adapter:
57
+
58
+ | Method | Route | Body | Response |
59
+ |--------|-------|------|----------|
60
+ | POST | `/auth/register` | `{ email, password }` | `{ user, token }` |
61
+ | POST | `/auth/login` | `{ email, password }` | `{ user, token }` |
62
+ | POST | `/auth/logout` | - | `{ message }` |
63
+ | GET | `/auth/me` | - | `{ user }` |
64
+ | POST | `/auth/verify-email` | `{ token }` | `{ message }` |
65
+ | POST | `/auth/forgot-password` | `{ email }` | `{ message }` |
66
+ | POST | `/auth/reset-password` | `{ token, password }` | `{ message }` |
67
+
68
+ ## License
69
+
70
+ [MIT](https://github.com/david-ouatedem/auth-core/blob/main/LICENSE)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@authcore/fastify",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "Fastify adapter for AuthCore",
5
5
  "author": "David Ouatedem",
6
6
  "repository": {
@@ -29,7 +29,7 @@
29
29
  "dependencies": {
30
30
  "@fastify/cookie": "^11.0.0",
31
31
  "fastify": "^5.0.0",
32
- "@authcore/core": "^0.5.1"
32
+ "@authcore/core": "^0.5.3"
33
33
  },
34
34
  "devDependencies": {
35
35
  "@prisma/client": "^5.22.0",
@@ -38,10 +38,11 @@
38
38
  "prisma": "^5.22.0",
39
39
  "typescript": "^5.4.0",
40
40
  "vitest": "^1.6.0",
41
- "@authcore/prisma-adapter": "^0.5.1"
41
+ "@authcore/prisma-adapter": "^0.5.3"
42
42
  },
43
43
  "files": [
44
- "dist"
44
+ "dist",
45
+ "README.md"
45
46
  ],
46
47
  "license": "MIT",
47
48
  "scripts": {