refactor: move session action to getSession (#307)

This commit is contained in:
Bereket Engida
2024-10-23 14:29:37 +03:00
committed by GitHub
parent 8a8759f20f
commit 87e8778e37
12 changed files with 42 additions and 22 deletions

View File

@@ -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.

View File

@@ -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({

View File

@@ -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,
},

View File

@@ -12,7 +12,7 @@ import type {
export const getSession = <Option extends BetterAuthOptions>() =>
createAuthEndpoint(
"/session",
"/get-session",
{
method: "GET",
requireHeaders: true,

View File

@@ -121,7 +121,7 @@ describe("updateUser", async () => {
const cookie = newHeaders.get("cookie");
const oldCookie = headers.get("cookie");
expect(cookie).not.toBe(oldCookie);
const sessionAttempt = await client.session({
const sessionAttempt = await client.getSession({
fetchOptions: {
headers: headers,
},

View File

@@ -80,7 +80,7 @@ describe("adapter test", async () => {
},
});
const { headers } = await signInWithTestUser();
const session = await client.session({
const session = await client.getSession({
fetchOptions: {
headers,
},

View File

@@ -152,7 +152,7 @@ export const createInternalAdapter = (
) => {
const headers = request instanceof Request ? request.headers : request;
const data: Session = {
id: generateId(),
id: generateId(32),
userId,
/**
* If the user doesn't want to be remembered

View File

@@ -145,7 +145,7 @@ describe("additionalFields", async () => {
const client = createAuthClient({
plugins: [inferAdditionalFields<typeof auth>()],
});
type t = Awaited<ReturnType<typeof client.session>>["data"];
type t = Awaited<ReturnType<typeof client.getSession>>["data"];
expectTypeOf<t>().toMatchTypeOf<{
user: {
id: string;
@@ -174,7 +174,7 @@ describe("additionalFields", async () => {
}),
],
});
type t = Awaited<ReturnType<typeof client.session>>["data"];
type t = Awaited<ReturnType<typeof client.getSession>>["data"];
expectTypeOf<t>().toMatchTypeOf<{
user: {
id: string;

View File

@@ -12,7 +12,7 @@ describe("bearer", async () => {
it("should get session", async () => {
const { res, headers } = await signInWithTestUser();
token = res.data?.session.id || "";
const session = await client.session({
const session = await client.getSession({
fetchOptions: {
headers: {
authorization: `Bearer ${token}`,
@@ -48,7 +48,7 @@ describe("bearer", async () => {
});
it("should work with encrypted token", async () => {
const session = await client.session({
const session = await client.getSession({
fetchOptions: {
headers: {
authorization: `Bearer ${encryptedToken}`,

View File

@@ -64,7 +64,7 @@ describe("multi-session", async () => {
});
it("should get active session", async () => {
const session = await client.session({
const session = await client.getSession({
fetchOptions: {
headers,
},

View File

@@ -78,7 +78,7 @@ describe("organization", async (it) => {
});
expect(organization.data?.id).toBe(orgId);
const session = await client.session({
const session = await client.getSession({
fetchOptions: {
headers,
},
@@ -137,7 +137,7 @@ describe("organization", async (it) => {
},
});
expect(invitation.data?.invitation.status).toBe("accepted");
const invitedUserSession = await client.session({
const invitedUserSession = await client.getSession({
fetchOptions: {
headers: headers2,
},

View File

@@ -80,7 +80,7 @@ describe("phone-number", async (it) => {
headers,
},
});
const user = await client.session({
const user = await client.getSession({
fetchOptions: {
headers,
},