From 1c7606d61e58e648d37a8741cbea7b4fbda1c00a Mon Sep 17 00:00:00 2001 From: Bereket Engida <86073083+Bekacru@users.noreply.github.com> Date: Mon, 28 Oct 2024 12:33:59 +0300 Subject: [PATCH] refactor: remove user prefix from endpoints (#358) --- demo/nextjs/app/dashboard/user-card.tsx | 8 ++++---- demo/nextjs/lib/auth-client.ts | 1 - docs/content/docs/concepts/session-management.mdx | 8 ++++---- docs/content/docs/concepts/users-accounts.mdx | 12 ++++++------ docs/content/docs/plugins/bearer.mdx | 4 ++-- .../astro-example/src/components/user-card.tsx | 15 +++++---------- examples/astro-example/src/libs/auth-client.ts | 5 ++++- .../nextjs-example/app/dashboard/user-card.tsx | 6 +++--- .../remix-example/app/components/user-card.tsx | 9 ++++----- .../better-auth/src/api/routes/account.test.ts | 8 ++++---- packages/better-auth/src/api/routes/account.ts | 11 ++++------- .../better-auth/src/api/routes/session.test.ts | 8 ++++---- packages/better-auth/src/api/routes/session.ts | 6 +++--- .../src/api/routes/update-user.test.ts | 12 ++++++------ .../better-auth/src/api/routes/update-user.ts | 12 ++++++------ packages/better-auth/src/client/config.ts | 4 ++-- packages/better-auth/src/client/path-to-object.ts | 2 +- packages/better-auth/src/db/db.test.ts | 2 +- .../additional-fields/additional-fields.test.ts | 2 +- packages/better-auth/src/plugins/admin/index.ts | 2 +- .../better-auth/src/plugins/bearer/bearer.test.ts | 2 +- .../src/plugins/phone-number/phone-number.test.ts | 2 +- .../src/plugins/username/username.test.ts | 2 +- 23 files changed, 68 insertions(+), 75 deletions(-) diff --git a/demo/nextjs/app/dashboard/user-card.tsx b/demo/nextjs/app/dashboard/user-card.tsx index 07186fbd8f..324c781fd7 100644 --- a/demo/nextjs/app/dashboard/user-card.tsx +++ b/demo/nextjs/app/dashboard/user-card.tsx @@ -24,7 +24,7 @@ import { import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { PasswordInput } from "@/components/ui/password-input"; -import { client, signOut, user, useSession } from "@/lib/auth-client"; +import { client, signOut, useSession } from "@/lib/auth-client"; import { Session } from "@/lib/auth-types"; import { MobileIcon } from "@radix-ui/react-icons"; import { @@ -180,7 +180,7 @@ export default function UserCard(props: { className="text-red-500 opacity-80 cursor-pointer text-xs border-muted-foreground border-red-600 underline " onClick={async () => { setIsTerminating(session.id); - const res = await client.user.revokeSession({ + const res = await client.revokeSession({ id: session.id, }); @@ -497,7 +497,7 @@ function ChangePassword() { return; } setLoading(true); - const res = await user.changePassword({ + const res = await client.changePassword({ newPassword: newPassword, currentPassword: currentPassword, revokeOtherSessions: signOutDevices, @@ -610,7 +610,7 @@ function EditUserDialog(props: { session: Session | null }) { disabled={isLoading} onClick={async () => { setIsLoading(true); - await user.update({ + await client.updateUser({ image: image ? await convertImageToBase64(image) : undefined, name: name ? name : undefined, fetchOptions: { diff --git a/demo/nextjs/lib/auth-client.ts b/demo/nextjs/lib/auth-client.ts index 6973862e91..bc881960e2 100644 --- a/demo/nextjs/lib/auth-client.ts +++ b/demo/nextjs/lib/auth-client.ts @@ -33,7 +33,6 @@ export const { signIn, signOut, useSession, - user, organization, useListOrganizations, useActiveOrganization, diff --git a/docs/content/docs/concepts/session-management.mdx b/docs/content/docs/concepts/session-management.mdx index c8340679d7..1fe6bebb26 100644 --- a/docs/content/docs/concepts/session-management.mdx +++ b/docs/content/docs/concepts/session-management.mdx @@ -64,7 +64,7 @@ The `listSessions` function returns a list of sessions that are active for the u ```ts title="client.ts" import { authClient } from "@/lib/client" -const sessions = await authClient.user.listSessions() +const sessions = await authClient.listSessions() ``` ### Revoke Session @@ -74,7 +74,7 @@ When a user signs out of a device, the session is automatically ended. However, To end a session, use the `revokeSession` function. Just pass the session ID as a parameter. ```ts title="client.ts" -await authClient.user.revokeSession({ +await authClient.revokeSession({ id: session.id, }) ``` @@ -84,7 +84,7 @@ await authClient.user.revokeSession({ To revoke all sessions, you can use the `revokeSessions` function. ```ts title="client.ts" -await authClient.user.revokeSessions() +await authClient.revokeSessions() ``` ### Revoking Sessions on Password Change @@ -92,7 +92,7 @@ await authClient.user.revokeSessions() You can revoke all sessions when the user changes their password by passing `revokeOtherSessions` true on `changePAssword` function. ```ts title="auth.ts" -await authClient.user.changePassword({ +await authClient.changePassword({ newPassword: newPassword, currentPassword: currentPassword, revokeOtherSessions: signOutDevices, diff --git a/docs/content/docs/concepts/users-accounts.mdx b/docs/content/docs/concepts/users-accounts.mdx index eb41bf26fd..7c3c780bf4 100644 --- a/docs/content/docs/concepts/users-accounts.mdx +++ b/docs/content/docs/concepts/users-accounts.mdx @@ -25,7 +25,7 @@ The user table can be extended by plugins to store additional data. When a plugi To update user information, you can use the `updateUser` function provided by the client. The `updateUser` function takes an object with the following properties: ```ts -await authClient.user.update({ +await authClient.updateUser({ image: "https://example.com/image.jpg", name: "John Doe", }) @@ -66,7 +66,7 @@ export const auth = betterAuth({ Once enabled, use the `changeEmail` function on the client to update a user’s email. The user must verify their current email before changing it. ```ts -await authClient.user.changeEmail({ +await authClient.changeEmail({ newEmail: "new-email@email.com", callbackURL: "/dashboard", //to redirect after verification }); @@ -83,7 +83,7 @@ After verification, the new email is updated in the user table, and a confirmati Password of a user isn't stored in the user table. Instead, it's stored in the account table. To change the password of a user, you can use the `changePassword` function provided by the client. The `changePassword` function takes an object with the following properties: ```ts -await authClient.user.changePassword({ +await authClient.changePassword({ newPassword: "newPassword123", currentPassword: "oldPassword123", revokeOtherSessions: true, // revoke all other sessions the user is signed into @@ -95,7 +95,7 @@ await authClient.user.changePassword({ If a user was registered using oAuth or other providers, they won't have a password. In this case, you can use the `setPassword` function to set a password for the user. This will create a new credential account with the password. ```ts -await authClient.user.setPassword({ +await authClient.setPassword({ password, }); ``` @@ -125,7 +125,7 @@ The account table stores the authentication data of the user. The account table To list user accounts you can use `client.user.listAccounts` method. Which will return all accounts associated with a user. ```ts -const accounts = await authClient.user.listAccounts(); +const accounts = await authClient.listAccounts(); ``` ### Account Linking @@ -165,7 +165,7 @@ Users already signed in can manually link their account to additional social pro - **Linking Social Accounts:** Use the `user.linkSocial` method on the client to link a social provider to the user's account. ```ts - await client.user.linkSocial({ + await client.linkSocial({ provider: "google", // Provider to link callbackURL: "/callback" // Callback URL after linking completes }); diff --git a/docs/content/docs/plugins/bearer.mdx b/docs/content/docs/plugins/bearer.mdx index 5e5392a557..eedf90b571 100644 --- a/docs/content/docs/plugins/bearer.mdx +++ b/docs/content/docs/plugins/bearer.mdx @@ -58,7 +58,7 @@ Now you can make authenticated API calls: ```ts title="client.ts" // This request is automatically authenticated -const { data } = await authClient.user.listSessions(); +const { data } = await authClient.listSessions(); ``` ### 4. Per-Request Token (Optional) @@ -66,7 +66,7 @@ const { data } = await authClient.user.listSessions(); You can also provide the token for individual requests: ```ts title="client.ts" -const { data } = await authClient.user.listSessions({ +const { data } = await authClient.listSessions({ fetchOptions: { headers: { Authorization: `Bearer ${token}` diff --git a/examples/astro-example/src/components/user-card.tsx b/examples/astro-example/src/components/user-card.tsx index d999a2f472..d91138b8f6 100644 --- a/examples/astro-example/src/components/user-card.tsx +++ b/examples/astro-example/src/components/user-card.tsx @@ -3,16 +3,11 @@ import { signOut, twoFactorActions, useListPasskeys, - userActions, useSession, + revokeSession, + updateUser, } from "@/libs/auth-client"; -import { - Card, - CardContent, - CardDescription, - CardHeader, - CardTitle, -} from "./ui/card"; +import { Card, CardContent, CardHeader, CardTitle } from "./ui/card"; import { UAParser } from "ua-parser-js"; import { Image, ImageFallback, ImageRoot } from "./ui/image"; import type { Session, User } from "better-auth/types"; @@ -109,7 +104,7 @@ export function UserCard(props: {