onLinkAccount not triggered when linking anonymous user with social account (BetterAuth v1.2.12)

Hi, I'm running into a problem likely related to #1457. I am building an app using Expo ~53.0.9 on the client and Hono ^4.7.11 on the server. BetterAuth version is v1.2.12. On the client side, I authenticate as an anonymous user and then try to link a Google account. However, the onLinkAccount callback on the server is never triggered. Here is the server-side config:
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: 'postgresql',
}),
baseURL: process.env.SERVER_URL || 'http://localhost:2020',
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID || '',
clientSecret: process.env.GOOGLE_CLIENT_SECRET || '',
},
},
plugins: [
anonymous({
onLinkAccount: async ({ anonymousUser, newUser }) => {
// This is not called
console.info('=== onLinkAccount callback triggered ===');
},
}),
expo(),
bearer(),
],
trustedOrigins: ['http://localhost:8081', 'http://localhost:8082'],
});
export const auth = betterAuth({
database: prismaAdapter(prisma, {
provider: 'postgresql',
}),
baseURL: process.env.SERVER_URL || 'http://localhost:2020',
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID || '',
clientSecret: process.env.GOOGLE_CLIENT_SECRET || '',
},
},
plugins: [
anonymous({
onLinkAccount: async ({ anonymousUser, newUser }) => {
// This is not called
console.info('=== onLinkAccount callback triggered ===');
},
}),
expo(),
bearer(),
],
trustedOrigins: ['http://localhost:8081', 'http://localhost:8082'],
});
And here’s the client-side config:
export const authClient = createAuthClient({
baseURL: `${SERVER_URL}`,
basePath: '/api/auth',
schema: 'MyApp',
plugins: [
expoClient({
scheme: 'MyApp',
storagePrefix: 'MyApp',
storage: SecureStore,
}),
anonymousClient(),
],
});
export const authClient = createAuthClient({
baseURL: `${SERVER_URL}`,
basePath: '/api/auth',
schema: 'MyApp',
plugins: [
expoClient({
scheme: 'MyApp',
storagePrefix: 'MyApp',
storage: SecureStore,
}),
anonymousClient(),
],
});
What ends up happening is: - The existing anonymous user is deleted from the Users table - A new user is created and linked to the account from Google login Is there something missing in my config to enable onLinkAccount to fire? Thanks!
GitHub
better-auth/better-auth
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
1 Reply
poteboy
poteboyOP2mo ago
it seems that onLinkAccount is not called when running without ID token sign-in. If I manually send the provider request from the mobile device and then verify the ID token, onLinkAccount does get triggered.

Did you find this page helpful?