refactor: return linked account in findOAuthUser (#7331)

Co-authored-by: Maxwell <145994855+ping-maxwell@users.noreply.github.com>
This commit is contained in:
Oscar Cornish
2026-01-19 20:24:32 +00:00
committed by Alex Yang
parent 08afa10337
commit feb2084f58
3 changed files with 35 additions and 24 deletions

View File

@@ -721,26 +721,29 @@ export const createInternalAdapter = (
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 (await getCurrentAdapter(adapter))
.findMany<Account & { user: User | null }>({
model: "account",
where: [
{
value: accountId,
field: "accountId",
},
],
join: {
user: true,
const account = await (await getCurrentAdapter(adapter)).findOne<
Account & { user: User | null }
>({
model: "account",
where: [
{
value: accountId,
field: "accountId",
},
})
.then((accounts) => {
return accounts.find((a) => a.providerId === providerId);
});
{
value: providerId,
field: "providerId",
},
],
join: {
user: true,
},
});
if (account) {
if (account.user) {
return {
user: account.user,
linkedAccount: account,
accounts: [account],
};
} else {
@@ -756,6 +759,7 @@ export const createInternalAdapter = (
if (user) {
return {
user,
linkedAccount: account,
accounts: [account],
};
}
@@ -785,6 +789,7 @@ export const createInternalAdapter = (
});
return {
user,
linkedAccount: null,
accounts: accounts || [],
};
} else {

View File

@@ -37,12 +37,14 @@ export async function handleOAuthUserInfo(
const isRegister = !user;
if (dbUser) {
const hasBeenLinked = dbUser.accounts.find(
(a) =>
a.providerId === account.providerId &&
a.accountId === account.accountId,
);
if (!hasBeenLinked) {
const linkedAccount =
dbUser.linkedAccount ??
dbUser.accounts.find(
(acc) =>
acc.providerId === account.providerId &&
acc.accountId === account.accountId,
);
if (!linkedAccount) {
const trustedProviders =
c.context.options.account?.accountLinking?.trustedProviders;
const isTrustedProvider =
@@ -111,14 +113,14 @@ export async function handleOAuthUserInfo(
if (c.context.options.account?.storeAccountCookie) {
await setAccountCookie(c, {
...hasBeenLinked,
...linkedAccount,
...freshTokens,
});
}
if (Object.keys(freshTokens).length > 0) {
await c.context.internalAdapter.updateAccount(
hasBeenLinked.id,
linkedAccount.id,
freshTokens,
);
}

View File

@@ -91,7 +91,11 @@ export interface InternalAdapter<
email: string,
accountId: string,
providerId: string,
): Promise<{ user: User; accounts: Account[] } | null>;
): Promise<{
user: User;
linkedAccount: Account | null;
accounts: Account[];
} | null>;
findUserByEmail(
email: string,