Resolving Discord ID during flow

I'm using BetterAuth as my Oauth handler. What I want to be able to do is extract the authorised users Discord Snowflake User ID during the process to save to my database (prisma). After some digging, I never see that value get exposed. Is this possible?
12 Replies
Ping
Ping4d ago
You can run listAccounts (docs: https://www.better-auth.com/docs/concepts/users-accounts#list-user-accounts) and then find the discord provider, then look for the accountId, as this is the discord user id
User & Accounts | Better Auth
User and account management.
vsw
vswOP4d ago
👀 I'll take a look at this now, thanks for the swift reply
Compiled /api/auth/[...all] in 625ms
GET /api/auth/list-accounts 401 in 2078ms
{ data: null, error: { status: 401, statusText: 'UNAUTHORIZED' } }
GET /dashboard 200 in 6101ms
Compiled /favicon.ico in 173ms
Compiled /api/auth/[...all] in 625ms
GET /api/auth/list-accounts 401 in 2078ms
{ data: null, error: { status: 401, statusText: 'UNAUTHORIZED' } }
GET /dashboard 200 in 6101ms
Compiled /favicon.ico in 173ms
It seems I have a session user (following Discord OAuth) but this method
const accounts = await listAccounts();
console.log(accounts);
const accounts = await listAccounts();
console.log(accounts);
-# from
import { createAuthClient } from "better-auth/react";
export const { signIn, signOut, useSession, listAccounts } = createAuthClient({
baseURL: "http://localhost:3000",
});
import { createAuthClient } from "better-auth/react";
export const { signIn, signOut, useSession, listAccounts } = createAuthClient({
baseURL: "http://localhost:3000",
});
Is throwing unauthorised? :think:
Ping
Ping4d ago
Are you logged in?
vsw
vswOP4d ago
Yes, my session info reflects the account I logged in with I've got a pretty basic page setup for what I'm going for
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { auth } from "@/lib/auth";
import { Button } from "@/components/ui/button";
import Container from "@/components/layouts/container";
import Image from "next/image";
import { EditBalanceForm } from "@/components/forms/editBalance";
import { listAccounts } from "@/lib/auth-client";

async function signOut() {
"use server";
await auth.api.signOut({
headers: await headers(),
});
redirect("/");
}

export default async function DashboardPage() {
const session = await auth.api.getSession({
headers: await headers(),
});

if (!session) {
redirect("/signin");
}

const accounts = await listAccounts();
console.log(accounts);

return (
<Container>
<div className="col-span-full space-y-4">
<div className="flex items-center justify-center size-24 rounded-full overflow-hidden">
<Image
src={session.user.image as string}
alt={session.user.name}
width={300}
height={300}
/>
</div>
<h1>Welcome {session.user.name}</h1>
<EditBalanceForm />
<Button onClick={signOut}>Sign Out</Button>
</div>
</Container>
);
}
import { headers } from "next/headers";
import { redirect } from "next/navigation";
import { auth } from "@/lib/auth";
import { Button } from "@/components/ui/button";
import Container from "@/components/layouts/container";
import Image from "next/image";
import { EditBalanceForm } from "@/components/forms/editBalance";
import { listAccounts } from "@/lib/auth-client";

async function signOut() {
"use server";
await auth.api.signOut({
headers: await headers(),
});
redirect("/");
}

export default async function DashboardPage() {
const session = await auth.api.getSession({
headers: await headers(),
});

if (!session) {
redirect("/signin");
}

const accounts = await listAccounts();
console.log(accounts);

return (
<Container>
<div className="col-span-full space-y-4">
<div className="flex items-center justify-center size-24 rounded-full overflow-hidden">
<Image
src={session.user.image as string}
alt={session.user.name}
width={300}
height={300}
/>
</div>
<h1>Welcome {session.user.name}</h1>
<EditBalanceForm />
<Button onClick={signOut}>Sign Out</Button>
</div>
</Container>
);
}
Ping
Ping4d ago
You're using authClient in a RSC It's ran on the server
vsw
vswOP4d ago
🤦‍♂️ i see
Ping
Ping4d ago
auth.api.listAccounts should fix it ;) Don't forget to pass headers
vsw
vswOP4d ago
do i need to await them 3 separate times or can i save the headers on a page load / capture
No description
Ping
Ping4d ago
I'm not sure what you mean
vsw
vswOP4d ago
as in - literally just const headers = await headers() is that fine to do or non-advisable?
Ping
Ping4d ago
Yeah that should be fine, I don't think it's actually asynchronous, it's mostly there for Next to identify that component to be RSC But yeah
vsw
vswOP4d ago
Thanks for the help! That saved further headache

Did you find this page helpful?