dexsnake
dexsnake
BABetter Auth
Created by dexsnake on 4/11/2025 in #help
Refresh provider access token using genericOAuth plugin
I am looking for a way to refresh the access token from my custom oauth provider. I can see it and the refresh token along with the expiration stored in the account table in the DB, but I'm not sure how to update them. Here is my config setup:
export const auth = betterAuth({
session: {
expiresIn: 15 * 60,
updateAge: 15 * 60
},
database: new Pool({
connectionString: process.env.DATABASE_URL
}),
plugins: [
customSession(async ({ session, user }) => {
const supabase = await createClient()
const { data, error } = await supabase.from('account').select('*').eq('userId', user.id).single()
if (error) throw new Error(error.message)
return {
user,
session: {
...session,
accessToken: data.accessToken
}
}
}),
genericOAuth({
config: [
{
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
providerId: 'customOAuth2ProviderId',
scopes: ['openid'],
authorizationUrl: `${process.env.AUTHORIZATION_URL}`,
tokenUrl: `${process.env.TOKEN_URL}`,
userInfoUrl: `${process.env.USER_INFO_URL}`

}
]
})
]
})
export const auth = betterAuth({
session: {
expiresIn: 15 * 60,
updateAge: 15 * 60
},
database: new Pool({
connectionString: process.env.DATABASE_URL
}),
plugins: [
customSession(async ({ session, user }) => {
const supabase = await createClient()
const { data, error } = await supabase.from('account').select('*').eq('userId', user.id).single()
if (error) throw new Error(error.message)
return {
user,
session: {
...session,
accessToken: data.accessToken
}
}
}),
genericOAuth({
config: [
{
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
providerId: 'customOAuth2ProviderId',
scopes: ['openid'],
authorizationUrl: `${process.env.AUTHORIZATION_URL}`,
tokenUrl: `${process.env.TOKEN_URL}`,
userInfoUrl: `${process.env.USER_INFO_URL}`

}
]
})
]
})
I dont see anywhere in the docs about how to go about refreshing the providers access token using the refresh token/endpoint. When using socialProviders option, there is a refreshAccessToken function that you can call, but i'm not sure how to go about this with the genericOAuth plugin.
2 replies