mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-29 10:26:49 -05:00
refactor: return linked account in findOAuthUser (#7331)
Co-authored-by: Maxwell <145994855+ping-maxwell@users.noreply.github.com>
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user