Differentiate Sessions And Users Based On Multiple Social Providers

Hi! I am currently working on a solution to tie sessions to an account rather than the user, I was wondering if there is already an existing implementation or would I have to modify the Core Schema of Better-Auth? Note: I see this pull request has been merged for better-auth v1.2.8-beta.1, would it be applicable to my scenario? Versions - better-auth v1.2.7 - @tanstack/react-start v1.116.1 Description Authenticating via social provider leads to 1 record in the users table and 1 record in the accounts table being created. The issue is when the user logs in with another social provider that uses the same email as their previous login, no new user record is created since the email column has a unique constraint but a new account is created. Since the sessions table is tied to the users table, it will use the previously saved user image and the session itself cannot tell which account they are logged in as. Question How would I tell what account I am logged in as based on the current session rather than the user that is logged in? So far I have attempted to read the context in my beforeCreate hook for sessions after passing the provider_id via the beforeCreateAuthMiddleware hook but the context is null in this scope.
databaseHooks: {
session: {
create: {
before: async (session, context) => {
console.log(context?.params.id)

// add this to the session, create migration to add provider_id column to sessions table
}
}
}
},
hooks: {
before: createAuthMiddleware(async (context) => {
if (!context.params?.id) {
return
}

return {
context: {
...context,
body: {
...context.body,
provider: context.params.id
}
}
}
})
},
databaseHooks: {
session: {
create: {
before: async (session, context) => {
console.log(context?.params.id)

// add this to the session, create migration to add provider_id column to sessions table
}
}
}
},
hooks: {
before: createAuthMiddleware(async (context) => {
if (!context.params?.id) {
return
}

return {
context: {
...context,
body: {
...context.body,
provider: context.params.id
}
}
}
})
},
Thanks in advance!
No description
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?