Skip to content

Commit

Permalink
refactor: remove user prefix from endpoints (better-auth#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bekacru authored Oct 28, 2024
1 parent 463d794 commit 1c7606d
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 75 deletions.
8 changes: 4 additions & 4 deletions demo/nextjs/app/dashboard/user-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
});

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down
1 change: 0 additions & 1 deletion demo/nextjs/lib/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const {
signIn,
signOut,
useSession,
user,
organization,
useListOrganizations,
useActiveOrganization,
Expand Down
8 changes: 4 additions & 4 deletions docs/content/docs/concepts/session-management.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
})
```
Expand All @@ -84,15 +84,15 @@ 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

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,
Expand Down
12 changes: 6 additions & 6 deletions docs/content/docs/concepts/users-accounts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
Expand Down Expand Up @@ -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
});
Expand All @@ -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
Expand All @@ -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,
});
```
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
});
Expand Down
4 changes: 2 additions & 2 deletions docs/content/docs/plugins/bearer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ 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)

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}`
Expand Down
15 changes: 5 additions & 10 deletions examples/astro-example/src/components/user-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -109,7 +104,7 @@ export function UserCard(props: {
<button
class="text-red-500 opacity-80 cursor-pointer text-xs border-muted-foreground border-red-600 underline "
onClick={async () => {
const res = await userActions.revokeSession({
const res = await revokeSession({
id: activeSession.id,
});

Expand Down Expand Up @@ -216,7 +211,7 @@ function EditUserDialog(props: { user?: User }) {
<Button
onClick={async () => {
setIsLoading(true);
await userActions.update({
await updateUser({
image: image()
? await convertImageToBase64(image()!)
: undefined,
Expand Down
5 changes: 4 additions & 1 deletion examples/astro-example/src/libs/auth-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ export const {
signUp,
passkey: passkeyActions,
useListPasskeys,
user: userActions,
twoFactor: twoFactorActions,
$Infer,
updateUser,
changePassword,
revokeSession,
revokeSessions,
} = createAuthClient({
baseURL:
process.env.NODE_ENV === "development"
Expand Down
6 changes: 3 additions & 3 deletions examples/nextjs-example/app/dashboard/user-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down
9 changes: 4 additions & 5 deletions examples/remix-example/app/components/user-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Avatar, AvatarFallback, AvatarImage } from "~/components/ui/avatar";
import { Button } from "~/components/ui/button";
import {
Card,
CardDescription,
CardContent,
CardFooter,
CardHeader,
Expand Down Expand Up @@ -84,7 +83,7 @@ export default function UserCard() {
useState<boolean>(false);

const { data: activeSessions } = useSWR("/sessions", async () => {
return (await authClient.user.listSessions()).data;
return (await authClient.listSessions()).data;
});
return (
<Card>
Expand Down Expand Up @@ -174,7 +173,7 @@ export default function UserCard() {
className="text-red-500 opacity-80 cursor-pointer text-xs border-muted-foreground border-red-600 underline "
onClick={async () => {
setIsTerminating(activeSession.id);
const res = await authClient.user.revokeSession({
const res = await authClient.revokeSession({
id: activeSession.id,
});

Expand Down Expand Up @@ -453,7 +452,7 @@ function ChangePassword() {
return;
}
setLoading(true);
const res = await authClient.user.changePassword({
const res = await authClient.changePassword({
newPassword: newPassword,
currentPassword: currentPassword,
revokeOtherSessions: signOutDevices,
Expand Down Expand Up @@ -564,7 +563,7 @@ function EditUserDialog(props: { session: Session | null }) {
disabled={isLoading}
onClick={async () => {
setIsLoading(true);
await authClient.user.update({
await authClient.updateUser({
image: image ? await convertImageToBase64(image) : undefined,
name: name ? name : undefined,
fetchOptions: {
Expand Down
8 changes: 4 additions & 4 deletions packages/better-auth/src/api/routes/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("account", async () => {
});
const { headers } = await signInWithTestUser();
it("should list all accounts", async () => {
const accounts = await client.user.listAccounts({
const accounts = await client.listAccounts({
fetchOptions: {
headers,
},
Expand All @@ -67,7 +67,7 @@ describe("account", async () => {
});

it("should link account", async () => {
const linkAccountRes = await client.user.linkSocial(
const linkAccountRes = await client.linkSocial(
{
provider: "google",
callbackURL: "/callback",
Expand Down Expand Up @@ -110,15 +110,15 @@ describe("account", async () => {
},
});
const { headers: headers2 } = await signInWithTestUser();
const accounts = await client.user.listAccounts({
const accounts = await client.listAccounts({
fetchOptions: { headers: headers2 },
});
expect(accounts.data?.length).toBe(2);
});

it("shouldn't link existing account", async () => {
const { headers: headers2 } = await signInWithTestUser();
const linkAccountRes = await client.user.linkSocial(
const linkAccountRes = await client.linkSocial(
{
provider: "google",
callbackURL: "/callback",
Expand Down
11 changes: 4 additions & 7 deletions packages/better-auth/src/api/routes/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import { z } from "zod";
import { createAuthEndpoint } from "../call";
import { socialProviderList } from "../../social-providers";
import { APIError } from "better-call";
import { generateState, parseState, type OAuth2Tokens } from "../../oauth2";
import { generateState } from "../../oauth2";
import { generateCodeVerifier } from "oslo/oauth2";
import { HIDE_METADATA, logger } from "../../utils";
import { compareHash } from "../../crypto/hash";
import { getSessionFromCtx, sessionMiddleware } from "./session";
import { userSchema } from "../../db/schema";
import { sessionMiddleware } from "./session";

export const listUserAccounts = createAuthEndpoint(
"/user/list-accounts",
"/list-accounts",
{
method: "GET",
use: [sessionMiddleware],
Expand All @@ -25,7 +22,7 @@ export const listUserAccounts = createAuthEndpoint(
);

export const linkSocialAccount = createAuthEndpoint(
"/user/link-social",
"/link-social",
{
method: "POST",
requireHeaders: true,
Expand Down
8 changes: 4 additions & 4 deletions packages/better-auth/src/api/routes/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe("session", async () => {
},
);

const response = await client.user.listSessions({
const response = await client.listSessions({
fetchOptions: {
headers,
},
Expand All @@ -227,7 +227,7 @@ describe("session", async () => {
onSuccess: sessionSetter(headers2),
},
});
await client.user.revokeSession({
await client.revokeSession({
fetchOptions: {
headers,
},
Expand All @@ -239,7 +239,7 @@ describe("session", async () => {
},
});
expect(session.data).toBeNull();
const revokeRes = await client.user.revokeSessions({
const revokeRes = await client.revokeSessions({
fetchOptions: {
headers: headers2,
},
Expand Down Expand Up @@ -324,7 +324,7 @@ describe("session storage", async () => {
},
});
expect(session.data).not.toBeNull();
const res = await client.user.revokeSession({
const res = await client.revokeSession({
fetchOptions: {
headers,
},
Expand Down
Loading

0 comments on commit 1c7606d

Please sign in to comment.