From ab0d9cc309ee9ffa3ff43fa81ee5443e25416ee7 Mon Sep 17 00:00:00 2001 From: Bereket Engida <86073083+Bekacru@users.noreply.github.com> Date: Thu, 17 Oct 2024 22:14:56 +0300 Subject: [PATCH] feat: support useSession ssr for nuxt (#209) --- docs/content/docs/integrations/nuxt.mdx | 28 ++++++++++++++---- examples/nuxt-example/lib/auth-client.ts | 4 +-- examples/nuxt-example/pages/dashboard.vue | 13 ++++---- examples/nuxt-example/pages/index.vue | 7 +++-- package.json | 2 +- packages/better-auth/src/client/vue.ts | 36 ++++++++++++++++++++++- 6 files changed, 71 insertions(+), 19 deletions(-) diff --git a/docs/content/docs/integrations/nuxt.mdx b/docs/content/docs/integrations/nuxt.mdx index 5df2192a..90074a39 100644 --- a/docs/content/docs/integrations/nuxt.mdx +++ b/docs/content/docs/integrations/nuxt.mdx @@ -35,20 +35,20 @@ Create a client instance. You can name the file anything you want. Here we are c ```ts title="client.ts" import { createAuthClient } from "better-auth/vue" // make sure to import from better-auth/vue -export const client = createAuthClient({ +export const authClient = createAuthClient({ //you can pass client configuration here }) ``` Once you have created the client, you can use it to sign up, sign in, and perform other actions. -Some of the actinos are reactive. The client use [nano-store](https://github.com/nanostores/nanostores) to store the state and re-render the components when the state changes. +Some of the actinos are reactive. ### Example usage ```vue title="index.vue" -``` \ No newline at end of file +``` + +### SSR Usage + +If you are using Nuxt with SSR, you can use the `useSession` function in the `setup` function of your page component and pass `useFetch` to make it work with SSR. + +```vue title="index.vue" + + + +``` diff --git a/examples/nuxt-example/lib/auth-client.ts b/examples/nuxt-example/lib/auth-client.ts index 642cd9c9..89fd967d 100644 --- a/examples/nuxt-example/lib/auth-client.ts +++ b/examples/nuxt-example/lib/auth-client.ts @@ -1,6 +1,6 @@ import { createAuthClient } from "better-auth/vue"; -export const client = createAuthClient({ +export const authClient = createAuthClient({ baseURL: "http://localhost:3000", }); @@ -11,4 +11,4 @@ export const { useSession, forgetPassword, resetPassword, -} = client; +} = authClient; diff --git a/examples/nuxt-example/pages/dashboard.vue b/examples/nuxt-example/pages/dashboard.vue index eb7eacb6..4d328a2b 100644 --- a/examples/nuxt-example/pages/dashboard.vue +++ b/examples/nuxt-example/pages/dashboard.vue @@ -1,7 +1,6 @@ @@ -16,15 +15,15 @@ const router = useRouter();
- - {{ session.data?.user.name[0] }} + + {{ session?.user.name[0] }}

- {{ session.data?.user?.name }} + {{ session?.user?.name }}

- {{ session.data?.user?.email }} + {{ session?.user?.email }}

@@ -32,7 +31,7 @@ const router = useRouter(); diff --git a/examples/nuxt-example/pages/index.vue b/examples/nuxt-example/pages/index.vue index 456ea4c0..fa107edd 100644 --- a/examples/nuxt-example/pages/index.vue +++ b/examples/nuxt-example/pages/index.vue @@ -17,7 +17,7 @@ const features = [ "Rate Limiting", "Session Management", ]; -const session = useSession(); +const { data: session } = await useSession(useFetch);