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.
@@ -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
+ }
@@ -1,4 +1,3 @@
1
1
  import { defineNuxtPlugin } from "#app";
2
2
  export default defineNuxtPlugin((_nuxtApp) => {
3
- console.log("Plugin injected by my-module!");
4
3
  });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<Response>>;
2
+ export default _default;
@@ -0,0 +1,5 @@
1
+ import { defineEventHandler, toWebRequest } from "h3";
2
+ import { auth } from "#better-auth-configs";
3
+ export default defineEventHandler((event) => {
4
+ return auth.handler(toWebRequest(event));
5
+ });
@@ -0,0 +1,6 @@
1
+ declare const _default: {
2
+ disabledPaths: never[];
3
+ plugins: never[];
4
+ trustedOrigins: never[];
5
+ };
6
+ export default _default;
@@ -0,0 +1,5 @@
1
+ export default {
2
+ disabledPaths: [],
3
+ plugins: [],
4
+ trustedOrigins: []
5
+ };
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.1",
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
- "@nuxt/kit": "^3.16.2"
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",