@kanjijs/auth 0.2.0-beta.1
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/README.md +73 -0
- package/dist/index.js +8724 -0
- package/package.json +27 -0
package/README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# @kanjijs/auth
|
|
2
|
+
|
|
3
|
+
Authentication module for the Kanjijs Framework, powered by [Auth.js](https://authjs.dev/).
|
|
4
|
+
|
|
5
|
+
## 📦 Features
|
|
6
|
+
|
|
7
|
+
- **Auth.js Integration**: Use any Auth.js provider (GitHub, Google, Credentials, etc.).
|
|
8
|
+
- **Global Auth**: Initialize authentication once for the entire application.
|
|
9
|
+
- **Decorators**: `@Use(requireAuth)` and factory `requireRole("admin")`.
|
|
10
|
+
- **Hono Native**: Built on top of `@hono/auth-js` and native middlewares.
|
|
11
|
+
|
|
12
|
+
## 🚀 Usage
|
|
13
|
+
|
|
14
|
+
### 1. Global Setup (Recommended)
|
|
15
|
+
|
|
16
|
+
Import `AuthModule.forRoot` in your root module. This sets up the session middleware globally.
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
// app.module.ts
|
|
20
|
+
import { Module } from "@kanjijs/core";
|
|
21
|
+
import { AuthModule } from "@kanjijs/auth";
|
|
22
|
+
import GitHub from "@auth/core/providers/github";
|
|
23
|
+
|
|
24
|
+
@Module({
|
|
25
|
+
imports: [
|
|
26
|
+
AuthModule.forRoot({
|
|
27
|
+
secret: process.env.AUTH_SECRET,
|
|
28
|
+
providers: [
|
|
29
|
+
GitHub({ clientId: "...", clientSecret: "..." })
|
|
30
|
+
]
|
|
31
|
+
})
|
|
32
|
+
]
|
|
33
|
+
})
|
|
34
|
+
export class AppModule {}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### 2. Protecting Routes
|
|
38
|
+
|
|
39
|
+
Use the `requireAuth` middleware to guard routes. The `user` object will be available in the context.
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { Controller, Get, Use } from "@kanjijs/core";
|
|
43
|
+
import { requireAuth } from "@kanjijs/auth";
|
|
44
|
+
import type { Context } from "hono";
|
|
45
|
+
|
|
46
|
+
@Controller("/profile")
|
|
47
|
+
export class ProfileController {
|
|
48
|
+
|
|
49
|
+
@Get("/")
|
|
50
|
+
@Use(requireAuth) // 401 if not logged in
|
|
51
|
+
getProfile(c: Context) {
|
|
52
|
+
const user = c.get("user");
|
|
53
|
+
return { id: user.id, name: user.name };
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### 3. Role-Based Access
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { requireRole } from "@kanjijs/auth";
|
|
62
|
+
|
|
63
|
+
@Get("/admin")
|
|
64
|
+
@Use(requireRole("admin")) // Checks user.role === 'admin'
|
|
65
|
+
adminPanel(c: Context) {
|
|
66
|
+
return { ok: true };
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## 🛠️ Configuration
|
|
71
|
+
|
|
72
|
+
The `config` object passed to `AuthModule.forRoot(config)` is strict `AuthConfig` from `@auth/core`.
|
|
73
|
+
Refer to [Auth.js Documentation](https://authjs.dev/reference/core#authconfig) for all available options (callbacks, pages, session strategy, etc.).
|