in packages/better-auth/src/db/internal-adapter.ts [530:610]
findOAuthUser: async (
email: string,
accountId: string,
providerId: string,
) => {
// we need to find account first to avoid missing user if the email changed with the provider for the same account
const account = await adapter
.findMany<Account>({
model: "account",
where: [
{
value: accountId,
field: "accountId",
},
],
})
.then((accounts) => {
return accounts.find((a) => a.providerId === providerId);
});
if (account) {
const user = await adapter.findOne<User>({
model: "user",
where: [
{
value: account.userId,
field: "id",
},
],
});
if (user) {
return {
user,
accounts: [account],
};
} else {
const user = await adapter.findOne<User>({
model: "user",
where: [
{
value: email.toLowerCase(),
field: "email",
},
],
});
if (user) {
return {
user,
accounts: [account],
};
}
return null;
}
} else {
const user = await adapter.findOne<User>({
model: "user",
where: [
{
value: email.toLowerCase(),
field: "email",
},
],
});
if (user) {
const accounts = await adapter.findMany<Account>({
model: "account",
where: [
{
value: user.id,
field: "userId",
},
],
});
return {
user,
accounts: accounts || [],
};
} else {
return null;
}
}
},