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
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { adminClient, usernameClient } from "better-auth/client/plugins";
|
|
2
|
+
import { createAuthClient } from "better-auth/vue";
|
|
3
|
+
import { defu } from "defu";
|
|
4
|
+
import { computed, ref } from "vue";
|
|
5
|
+
import { navigateTo, useRequestHeaders, useRequestURL, useRuntimeConfig, useState } from "#app";
|
|
6
|
+
let _authInstance;
|
|
7
|
+
function createAuthInstance() {
|
|
8
|
+
const url = useRequestURL();
|
|
9
|
+
const headers = import.meta.server ? useRequestHeaders() : void 0;
|
|
10
|
+
const config = useRuntimeConfig();
|
|
11
|
+
const authClient = createAuthClient({
|
|
12
|
+
baseURL: url.origin,
|
|
13
|
+
fetchOptions: { headers },
|
|
14
|
+
plugins: [
|
|
15
|
+
adminClient(),
|
|
16
|
+
usernameClient()
|
|
17
|
+
]
|
|
18
|
+
});
|
|
19
|
+
const options = defu(config.public.betterAuth.redirectOptions || {}, {
|
|
20
|
+
redirectUserTo: "/profile",
|
|
21
|
+
redirectGuestTo: "/signin",
|
|
22
|
+
redirectUnauthorizedTo: "/401"
|
|
23
|
+
});
|
|
24
|
+
const session = useState("auth:session", () => null);
|
|
25
|
+
const user = useState("auth:user", () => null);
|
|
26
|
+
const sessionFetching = import.meta.server ? ref(false) : useState("auth:sessionFetching", () => false);
|
|
27
|
+
const fetchSession = async () => {
|
|
28
|
+
if (sessionFetching.value) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
sessionFetching.value = true;
|
|
32
|
+
const { data } = await authClient.getSession({
|
|
33
|
+
fetchOptions: {
|
|
34
|
+
headers
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
session.value = data?.session || null;
|
|
38
|
+
user.value = data?.user || null;
|
|
39
|
+
sessionFetching.value = false;
|
|
40
|
+
return data;
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
session,
|
|
44
|
+
user,
|
|
45
|
+
loggedIn: computed(() => !!session.value),
|
|
46
|
+
signIn: authClient.signIn,
|
|
47
|
+
signUp: authClient.signUp,
|
|
48
|
+
options,
|
|
49
|
+
fetchSession,
|
|
50
|
+
client: authClient,
|
|
51
|
+
signOut: async ({ redirectTo } = {}) => {
|
|
52
|
+
try {
|
|
53
|
+
await authClient.signOut();
|
|
54
|
+
if (redirectTo)
|
|
55
|
+
await navigateTo(redirectTo);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.error("Sign out failed:", error);
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function setupSessionListener(client) {
|
|
64
|
+
if (!import.meta.client)
|
|
65
|
+
return;
|
|
66
|
+
client.$store.listen("$sessionSignal", async (signal) => {
|
|
67
|
+
if (!signal)
|
|
68
|
+
return;
|
|
69
|
+
await client.useSession($fetch);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
export function useAuth() {
|
|
73
|
+
if (_authInstance && import.meta.client)
|
|
74
|
+
return _authInstance;
|
|
75
|
+
const auth = createAuthInstance();
|
|
76
|
+
if (import.meta.client) {
|
|
77
|
+
setupSessionListener(auth.client);
|
|
78
|
+
_authInstance = auth;
|
|
79
|
+
}
|
|
80
|
+
return auth;
|
|
81
|
+
}
|
package/dist/runtime/plugin.js
CHANGED
package/dist/types.d.mts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import type { ModulePublicRuntimeConfig } from './module.mjs'
|
|
2
|
+
|
|
3
|
+
declare module '@nuxt/schema' {
|
|
4
|
+
interface PublicRuntimeConfig extends ModulePublicRuntimeConfig {}
|
|
5
|
+
}
|
|
6
|
+
|
|
1
7
|
export { default } from './module.mjs'
|
|
2
8
|
|
|
3
|
-
export { type ModuleOptions } from './module.mjs'
|
|
9
|
+
export { type ModuleClientOptions, type ModuleOptions, type ModulePublicRuntimeConfig, type ModuleServerOptions } from './module.mjs'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "better-auth-nuxt",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Better Auth Nuxt Module",
|
|
5
5
|
"repository": "productdevbook/better-auth-nuxt",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,8 +34,17 @@
|
|
|
34
34
|
"test:watch": "vitest watch",
|
|
35
35
|
"test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
|
|
36
36
|
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"better-auth": ">=1.2.7"
|
|
39
|
+
},
|
|
37
40
|
"dependencies": {
|
|
38
|
-
"@
|
|
41
|
+
"@fastify/deepmerge": "^3.1.0",
|
|
42
|
+
"@nuxt/kit": "^3.16.2",
|
|
43
|
+
"defu": "^6.1.4",
|
|
44
|
+
"ohash": "^2.0.11",
|
|
45
|
+
"pathe": "^2.0.3",
|
|
46
|
+
"scule": "^1.3.0",
|
|
47
|
+
"tinyglobby": "^0.2.12"
|
|
39
48
|
},
|
|
40
49
|
"devDependencies": {
|
|
41
50
|
"@nuxt/devtools": "^2.4.0",
|
|
@@ -43,7 +52,10 @@
|
|
|
43
52
|
"@nuxt/module-builder": "^1.0.1",
|
|
44
53
|
"@nuxt/schema": "^3.16.2",
|
|
45
54
|
"@nuxt/test-utils": "^3.17.2",
|
|
55
|
+
"@types/better-sqlite3": "^7.6.13",
|
|
46
56
|
"@types/node": "latest",
|
|
57
|
+
"better-auth": "^1.2.7",
|
|
58
|
+
"better-sqlite3": "^11.9.1",
|
|
47
59
|
"changelogen": "^0.6.1",
|
|
48
60
|
"eslint": "^9.24.0",
|
|
49
61
|
"nuxt": "^3.16.2",
|