diff --git a/docs/content/docs/concepts/session-management.mdx b/docs/content/docs/concepts/session-management.mdx index bead809e93..69c19c8b82 100644 --- a/docs/content/docs/concepts/session-management.mdx +++ b/docs/content/docs/concepts/session-management.mdx @@ -37,6 +37,26 @@ export const auth = betterAuth({ Better Auth provides a set of functions to manage sessions. +### Get Session + +The `getSession` function retrieves the current active session. + +```ts client="client.ts" +import { authClient } from "@/lib/client" + +const session = await authClient.getSession() +``` + +### Use Session + +The `useSession` action provides a reactive way to access the current session. + +```ts client="client.ts" +import { authClient } from "@/lib/client" + +const session = await authClient.useSession() +``` + ### List Sessions The `listSessions` function returns a list of sessions that are active for the user. diff --git a/packages/better-auth/src/api/rate-limiter/rate-limiter.test.ts b/packages/better-auth/src/api/rate-limiter/rate-limiter.test.ts index c3c1976d94..ce5587afe1 100644 --- a/packages/better-auth/src/api/rate-limiter/rate-limiter.test.ts +++ b/packages/better-auth/src/api/rate-limiter/rate-limiter.test.ts @@ -80,7 +80,7 @@ describe( it("non-special-rules limits", async () => { for (let i = 0; i < 25; i++) { - const response = await client.session(); + const response = await client.getSession(); expect(response.error?.status).toBe(i >= 20 ? 429 : 401); } }); @@ -107,7 +107,7 @@ describe("custom rate limiting storage", async () => { }); it("should use custom storage", async () => { - await client.session(); + await client.getSession(); expect(store.size).toBe(2); for (let i = 0; i < 4; i++) { const response = await client.signIn.email({ diff --git a/packages/better-auth/src/api/routes/session.test.ts b/packages/better-auth/src/api/routes/session.test.ts index e5d745f015..5a962883c7 100644 --- a/packages/better-auth/src/api/routes/session.test.ts +++ b/packages/better-auth/src/api/routes/session.test.ts @@ -33,7 +33,7 @@ describe("session", async () => { }); it("should return null when not authenticated", async () => { - const response = await client.session(); + const response = await client.getSession(); expect(response.data).toBeNull(); }); @@ -66,7 +66,7 @@ describe("session", async () => { const nearExpiryDate = new Date(); nearExpiryDate.setDate(nearExpiryDate.getDate() + 6); vi.setSystemTime(nearExpiryDate); - const response = await client.session({ + const response = await client.getSession({ fetchOptions: { headers, }, @@ -110,7 +110,7 @@ describe("session", async () => { expect(new Date(expiresAt).valueOf()).toBeLessThanOrEqual( getDate(1000 * 60 * 60 * 24).valueOf(), ); - const response = await client.session({ + const response = await client.getSession({ fetchOptions: { headers, }, @@ -178,7 +178,7 @@ describe("session", async () => { headers, }, }); - const response = await client.session({ + const response = await client.getSession({ fetchOptions: { headers, }, @@ -230,7 +230,7 @@ describe("session", async () => { }, id: res.data?.session?.id || "", }); - const session = await client.session({ + const session = await client.getSession({ fetchOptions: { headers, }, @@ -269,7 +269,7 @@ describe("session storage", async () => { expect(store.size).toBe(1); const { headers } = await signInWithTestUser(); expect(store.size).toBe(2); - const session = await client.session({ + const session = await client.getSession({ fetchOptions: { headers, }, @@ -315,7 +315,7 @@ describe("session storage", async () => { it("should revoke session", async () => { const { headers } = await signInWithTestUser(); - const session = await client.session({ + const session = await client.getSession({ fetchOptions: { headers, }, @@ -327,7 +327,7 @@ describe("session storage", async () => { }, id: session.data?.session?.id || "", }); - const revokedSession = await client.session({ + const revokedSession = await client.getSession({ fetchOptions: { headers, }, diff --git a/packages/better-auth/src/api/routes/session.ts b/packages/better-auth/src/api/routes/session.ts index d7b1e88677..1d3cc33d1f 100644 --- a/packages/better-auth/src/api/routes/session.ts +++ b/packages/better-auth/src/api/routes/session.ts @@ -12,7 +12,7 @@ import type { export const getSession =