better-auth-nuxt 0.0.1 → 0.0.2
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 +7 -16
- package/dist/module.d.mts +37 -2
- package/dist/module.json +1 -1
- package/dist/module.mjs +142 -3
- package/dist/runtime/composables/useAuth.d.ts +3492 -0
- package/dist/runtime/composables/useAuth.js +81 -0
- package/dist/runtime/plugin.js +0 -1
- package/dist/runtime/server/handler.d.ts +2 -0
- package/dist/runtime/server/handler.js +5 -0
- package/dist/runtime/server.d.ts +6 -0
- package/dist/runtime/server.js +5 -0
- package/dist/types.d.mts +7 -1
- package/package.json +14 -2
package/README.md
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Get your module up and running quickly.
|
|
3
|
-
|
|
4
|
-
Find and replace all on all files (CMD+SHIFT+F):
|
|
5
|
-
- Name: My Module
|
|
6
|
-
- Package name: my-module
|
|
7
|
-
- Description: My new Nuxt module
|
|
8
|
-
-->
|
|
9
|
-
|
|
10
|
-
# My Module
|
|
1
|
+
# Better Auth Nuxt
|
|
11
2
|
|
|
12
3
|
[![npm version][npm-version-src]][npm-version-href]
|
|
13
4
|
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
@@ -71,14 +62,14 @@ That's it! You can now use My Module in your Nuxt app ✨
|
|
|
71
62
|
|
|
72
63
|
|
|
73
64
|
<!-- Badges -->
|
|
74
|
-
[npm-version-src]: https://img.shields.io/npm/v/
|
|
75
|
-
[npm-version-href]: https://npmjs.com/package/
|
|
65
|
+
[npm-version-src]: https://img.shields.io/npm/v/better-auth-nuxt/latest.svg?style=flat&colorA=020420&colorB=00DC82
|
|
66
|
+
[npm-version-href]: https://npmjs.com/package/better-auth-nuxt
|
|
76
67
|
|
|
77
|
-
[npm-downloads-src]: https://img.shields.io/npm/dm/
|
|
78
|
-
[npm-downloads-href]: https://npm.chart.dev/
|
|
68
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/better-auth-nuxt.svg?style=flat&colorA=020420&colorB=00DC82
|
|
69
|
+
[npm-downloads-href]: https://npm.chart.dev/better-auth-nuxt
|
|
79
70
|
|
|
80
|
-
[license-src]: https://img.shields.io/npm/l/
|
|
81
|
-
[license-href]: https://npmjs.com/package/
|
|
71
|
+
[license-src]: https://img.shields.io/npm/l/better-auth-nuxt.svg?style=flat&colorA=020420&colorB=00DC82
|
|
72
|
+
[license-href]: https://npmjs.com/package/better-auth-nuxt
|
|
82
73
|
|
|
83
74
|
[nuxt-src]: https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js
|
|
84
75
|
[nuxt-href]: https://nuxt.com
|
package/dist/module.d.mts
CHANGED
|
@@ -1,9 +1,44 @@
|
|
|
1
1
|
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
+
import { ClientOptions, BetterAuthOptions } from 'better-auth';
|
|
2
3
|
|
|
4
|
+
interface ModuleServerOptions extends Pick<BetterAuthOptions, 'appName' | 'baseURL' | 'basePath' | 'secret'> {
|
|
5
|
+
}
|
|
6
|
+
interface ModuleClientOptions extends Pick<ClientOptions, 'baseURL' | 'basePath' | 'disableDefaultFetchPlugins'> {
|
|
7
|
+
}
|
|
3
8
|
interface ModuleOptions {
|
|
4
|
-
baseUrl
|
|
9
|
+
baseUrl: string;
|
|
10
|
+
/**
|
|
11
|
+
* auth endpoint
|
|
12
|
+
* @default 'api/auth/**'
|
|
13
|
+
*/
|
|
14
|
+
endpoint: string;
|
|
15
|
+
/**
|
|
16
|
+
* @default ['*.better-auth']
|
|
17
|
+
*/
|
|
18
|
+
serverConfigs?: string[];
|
|
19
|
+
options: {
|
|
20
|
+
/**
|
|
21
|
+
* client options object or path to client setup script
|
|
22
|
+
*/
|
|
23
|
+
client: ModuleClientOptions;
|
|
24
|
+
/**
|
|
25
|
+
* server options object or path to server setup script
|
|
26
|
+
*/
|
|
27
|
+
server: ModuleServerOptions;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* redirect options
|
|
31
|
+
*/
|
|
32
|
+
redirectOptions: {
|
|
33
|
+
redirectUserTo: string;
|
|
34
|
+
redirectGuestTo: string;
|
|
35
|
+
redirectUnauthorizedTo: string;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
interface ModulePublicRuntimeConfig {
|
|
39
|
+
betterAuth: ModuleOptions;
|
|
5
40
|
}
|
|
6
41
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
|
7
42
|
|
|
8
43
|
export { _default as default };
|
|
9
|
-
export type { ModuleOptions };
|
|
44
|
+
export type { ModuleClientOptions, ModuleOptions, ModulePublicRuntimeConfig, ModuleServerOptions };
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,4 +1,37 @@
|
|
|
1
|
-
import
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import { defineNuxtModule, createResolver, logger, addImportsDir, addServerHandler, addTypeTemplate, addServerImports, addPlugin, addTemplate } from '@nuxt/kit';
|
|
3
|
+
import { defu } from 'defu';
|
|
4
|
+
import { resolve } from 'pathe';
|
|
5
|
+
import { hash } from 'ohash';
|
|
6
|
+
import { glob } from 'tinyglobby';
|
|
7
|
+
import { pascalCase } from 'scule';
|
|
8
|
+
|
|
9
|
+
async function serverAuth({ options }) {
|
|
10
|
+
return [
|
|
11
|
+
'import mergeDeep from "@fastify/deepmerge"',
|
|
12
|
+
'import { betterAuth } from "better-auth"',
|
|
13
|
+
...options.configs.map((config) => {
|
|
14
|
+
return `import ${config.key} from "${config.path}"`;
|
|
15
|
+
}),
|
|
16
|
+
"const betterAuthConfigs = mergeDeep({all: true})({},",
|
|
17
|
+
...options.configs.map((config) => {
|
|
18
|
+
return `${config.key},`;
|
|
19
|
+
}),
|
|
20
|
+
")",
|
|
21
|
+
"",
|
|
22
|
+
"let _auth",
|
|
23
|
+
"",
|
|
24
|
+
"export function useAuth() {",
|
|
25
|
+
" if (!_auth) {",
|
|
26
|
+
" _auth = betterAuth(betterAuthConfigs)",
|
|
27
|
+
" }",
|
|
28
|
+
" return _auth",
|
|
29
|
+
"}",
|
|
30
|
+
"",
|
|
31
|
+
"export const auth = useAuth()",
|
|
32
|
+
""
|
|
33
|
+
].join("\n");
|
|
34
|
+
}
|
|
2
35
|
|
|
3
36
|
const module = defineNuxtModule({
|
|
4
37
|
meta: {
|
|
@@ -6,9 +39,115 @@ const module = defineNuxtModule({
|
|
|
6
39
|
configKey: "betterAuth"
|
|
7
40
|
},
|
|
8
41
|
// Default configuration options of the Nuxt module
|
|
9
|
-
defaults: {
|
|
10
|
-
|
|
42
|
+
defaults: {
|
|
43
|
+
endpoint: "/api/auth/**",
|
|
44
|
+
serverConfigs: [],
|
|
45
|
+
redirectOptions: {
|
|
46
|
+
redirectUserTo: "/profile",
|
|
47
|
+
redirectGuestTo: "/signin",
|
|
48
|
+
redirectUnauthorizedTo: "/401"
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
async setup(options, nuxt) {
|
|
11
52
|
const resolver = createResolver(import.meta.url);
|
|
53
|
+
if (!options.endpoint) {
|
|
54
|
+
logger.withTag("better-auth").error("Missing endpoint option");
|
|
55
|
+
}
|
|
56
|
+
nuxt.options.runtimeConfig.public.betterAuth = defu(nuxt.options.runtimeConfig.public.betterAuth, {
|
|
57
|
+
baseUrl: options.baseUrl,
|
|
58
|
+
endpoint: options.endpoint,
|
|
59
|
+
redirectOptions: options.redirectOptions
|
|
60
|
+
});
|
|
61
|
+
nuxt.options.alias["#better-auth"] = resolve("./runtime");
|
|
62
|
+
addImportsDir(resolve("./runtime/composables"));
|
|
63
|
+
addServerHandler({
|
|
64
|
+
route: options.endpoint,
|
|
65
|
+
handler: resolver.resolve("./runtime/server/handler")
|
|
66
|
+
});
|
|
67
|
+
const registerTemplate = (options2) => {
|
|
68
|
+
const name = options2.filename.replace(/\.m?js$/, "");
|
|
69
|
+
const alias = "#" + name;
|
|
70
|
+
const results = addTemplate({
|
|
71
|
+
...options2,
|
|
72
|
+
write: true
|
|
73
|
+
// Write to disk for Nitro to consume
|
|
74
|
+
});
|
|
75
|
+
nuxt.options.nitro.alias ||= {};
|
|
76
|
+
nuxt.options.nitro.externals ||= {};
|
|
77
|
+
nuxt.options.nitro.externals.inline ||= [];
|
|
78
|
+
nuxt.options.alias[alias] = results.dst;
|
|
79
|
+
nuxt.options.nitro.alias[alias] = nuxt.options.alias[alias];
|
|
80
|
+
nuxt.options.nitro.externals.inline.push(nuxt.options.alias[alias]);
|
|
81
|
+
nuxt.options.nitro.externals.inline.push(alias);
|
|
82
|
+
return results;
|
|
83
|
+
};
|
|
84
|
+
const serverConfigs = [
|
|
85
|
+
{
|
|
86
|
+
key: pascalCase(hash("better-auth-configs")),
|
|
87
|
+
path: resolver.resolve("./runtime/server")
|
|
88
|
+
}
|
|
89
|
+
];
|
|
90
|
+
for (const layer of nuxt.options._layers) {
|
|
91
|
+
const paths = await glob([
|
|
92
|
+
"**/*.better-auth.ts",
|
|
93
|
+
...options.serverConfigs?.map((pattern) => {
|
|
94
|
+
return `**/${pattern}.ts`;
|
|
95
|
+
}) || []
|
|
96
|
+
], { onlyFiles: true, ignore: nuxt.options.ignore, dot: true, cwd: layer.config.rootDir, absolute: true });
|
|
97
|
+
const pathsJS = await glob([
|
|
98
|
+
"**/*.better-auth.js",
|
|
99
|
+
...options.serverConfigs?.map((pattern) => {
|
|
100
|
+
return `**/${pattern}.js`;
|
|
101
|
+
}) || []
|
|
102
|
+
], { cwd: layer.config.serverDir });
|
|
103
|
+
if (paths.length === 0 && pathsJS.length === 0) {
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
for (const path of [...paths, ...pathsJS]) {
|
|
107
|
+
console.log("path", path);
|
|
108
|
+
if (fs.existsSync(path)) {
|
|
109
|
+
serverConfigs.push({
|
|
110
|
+
key: pascalCase(hash(path)),
|
|
111
|
+
path
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
registerTemplate({
|
|
117
|
+
filename: "better-auth-configs.mjs",
|
|
118
|
+
getContents: serverAuth,
|
|
119
|
+
options: { configs: serverConfigs }
|
|
120
|
+
});
|
|
121
|
+
addTypeTemplate({
|
|
122
|
+
filename: "better-auth-configs.d.ts",
|
|
123
|
+
getContents: () => {
|
|
124
|
+
return [
|
|
125
|
+
'import { betterAuth } from "better-auth"',
|
|
126
|
+
'import mergeDeep from "@fastify/deepmerge"',
|
|
127
|
+
...serverConfigs.map((config) => {
|
|
128
|
+
return `import ${config.key} from "${config.path}"`;
|
|
129
|
+
}),
|
|
130
|
+
"const betterAuthConfigs = mergeDeep({all: true})({},",
|
|
131
|
+
...serverConfigs.map((config) => {
|
|
132
|
+
return `${config.key},`;
|
|
133
|
+
}),
|
|
134
|
+
")",
|
|
135
|
+
"export type BetterAuth = ReturnType<typeof betterAuth<typeof betterAuthConfigs>>",
|
|
136
|
+
"export declare const useAuth: () => BetterAuth",
|
|
137
|
+
"export declare const auth: BetterAuth"
|
|
138
|
+
].join("\n");
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
addServerImports([
|
|
142
|
+
{
|
|
143
|
+
from: "#better-auth-configs",
|
|
144
|
+
name: "useAuth"
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
from: "#better-auth-configs",
|
|
148
|
+
name: "auth"
|
|
149
|
+
}
|
|
150
|
+
]);
|
|
12
151
|
addPlugin(resolver.resolve("./runtime/plugin"));
|
|
13
152
|
}
|
|
14
153
|
});
|