@onmax/nuxt-better-auth 0.0.1 → 0.0.2-alpha.10
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 +17 -170
- package/dist/module.d.mts +20 -2
- package/dist/module.json +1 -1
- package/dist/module.mjs +432 -14
- package/dist/runtime/app/components/BetterAuthState.d.vue.ts +20 -0
- package/dist/runtime/app/components/BetterAuthState.vue +8 -0
- package/dist/runtime/app/components/BetterAuthState.vue.d.ts +20 -0
- package/dist/runtime/app/composables/useUserSession.d.ts +22 -0
- package/dist/runtime/app/composables/useUserSession.js +159 -0
- package/dist/runtime/app/middleware/auth.global.d.ts +13 -0
- package/dist/runtime/app/middleware/auth.global.js +37 -0
- package/dist/runtime/app/pages/__better-auth-devtools.d.vue.ts +3 -0
- package/dist/runtime/app/pages/__better-auth-devtools.vue +426 -0
- package/dist/runtime/app/pages/__better-auth-devtools.vue.d.ts +3 -0
- package/dist/runtime/app/plugins/session.client.d.ts +2 -0
- package/dist/runtime/app/plugins/session.client.js +16 -0
- package/dist/runtime/app/plugins/session.server.d.ts +2 -0
- package/dist/runtime/app/plugins/session.server.js +24 -0
- package/dist/runtime/config.d.ts +44 -0
- package/dist/runtime/config.js +6 -0
- package/dist/runtime/server/api/_better-auth/_schema.d.ts +8 -0
- package/dist/runtime/server/api/_better-auth/_schema.js +11 -0
- package/dist/runtime/server/api/_better-auth/accounts.get.d.ts +14 -0
- package/dist/runtime/server/api/_better-auth/accounts.get.js +28 -0
- package/dist/runtime/server/api/_better-auth/config.get.d.ts +35 -0
- package/dist/runtime/server/api/_better-auth/config.get.js +47 -0
- package/dist/runtime/server/api/_better-auth/sessions.delete.d.ts +4 -0
- package/dist/runtime/server/api/_better-auth/sessions.delete.js +22 -0
- package/dist/runtime/server/api/_better-auth/sessions.get.d.ts +14 -0
- package/dist/runtime/server/api/_better-auth/sessions.get.js +43 -0
- package/dist/runtime/server/api/_better-auth/users.get.d.ts +14 -0
- package/dist/runtime/server/api/_better-auth/users.get.js +34 -0
- package/dist/runtime/server/api/auth/[...all].d.ts +2 -0
- package/dist/runtime/server/api/auth/[...all].js +6 -0
- package/dist/runtime/server/middleware/route-access.d.ts +2 -0
- package/dist/runtime/server/middleware/route-access.js +28 -0
- package/dist/runtime/server/tsconfig.json +3 -0
- package/dist/runtime/server/utils/auth.d.ts +11 -0
- package/dist/runtime/server/utils/auth.js +32 -0
- package/dist/runtime/server/utils/session.d.ts +9 -0
- package/dist/runtime/server/utils/session.js +23 -0
- package/dist/runtime/types/augment.d.ts +42 -0
- package/dist/runtime/types/augment.js +0 -0
- package/dist/runtime/types.d.ts +23 -0
- package/dist/runtime/types.js +0 -0
- package/dist/runtime/utils/match-user.d.ts +2 -0
- package/dist/runtime/utils/match-user.js +13 -0
- package/dist/types.d.mts +8 -10
- package/package.json +40 -11
- package/dist/module.d.cts +0 -2
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare var __VLS_1: {
|
|
2
|
+
loggedIn: any;
|
|
3
|
+
user: any;
|
|
4
|
+
session: any;
|
|
5
|
+
signOut: any;
|
|
6
|
+
}, __VLS_3: {};
|
|
7
|
+
type __VLS_Slots = {} & {
|
|
8
|
+
default?: (props: typeof __VLS_1) => any;
|
|
9
|
+
} & {
|
|
10
|
+
placeholder?: (props: typeof __VLS_3) => any;
|
|
11
|
+
};
|
|
12
|
+
declare const __VLS_base: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
13
|
+
declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
|
|
14
|
+
declare const _default: typeof __VLS_export;
|
|
15
|
+
export default _default;
|
|
16
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
17
|
+
new (): {
|
|
18
|
+
$slots: S;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare var __VLS_1: {
|
|
2
|
+
loggedIn: any;
|
|
3
|
+
user: any;
|
|
4
|
+
session: any;
|
|
5
|
+
signOut: any;
|
|
6
|
+
}, __VLS_3: {};
|
|
7
|
+
type __VLS_Slots = {} & {
|
|
8
|
+
default?: (props: typeof __VLS_1) => any;
|
|
9
|
+
} & {
|
|
10
|
+
placeholder?: (props: typeof __VLS_3) => any;
|
|
11
|
+
};
|
|
12
|
+
declare const __VLS_base: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
13
|
+
declare const __VLS_export: __VLS_WithSlots<typeof __VLS_base, __VLS_Slots>;
|
|
14
|
+
declare const _default: typeof __VLS_export;
|
|
15
|
+
export default _default;
|
|
16
|
+
type __VLS_WithSlots<T, S> = T & {
|
|
17
|
+
new (): {
|
|
18
|
+
$slots: S;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AppAuthClient, AuthSession, AuthUser } from '#nuxt-better-auth';
|
|
2
|
+
import type { ComputedRef, Ref } from 'vue';
|
|
3
|
+
export interface SignOutOptions {
|
|
4
|
+
onSuccess?: () => void | Promise<void>;
|
|
5
|
+
}
|
|
6
|
+
export interface UseUserSessionReturn {
|
|
7
|
+
client: AppAuthClient | null;
|
|
8
|
+
session: Ref<AuthSession | null>;
|
|
9
|
+
user: Ref<AuthUser | null>;
|
|
10
|
+
loggedIn: ComputedRef<boolean>;
|
|
11
|
+
ready: ComputedRef<boolean>;
|
|
12
|
+
signIn: NonNullable<AppAuthClient>['signIn'];
|
|
13
|
+
signUp: NonNullable<AppAuthClient>['signUp'];
|
|
14
|
+
signOut: (options?: SignOutOptions) => Promise<void>;
|
|
15
|
+
waitForSession: () => Promise<void>;
|
|
16
|
+
fetchSession: (options?: {
|
|
17
|
+
headers?: HeadersInit;
|
|
18
|
+
force?: boolean;
|
|
19
|
+
}) => Promise<void>;
|
|
20
|
+
updateUser: (updates: Partial<AuthUser>) => void;
|
|
21
|
+
}
|
|
22
|
+
export declare function useUserSession(): UseUserSessionReturn;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { createAppAuthClient } from "#auth/client";
|
|
2
|
+
import { computed, nextTick, useRequestHeaders, useRequestURL, useRuntimeConfig, useState, watch } from "#imports";
|
|
3
|
+
let _client = null;
|
|
4
|
+
function getClient(baseURL) {
|
|
5
|
+
if (!_client)
|
|
6
|
+
_client = createAppAuthClient(baseURL);
|
|
7
|
+
return _client;
|
|
8
|
+
}
|
|
9
|
+
export function useUserSession() {
|
|
10
|
+
const runtimeConfig = useRuntimeConfig();
|
|
11
|
+
const requestURL = useRequestURL();
|
|
12
|
+
const client = import.meta.client ? getClient(runtimeConfig.public.siteUrl || requestURL.origin) : null;
|
|
13
|
+
const session = useState("auth:session", () => null);
|
|
14
|
+
const user = useState("auth:user", () => null);
|
|
15
|
+
const authReady = useState("auth:ready", () => false);
|
|
16
|
+
const ready = computed(() => authReady.value);
|
|
17
|
+
const loggedIn = computed(() => Boolean(session.value && user.value));
|
|
18
|
+
function clearSession() {
|
|
19
|
+
session.value = null;
|
|
20
|
+
user.value = null;
|
|
21
|
+
}
|
|
22
|
+
function updateUser(updates) {
|
|
23
|
+
if (user.value)
|
|
24
|
+
user.value = { ...user.value, ...updates };
|
|
25
|
+
}
|
|
26
|
+
if (import.meta.client && client) {
|
|
27
|
+
const clientSession = client.useSession();
|
|
28
|
+
watch(
|
|
29
|
+
() => clientSession.value,
|
|
30
|
+
(newSession) => {
|
|
31
|
+
if (newSession?.data?.session && newSession?.data?.user) {
|
|
32
|
+
const { token: _, ...safeSession } = newSession.data.session;
|
|
33
|
+
session.value = safeSession;
|
|
34
|
+
user.value = newSession.data.user;
|
|
35
|
+
} else if (!newSession?.isPending) {
|
|
36
|
+
clearSession();
|
|
37
|
+
}
|
|
38
|
+
if (!authReady.value && !newSession?.isPending)
|
|
39
|
+
authReady.value = true;
|
|
40
|
+
},
|
|
41
|
+
{ immediate: true, deep: true }
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
function waitForSession() {
|
|
45
|
+
return new Promise((resolve) => {
|
|
46
|
+
if (loggedIn.value) {
|
|
47
|
+
resolve();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const unwatch = watch(loggedIn, (isLoggedIn) => {
|
|
51
|
+
if (isLoggedIn) {
|
|
52
|
+
unwatch();
|
|
53
|
+
resolve();
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
setTimeout(() => {
|
|
57
|
+
unwatch();
|
|
58
|
+
resolve();
|
|
59
|
+
}, 5e3);
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
function wrapOnSuccess(cb) {
|
|
63
|
+
return async (ctx) => {
|
|
64
|
+
await fetchSession({ force: true });
|
|
65
|
+
if (!loggedIn.value)
|
|
66
|
+
await waitForSession();
|
|
67
|
+
await nextTick();
|
|
68
|
+
await cb(ctx);
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function wrapAuthMethod(method) {
|
|
72
|
+
return (async (...args) => {
|
|
73
|
+
const [data, options] = args;
|
|
74
|
+
if (data?.fetchOptions?.onSuccess) {
|
|
75
|
+
return method({ ...data, fetchOptions: { ...data.fetchOptions, onSuccess: wrapOnSuccess(data.fetchOptions.onSuccess) } }, options);
|
|
76
|
+
}
|
|
77
|
+
if (options?.onSuccess) {
|
|
78
|
+
return method(data, { ...options, onSuccess: wrapOnSuccess(options.onSuccess) });
|
|
79
|
+
}
|
|
80
|
+
return method(data, options);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
const signIn = client?.signIn ? new Proxy(client.signIn, {
|
|
84
|
+
get(target, prop) {
|
|
85
|
+
const targetRecord = target;
|
|
86
|
+
const method = targetRecord[prop];
|
|
87
|
+
if (typeof method !== "function")
|
|
88
|
+
return method;
|
|
89
|
+
return wrapAuthMethod((...args) => targetRecord[prop](...args));
|
|
90
|
+
}
|
|
91
|
+
}) : new Proxy({}, {
|
|
92
|
+
get: (_, prop) => {
|
|
93
|
+
throw new Error(`signIn.${String(prop)}() can only be called on client-side`);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
const signUp = client?.signUp ? new Proxy(client.signUp, {
|
|
97
|
+
get(target, prop) {
|
|
98
|
+
const targetRecord = target;
|
|
99
|
+
const method = targetRecord[prop];
|
|
100
|
+
if (typeof method !== "function")
|
|
101
|
+
return method;
|
|
102
|
+
return wrapAuthMethod((...args) => targetRecord[prop](...args));
|
|
103
|
+
}
|
|
104
|
+
}) : new Proxy({}, {
|
|
105
|
+
get: (_, prop) => {
|
|
106
|
+
throw new Error(`signUp.${String(prop)}() can only be called on client-side`);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
async function fetchSession(options = {}) {
|
|
110
|
+
if (import.meta.server) {
|
|
111
|
+
if (!authReady.value)
|
|
112
|
+
authReady.value = true;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (client) {
|
|
116
|
+
try {
|
|
117
|
+
const headers = options.headers || useRequestHeaders(["cookie"]);
|
|
118
|
+
const fetchOptions = headers ? { headers } : void 0;
|
|
119
|
+
const query = options.force ? { disableCookieCache: true } : void 0;
|
|
120
|
+
const result = await client.getSession({ query }, fetchOptions);
|
|
121
|
+
const data = result.data;
|
|
122
|
+
if (data?.session && data?.user) {
|
|
123
|
+
const { token: _, ...safeSession } = data.session;
|
|
124
|
+
session.value = safeSession;
|
|
125
|
+
user.value = data.user;
|
|
126
|
+
} else {
|
|
127
|
+
clearSession();
|
|
128
|
+
}
|
|
129
|
+
} catch (error) {
|
|
130
|
+
clearSession();
|
|
131
|
+
console.error("[nuxt-better-auth] Failed to fetch session:", error);
|
|
132
|
+
} finally {
|
|
133
|
+
if (!authReady.value)
|
|
134
|
+
authReady.value = true;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
async function signOut(options) {
|
|
139
|
+
if (!client)
|
|
140
|
+
throw new Error("signOut can only be called on client-side");
|
|
141
|
+
await client.signOut();
|
|
142
|
+
clearSession();
|
|
143
|
+
if (options?.onSuccess)
|
|
144
|
+
await options.onSuccess();
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
client,
|
|
148
|
+
session,
|
|
149
|
+
user,
|
|
150
|
+
loggedIn,
|
|
151
|
+
ready,
|
|
152
|
+
signIn,
|
|
153
|
+
signUp,
|
|
154
|
+
signOut,
|
|
155
|
+
waitForSession,
|
|
156
|
+
fetchSession,
|
|
157
|
+
updateUser
|
|
158
|
+
};
|
|
159
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AuthMeta } from '../../types.js';
|
|
2
|
+
declare module '#app' {
|
|
3
|
+
interface PageMeta {
|
|
4
|
+
auth?: AuthMeta;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
declare module 'vue-router' {
|
|
8
|
+
interface RouteMeta {
|
|
9
|
+
auth?: AuthMeta;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
declare const _default: any;
|
|
13
|
+
export default _default;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createError, defineNuxtRouteMiddleware, getRouteRules, navigateTo, useNuxtApp, useRequestHeaders, useRuntimeConfig, useUserSession } from "#imports";
|
|
2
|
+
import { matchesUser } from "../../utils/match-user.js";
|
|
3
|
+
export default defineNuxtRouteMiddleware(async (to) => {
|
|
4
|
+
const nuxtApp = useNuxtApp();
|
|
5
|
+
if (import.meta.client) {
|
|
6
|
+
const isPrerendered = nuxtApp.payload.prerenderedAt || nuxtApp.payload.isCached;
|
|
7
|
+
if (isPrerendered && nuxtApp.isHydrating)
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (to.meta.auth === void 0) {
|
|
11
|
+
const rules = await getRouteRules({ path: to.path });
|
|
12
|
+
if (rules.auth !== void 0)
|
|
13
|
+
to.meta.auth = rules.auth;
|
|
14
|
+
}
|
|
15
|
+
const auth = to.meta.auth;
|
|
16
|
+
if (auth === void 0 || auth === false)
|
|
17
|
+
return;
|
|
18
|
+
const config = useRuntimeConfig().public.auth;
|
|
19
|
+
const { fetchSession, user, loggedIn } = useUserSession();
|
|
20
|
+
if (!loggedIn.value) {
|
|
21
|
+
const headers = import.meta.server ? useRequestHeaders(["cookie"]) : void 0;
|
|
22
|
+
await fetchSession({ headers });
|
|
23
|
+
}
|
|
24
|
+
const mode = typeof auth === "string" ? auth : auth?.only ?? "user";
|
|
25
|
+
const redirectTo = typeof auth === "object" ? auth.redirectTo : void 0;
|
|
26
|
+
if (mode === "guest") {
|
|
27
|
+
if (loggedIn.value)
|
|
28
|
+
return navigateTo(redirectTo ?? config?.redirects?.guest ?? "/");
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (!loggedIn.value)
|
|
32
|
+
return navigateTo(redirectTo ?? config?.redirects?.login ?? "/login");
|
|
33
|
+
if (typeof auth === "object" && auth.user) {
|
|
34
|
+
if (!user.value || !matchesUser(user.value, auth.user))
|
|
35
|
+
throw createError({ statusCode: 403, statusMessage: "Access denied" });
|
|
36
|
+
}
|
|
37
|
+
});
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
declare const __VLS_export: import("vue").DefineComponent<{}, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<{}> & Readonly<{}>, {}, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
|
|
2
|
+
declare const _default: typeof __VLS_export;
|
|
3
|
+
export default _default;
|