Skip to content

Commit 09d8d4d

Browse files
committed
setup: lint, ci, tests, nuxthub integration
1 parent 4141146 commit 09d8d4d

File tree

20 files changed

+2039
-622
lines changed

20 files changed

+2039
-622
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 49 deletions
This file was deleted.

.github/workflows/lint.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: lint
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
lint:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: pnpm/action-setup@v4
15+
- uses: actions/setup-node@v4
16+
with:
17+
node-version: 22
18+
cache: pnpm
19+
20+
- run: pnpm install
21+
- run: pnpm lint

.github/workflows/pkg.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: pkg.new
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
pkg:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- uses: pnpm/action-setup@v4
15+
- uses: actions/setup-node@v4
16+
with:
17+
node-version: 22
18+
cache: pnpm
19+
20+
- run: pnpm install
21+
- run: pnpm prepack
22+
- uses: pkg-pr-new/action@v1

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
enable-pre-post-scripts=true
2+
shamefully-hoist=true

README.md

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,51 @@
55
[![License][license-src]][license-href]
66
[![Nuxt][nuxt-src]][nuxt-href]
77

8-
Nuxt module for [Better Auth](https://better-auth.com) integration with route protection, session management, and role-based access.
8+
Nuxt module for [Better Auth](https://better-auth.com) integration with [NuxtHub](https://hub.nuxt.com). Provides route protection, session management, and role-based access.
99

1010
## Features
1111

12+
- NuxtHub Integration - Uses `hub:db` for database access
1213
- Route Protection - Declarative access rules via `routeRules`
1314
- Session Management - Server and client plugins that sync auth state
1415
- Role-Based Access - Support for `admin`, `user`, and custom roles
1516
- Tier Gating - Generic tier system for subscription/premium features
1617
- Auto-Imports - `useUserSession`, `usePageAccess`, `requireUserSession`, `getUserSession`
1718

19+
## Requirements
20+
21+
- NuxtHub with database enabled (`hub: { database: true }`)
22+
1823
## Quick Start
1924

2025
### 1. Install
2126

2227
```bash
23-
pnpm add nuxt-better-auth better-auth drizzle-orm
28+
pnpm add nuxt-better-auth better-auth drizzle-orm @nuxthub/core
29+
```
30+
31+
### 2. Configure Nuxt
32+
33+
```ts
34+
export default defineNuxtConfig({
35+
modules: ['@nuxthub/core', 'nuxt-better-auth'],
36+
37+
hub: { database: true },
38+
39+
runtimeConfig: {
40+
betterAuthSecret: '', // BETTER_AUTH_SECRET env var
41+
public: { siteUrl: 'http://localhost:3000' },
42+
},
43+
44+
routeRules: {
45+
'/app/**': { auth: 'user' },
46+
'/admin/**': { auth: 'user', requiresAdmin: true },
47+
'/login': { auth: 'guest' },
48+
},
49+
})
2450
```
2551

26-
### 2. Create Server Config
52+
### 3. Create Server Config
2753

2854
Create `server/auth.config.ts`:
2955

@@ -38,7 +64,7 @@ export default defineServerAuth(({ runtimeConfig, db }) => ({
3864
}))
3965
```
4066

41-
### 3. Create Client Config
67+
### 4. Create Client Config
4268

4369
Create `app/auth.client.ts`:
4470

@@ -56,7 +82,7 @@ export function createAppAuthClient(baseURL: string) {
5682
export type AppAuthClient = ReturnType<typeof createAppAuthClient>
5783
```
5884
59-
### 4. Add Type Extensions
85+
### 5. Add Type Extensions
6086
6187
Create `shared/types/auth.d.ts`:
6288
@@ -71,36 +97,6 @@ declare module '#nuxt-better-auth' {
7197
}
7298
```
7399

74-
### 5. Configure Nuxt
75-
76-
```ts
77-
export default defineNuxtConfig({
78-
modules: ['nuxt-better-auth'],
79-
80-
runtimeConfig: {
81-
betterAuthSecret: '', // BETTER_AUTH_SECRET env var
82-
public: { siteUrl: 'http://localhost:3000' },
83-
},
84-
85-
routeRules: {
86-
'/app/**': { auth: 'user' },
87-
'/admin/**': { auth: 'user', requiresAdmin: true },
88-
'/login': { auth: 'guest' },
89-
},
90-
})
91-
```
92-
93-
### 6. Setup Database
94-
95-
Set the global database instance in a server plugin:
96-
97-
```ts
98-
// server/plugins/db.ts
99-
export default defineNitroPlugin(() => {
100-
;(globalThis as any).__nuxt_better_auth_db = yourDrizzleInstance
101-
})
102-
```
103-
104100
## Route Rules
105101

106102
| Option | Type | Description |

eslint.config.mjs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
// @ts-check
2-
import { createConfigForNuxt } from '@nuxt/eslint-config/flat'
2+
import antfu from '@antfu/eslint-config'
33

4-
// Run `npx @eslint/config-inspector` to inspect the resolved config interactively
5-
export default createConfigForNuxt({
6-
features: {
7-
// Rules for module authors
8-
tooling: true,
9-
// Rules for formatting
10-
stylistic: true,
11-
},
12-
dirs: {
13-
src: [
14-
'./playground',
15-
],
4+
export default antfu({
5+
ignores: ['**/*.md', 'dist/**', '.nuxt/**'],
6+
}, {
7+
rules: {
8+
'node/prefer-global/process': 'off',
169
},
1710
})
18-
.append(
19-
// your custom flat config here...
20-
)

package.json

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "nuxt-better-auth",
3+
"type": "module",
34
"version": "0.1.0",
4-
"description": "Nuxt module for Better Auth integration with route protection, session management, and role-based access",
5-
"repository": "your-org/nuxt-better-auth",
5+
"description": "Nuxt module for Better Auth integration with NuxtHub, route protection, session management, and role-based access",
66
"license": "MIT",
7-
"type": "module",
7+
"repository": "your-org/nuxt-better-auth",
88
"exports": {
99
".": { "types": "./dist/types.d.mts", "import": "./dist/module.mjs" }
1010
},
@@ -16,27 +16,35 @@
1616
"dev": "pnpm dev:prepare && nuxi dev playground",
1717
"dev:build": "nuxi build playground",
1818
"dev:prepare": "nuxt-module-build build --stub && nuxi prepare playground",
19+
"prepare": "nuxt-module-build build --stub && nuxi prepare playground",
1920
"release": "pnpm lint && pnpm test && pnpm prepack && changelogen --release && npm publish && git push --follow-tags",
2021
"lint": "eslint .",
22+
"lint:fix": "eslint . --fix",
23+
"typecheck": "vue-tsc --noEmit",
24+
"typecheck:playground": "cd playground && vue-tsc --noEmit",
2125
"test": "vitest run",
22-
"test:watch": "vitest watch",
23-
"test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
26+
"test:watch": "vitest watch"
27+
},
28+
"peerDependencies": {
29+
"@nuxthub/core": ">=0.10.0",
30+
"better-auth": ">=1.0.0",
31+
"drizzle-orm": ">=0.30.0"
2432
},
2533
"dependencies": {
2634
"@nuxt/kit": "^4.2.2",
2735
"defu": "^6.1.4",
2836
"radix3": "^1.1.2"
2937
},
30-
"peerDependencies": {
31-
"better-auth": ">=1.0.0",
32-
"drizzle-orm": ">=0.30.0"
38+
"pnpm": {
39+
"onlyBuiltDependencies": ["better-sqlite3", "esbuild", "@parcel/watcher"]
3340
},
3441
"devDependencies": {
42+
"@antfu/eslint-config": "^4.12.0",
3543
"@nuxt/devtools": "^3.1.1",
36-
"@nuxt/eslint-config": "^1.11.0",
3744
"@nuxt/module-builder": "^1.0.2",
3845
"@nuxt/schema": "^4.2.2",
3946
"@nuxt/test-utils": "^3.21.0",
47+
"@nuxthub/core": "^0.10.0",
4048
"@types/better-sqlite3": "^7.6.13",
4149
"@types/node": "latest",
4250
"better-auth": "^1.2.8",

playground/nuxt.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export default defineNuxtConfig({
2-
modules: ['../src/module'],
2+
modules: ['@nuxthub/core', '../src/module'],
3+
4+
hub: { database: true },
35

46
devtools: { enabled: true },
57

playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"private": true,
32
"name": "my-module-playground",
43
"type": "module",
4+
"private": true,
55
"scripts": {
66
"dev": "nuxi dev",
77
"build": "nuxi build",

playground/server/auth.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { admin } from 'better-auth/plugins'
22
import { defineServerAuth } from '../../src/runtime/config'
33

4-
export default defineServerAuth(({ runtimeConfig }) => ({
4+
export default defineServerAuth(({ runtimeConfig: _runtimeConfig }) => ({
55
appName: 'Nuxt Better Auth Playground',
66
plugins: [admin()],
77
emailAndPassword: { enabled: true },

0 commit comments

Comments
 (0)