better-auth-nuxt 0.0.2 → 0.0.3
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/dist/module.d.mts +4 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +196 -7
- package/dist/runtime/middleware/auth.global.d.ts +27 -0
- package/dist/runtime/middleware/auth.global.js +35 -0
- package/dist/runtime/plugin.d.ts +1 -1
- package/dist/runtime/server/handler.js +1 -1
- package/package.json +1 -1
- package/dist/runtime/composables/useAuth.d.ts +0 -3492
- package/dist/runtime/composables/useAuth.js +0 -81
|
@@ -1,81 +0,0 @@
|
|
|
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
|
-
}
|