Persist an OAuth/OIDC account
An OAuth or OIDC sign-in creates a provider account for a user.
Auth.js · Enum
linkAccount is an optional Auth.js adapter method that persists an account connection in the database. Auth.js uses it internally, and applications may use it for manual linking.
The optional adapter method receives an account object and creates its database record. Auth.js invokes it internally when an account is linked, while an adapter may also expose it for manual linking; the method can resolve to no value, null, or the stored account.
optional linkAccount(account): Promise<void> | Awaitable<undefined | null | AdapterAccount>| Name | Type | Description |
|---|---|---|
account | AdapterAccount | The account connection to persist for the user and provider. |
Promise<void> | Awaitable<undefined | null | AdapterAccount>
linkAccount is an optional method on the adapter interface and accepts one account argument.undefined, null, or an AdapterAccount.import type { Adapter, AdapterAccount } from "next-auth/adapters";
export const adapter: Adapter = {
async linkAccount(account: AdapterAccount) {
await db.account.create({ data: account });
return account;
},
};Implement the adapter method to persist the account record, then return the created account.
An OAuth or OIDC sign-in creates a provider account for a user.
An Email provider sign-in creates an associated account record.
A signed-in user intentionally connects another provider.
linkAccount do and what can it return?linkAccount is an optional adapter method that receives an account and creates the corresponding Account record in the database. Its return type may resolve to void, undefined, null, or an AdapterAccount.
Follow-up: Why is the method optional? The core flow can use an adapter implementation when account persistence is configured, while adapters that do not support this operation need not expose it.
linkAccount called?Auth.js invokes the method internally when it needs to persist a linked account. The same method can optionally support an application's manual account-linking flow.
Follow-up: What must manual linking additionally protect against? The application should authorize the signed-in user and validate the account association before writing it.
An account connects a user to a provider, and one user may have multiple accounts. OAuth/OIDC accounts arise from OAuth sign-ins, while Email accounts arise from Email-provider sign-ins.
Follow-up: How would you model the relationship? Store multiple account rows for one user, with each row identifying its provider connection.
AdapterAdapterAccountunlinkAccountcreateUsergetUserByAccount