Using auth.api.getAccessToken with Prisma + GitHub throws FAILED_TO_GET_A_VALID_ACCESS_TOKEN - 403 Forbidden #1300

Closed
opened 2026-03-13 08:31:56 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @MathiasWP on GitHub (Jun 3, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

I'm using BetterAuth on a SvelteKit project with Prisma. I have generated the prisma schemas, and they all work as expected. However, when i try to the following in my SvelteKit server load method the auth.api.getAccessToken throws a FAILED_TO_GET_A_VALID_ACCESS_TOKEN 403 Forbidden error:

const { accessToken } = await auth.api.getAccessToken({
	body: {
		providerId: 'github'
	},
	headers: request.headers
});

My current workaround is to do the following:

import { auth } from '$auth/config';
import { prisma } from '$prisma/client';
import { redirect } from '@sveltejs/kit';
import type { LayoutServerLoad } from './$types';

export const load: LayoutServerLoad = async ({ request }) => {
	const session = await auth.api.getSession({
		headers: request.headers
	});

	if (!session) {
		redirect(302, '/sign-in');
	}

	const account = await prisma.account.findFirst({
		where: {
			providerId: 'github',
			userId: session.user.id
		}
	});

	// For some reason this throws a FAILED_TO_GET_A_VALID_ACCESS_TOKEN with 403 Forbidden
	// May need to create an issue on the betterauth repo, or try to figure out why this happens
	// const { accessToken } = await auth.api.getAccessToken({
	// 	body: {
	// 		providerId: 'github'
	// 	},
	// 	headers: request.headers
	// });

	if (!account?.accessToken) {
		redirect(302, '/sign-in');
	}

	return {
		session,
		accessToken: account.accessToken
	};
};

So the access token is correctly stored in my Prisma DataBase (as far as i can understand). Why does the auth.api.getAccessToken fail?

Current vs. Expected behavior

I'm expecting this method to work out of the box, since my Prisma schemas are the exact ones generated from BetterAuth.

What version of Better Auth are you using?

1.2.8

Provide environment information

- OS: MacOs 15.5
- NodeJS 22
- pnpm 10
- `prisma` `6.8.2` with `@prisma/extension-accelerate` `2.0.1`

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

export const auth = betterAuth({
	database: prismaAdapter(prisma, {
		provider: 'postgresql'
	}),
	baseURL: BETTER_AUTH_URL,
	secret: BETTER_AUTH_SECRET,
	socialProviders: {
		github: {
			clientId: OAUTH_GITHUB_CLIENT_ID,
			clientSecret: OAUTH_GITHUB_CLIENT_SECRET,
			scope: ['repo', 'read:org']
		}
	}
});

Additional context

My Prisma Client setup:

import { PRISMA_DATABASE_URL } from '$env/static/private';
import { withAccelerate } from '@prisma/extension-accelerate';
import { PrismaClient } from '../generated/prisma/client';

const prisma = new PrismaClient({
	datasources: {
		db: {
			url: PRISMA_DATABASE_URL
		}
	}
}).$extends(withAccelerate());

export { prisma };
Originally created by @MathiasWP on GitHub (Jun 3, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce I'm using BetterAuth on a SvelteKit project with Prisma. I have generated the prisma schemas, and they all work as expected. However, when i try to the following in my SvelteKit server load method the `auth.api.getAccessToken` throws a FAILED_TO_GET_A_VALID_ACCESS_TOKEN 403 Forbidden error: ```ts const { accessToken } = await auth.api.getAccessToken({ body: { providerId: 'github' }, headers: request.headers }); ``` My current workaround is to do the following: ```ts import { auth } from '$auth/config'; import { prisma } from '$prisma/client'; import { redirect } from '@sveltejs/kit'; import type { LayoutServerLoad } from './$types'; export const load: LayoutServerLoad = async ({ request }) => { const session = await auth.api.getSession({ headers: request.headers }); if (!session) { redirect(302, '/sign-in'); } const account = await prisma.account.findFirst({ where: { providerId: 'github', userId: session.user.id } }); // For some reason this throws a FAILED_TO_GET_A_VALID_ACCESS_TOKEN with 403 Forbidden // May need to create an issue on the betterauth repo, or try to figure out why this happens // const { accessToken } = await auth.api.getAccessToken({ // body: { // providerId: 'github' // }, // headers: request.headers // }); if (!account?.accessToken) { redirect(302, '/sign-in'); } return { session, accessToken: account.accessToken }; }; ``` So the access token is correctly stored in my Prisma DataBase (as far as i can understand). Why does the `auth.api.getAccessToken` fail? ### Current vs. Expected behavior I'm expecting this method to work out of the box, since my Prisma schemas are the exact ones generated from BetterAuth. ### What version of Better Auth are you using? 1.2.8 ### Provide environment information ```bash - OS: MacOs 15.5 - NodeJS 22 - pnpm 10 - `prisma` `6.8.2` with `@prisma/extension-accelerate` `2.0.1` ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript export const auth = betterAuth({ database: prismaAdapter(prisma, { provider: 'postgresql' }), baseURL: BETTER_AUTH_URL, secret: BETTER_AUTH_SECRET, socialProviders: { github: { clientId: OAUTH_GITHUB_CLIENT_ID, clientSecret: OAUTH_GITHUB_CLIENT_SECRET, scope: ['repo', 'read:org'] } } }); ``` ### Additional context My Prisma Client setup: ```ts import { PRISMA_DATABASE_URL } from '$env/static/private'; import { withAccelerate } from '@prisma/extension-accelerate'; import { PrismaClient } from '../generated/prisma/client'; const prisma = new PrismaClient({ datasources: { db: { url: PRISMA_DATABASE_URL } } }).$extends(withAccelerate()); export { prisma }; ```
Author
Owner

@chroxify commented on GitHub (Jun 6, 2025):

Hey @MathiasWP, just ran into this myself so dug into it a little and seems like this was related to #2764 which was merged with v1.2.9-beta.6 and upwards. Upgrading to it fixed this!

@chroxify commented on GitHub (Jun 6, 2025): Hey @MathiasWP, just ran into this myself so dug into it a little and seems like this was related to #2764 which was merged with [v1.2.9-beta.6](https://github.com/better-auth/better-auth/releases/tag/v1.2.9-beta.6) and upwards. Upgrading to it fixed this!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1300