invocation97
invocation97
BABetter Auth
Created by invocation97 on 2/27/2025 in #help
Bearer Authorisation on request time using better-fetch
Hey everyone, I just swapped our custom http client to better fetch, and I am really happy so far. However, I have 2 questions that would save my life: 1. Since the type of app we're building is an admin app which should attach the Bearer ${access_token} in 99.99% of the requests, I was wondering if there was a way to use the init plugin or just the onRequest hook to attach it without having to call my const session = await auth() and then to attach the
auth: {
type: "Bearer",
token: session?.user?.accessToken
}
auth: {
type: "Bearer",
token: session?.user?.accessToken
}
in each request (as described in the docs). The token callback function can't be async, so I know i can't attach the header there 2. It's really cool that you provide a logger, but I was wondering if this logger can be used in the app as well, or is it strictly bound to
plugins: [
logger(),
],
plugins: [
logger(),
],
2 replies
BABetter Auth
Created by invocation97 on 2/7/2025 in #help
listUsers returns status 401 UNAUTHORIZED even though the current user is an admin.
I am using the admin() and adminClient() plugins while testing out better-auth. I am trying to list out the users using the auth client, but I am getting a 401 unauthorized. I've registered a user and manually changed the role to admin in the database. I am using nextjs15 Here's the code:
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { authClient } from "@/lib/auth-client";
import { verifySession } from "@/lib/verify-session";

export default async function UsersPage() {
const { user } = await verifySession();

console.log("CURRENT USER: ", user);

const { data } = await authClient.admin.listUsers({
query: {
limit: 10,
offset: 0,
},
});

const users = data?.users;

return (
<div className="space-y-4">
<h1 className="text-3xl font-bold">Users</h1>
<Card>
<CardHeader>
<CardTitle>Users</CardTitle>
</CardHeader>
<CardContent>
<ul>
{users && users.map((user) => <li key={user.id}>{user.email}</li>)}
</ul>
</CardContent>
</Card>
</div>
);
}
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { authClient } from "@/lib/auth-client";
import { verifySession } from "@/lib/verify-session";

export default async function UsersPage() {
const { user } = await verifySession();

console.log("CURRENT USER: ", user);

const { data } = await authClient.admin.listUsers({
query: {
limit: 10,
offset: 0,
},
});

const users = data?.users;

return (
<div className="space-y-4">
<h1 className="text-3xl font-bold">Users</h1>
<Card>
<CardHeader>
<CardTitle>Users</CardTitle>
</CardHeader>
<CardContent>
<ul>
{users && users.map((user) => <li key={user.id}>{user.email}</li>)}
</ul>
</CardContent>
</Card>
</div>
);
}
Here are the console logs:
CURRENT USER: {
id: 'ZjLohn28IT3OHklqxwAdspRV1Om2X0Ss',
name: 'xxxx',
emailVerified: true,
image: null,
createdAt: 2025-02-07T10:24:09.645Z,
updatedAt: 2025-02-07T10:24:09.645Z,
role: 'admin',
banned: null,
banReason: null,
banExpires: null
}
✓ Compiled /api/auth/[...all] in 180ms (1197 modules)
GET /api/auth/admin/list-users?limit=10&offset=0 401 in 345ms
CURRENT USER: {
id: 'ZjLohn28IT3OHklqxwAdspRV1Om2X0Ss',
name: 'xxxx',
emailVerified: true,
image: null,
createdAt: 2025-02-07T10:24:09.645Z,
updatedAt: 2025-02-07T10:24:09.645Z,
role: 'admin',
banned: null,
banReason: null,
banExpires: null
}
✓ Compiled /api/auth/[...all] in 180ms (1197 modules)
GET /api/auth/admin/list-users?limit=10&offset=0 401 in 345ms
What am I missing? 😅
7 replies