跳到內容
從 NextAuth.js v4 遷移?閱讀 我們的遷移指南.

adapters

适配器

适配器是一个具有函数属性(方法)的对象,它从数据源读取和写入数据。可以将这些方法视为将数据层标准化为 Auth.js 可以理解的通用接口的一种方式。

这使得 Auth.js 非常灵活,并允许它与任何数据层一起使用。

适配器方法用于执行以下操作

  • 创建/更新/删除用户
  • 将帐户链接/取消链接到/从用户
  • 处理活动会话
  • 支持跨多个设备的无密码身份验证

如果未实现任何方法,但 Auth.js 调用了这些方法,则会向用户显示错误,并且操作将失败。

方法

createAuthenticator()?

optional createAuthenticator(authenticator): Awaitable<AdapterAuthenticator>

创建一个新的验证器。

如果创建失败,适配器必须抛出错误。

参数
参数类型
验证器AdapterAuthenticator
返回

Awaitable<AdapterAuthenticator>

createSession()?

optional createSession(session): Awaitable<AdapterSession>

为用户创建一个会话并返回它。

另请参阅数据库会话管理

参数
参数类型
session对象
session.expiresDate
session.sessionTokenstring
session.userIdstring
返回

Awaitable<AdapterSession>

createUser()?

optional createUser(user): Awaitable<AdapterUser>

在数据库中创建一个用户并返回它。

另请参阅用户管理

参数
参数类型
userAdapterUser
返回

Awaitable<AdapterUser>

createVerificationToken()?

optional createVerificationToken(verificationToken): Awaitable<undefined | null | VerificationToken>

创建一个验证令牌并返回它。

另请参阅验证令牌

参数
参数类型
verificationTokenVerificationToken
返回

Awaitable<undefined | null | VerificationToken>

deleteSession()?

optional deleteSession(sessionToken): Promise<void> | Awaitable<undefined | null | AdapterSession>

从数据库中删除会话。最好此方法也返回正在删除的会话以进行日志记录。

另请参阅数据库会话管理

参数
参数类型
sessionTokenstring
返回

Promise<void> | Awaitable<undefined | null | AdapterSession>

deleteUser()?

optional deleteUser(userId): Promise<void> | Awaitable<undefined | null | AdapterUser>
參數
参数类型
userIdstring
返回

Promise<void> | Awaitable<undefined | null | AdapterUser>

待辦事項

此方法目前尚未被調用。

另请参阅用户管理

getAccount()?

optional getAccount(providerAccountId, provider): Awaitable<null | AdapterAccount>

通過提供者帳戶 ID 和提供者獲取帳戶。

如果找不到帳戶,適配器必須返回 null

參數
参数类型
providerAccountIdstring
providerstring
返回

Awaitable<null | AdapterAccount>

getAuthenticator()?

optional getAuthenticator(credentialID): Awaitable<null | AdapterAuthenticator>

從其 credentialID 返回驗證器。

如果找不到驗證器,適配器必須返回 null

參數
参数类型
credentialIDstring
返回

Awaitable<null | AdapterAuthenticator>

getSessionAndUser()?

optional getSessionAndUser(sessionToken): Awaitable<null | {
  session: AdapterSession;
  user: AdapterUser;
}>

一次性從資料庫返回一個會話和一個使用者。

💡

如果資料庫支援 joins,建議減少資料庫查詢的數量。

另请参阅数据库会话管理

參數
参数类型
sessionTokenstring
返回

Awaitable<null | { session: AdapterSession; user: AdapterUser; }>

getUser()?

optional getUser(id): Awaitable<null | AdapterUser>

通過使用者 ID 從資料庫返回使用者。

另请参阅用户管理

參數
参数类型
idstring
返回

Awaitable<null | AdapterUser>

getUserByAccount()?

optional getUserByAccount(providerAccountId): Awaitable<null | AdapterUser>

使用提供者 ID 和特定帳戶的使用者 ID,獲取使用者。

另请参阅用户管理

參數
参数类型
providerAccountIdPick<AdapterAccount, "provider" | "providerAccountId">
返回

Awaitable<null | AdapterUser>

getUserByEmail()?

optional getUserByEmail(email): Awaitable<null | AdapterUser>

通過使用者的電子郵件地址從資料庫返回使用者。

另请参阅验证令牌

參數
参数类型
emailstring
返回

Awaitable<null | AdapterUser>

linkAccount()?

optional linkAccount(account): Promise<void> | Awaitable<undefined | null | AdapterAccount>

此方法在內部調用(但可以選擇用於手動連結)。它在資料庫中建立一個帳戶

另请参阅用户管理

參數
参数类型
帳戶AdapterAccount
返回

Promise<void> | Awaitable<undefined | null | AdapterAccount>

listAuthenticatorsByUserId()?

optional listAuthenticatorsByUserId(userId): Awaitable<AdapterAuthenticator[]>

返回來自使用者的所有驗證器。

如果找不到使用者,適配器仍應返回一個空陣列。如果由於其他原因檢索失敗,適配器必須拋出錯誤。

參數
参数类型
userIdstring
返回

Awaitable<AdapterAuthenticator[]>

unlinkAccount()?

optional unlinkAccount(providerAccountId): Promise<void> | Awaitable<undefined | AdapterAccount>
參數
参数类型
providerAccountIdPick<AdapterAccount, "provider" | "providerAccountId">
返回

Promise<void> | Awaitable<undefined | AdapterAccount>

待辦事項

此方法目前尚未被調用。

updateAuthenticatorCounter()?

optional updateAuthenticatorCounter(credentialID, newCounter): Awaitable<AdapterAuthenticator>

更新驗證器的計數器。

如果更新失敗,適配器必須拋出錯誤。

參數
参数类型
credentialIDstring
newCounter數字
返回

Awaitable<AdapterAuthenticator>

updateSession()?

optional updateSession(session): Awaitable<undefined | null | AdapterSession>

更新資料庫中的會話並返回它。

另请参阅数据库会话管理

參數
参数类型
sessionPartial<AdapterSession> & Pick<AdapterSession, "sessionToken">
返回

Awaitable<undefined | null | AdapterSession>

updateUser()?

optional updateUser(user): Awaitable<AdapterUser>

更新資料庫中的使用者並返回它。

另请参阅用户管理

參數
参数类型
userPartial<AdapterUser> & Pick<AdapterUser, "id">
返回

Awaitable<AdapterUser>

useVerificationToken()?

optional useVerificationToken(params): Awaitable<null | VerificationToken>

從資料庫返回驗證令牌並將其刪除,以便它只能使用一次。

另请参阅验证令牌

參數
参数类型
params对象
params.identifierstring
params.tokenstring
返回

Awaitable<null | VerificationToken>


AdapterAccount

帳戶是使用者和提供者之間的連線。

帳戶有兩種型別

  • OAuth/OIDC 帳戶,當使用者使用 OAuth 提供者登入時建立。
  • 電子郵件帳戶,當使用者使用電子郵件提供者登入時建立。

一個使用者可以有多個帳戶。

延伸自

屬性

access_token?

optional readonly access_token: string;
繼承自

Account.access_token

authorization_details?

optional readonly authorization_details: AuthorizationDetails[];
繼承自

Account.authorization_details

expires_at?

optional expires_at: number;

根據 TokenEndpointResponse.expires_in 計算的值。

它是 TokenEndpointResponse.access_token 過期的絕對時間戳記(以秒為單位)。

此值可用於與 TokenEndpointResponse.refresh_token 一起實現令牌輪換。

請參閱
繼承自

Account.expires_at

expires_in?

optional readonly expires_in: number;
繼承自

Account.expires_in

id_token?

optional readonly id_token: string;
繼承自

Account.id_token

provider

provider: string;

此帳戶的供應商 ID。例如「google」。請參閱完整列表:https://authjs.dev.org.tw/reference/core/providers

繼承自

Account.provider

providerAccountId

providerAccountId: string;

此值取決於用於建立帳戶的供應商類型。

  • oauth/oidc:OAuth 帳戶的 ID,從 profile() 回呼返回。
  • email:使用者的電子郵件地址。
  • credentials:從 authorize() 回呼返回的 id
繼承自

Account.providerAccountId

refresh_token?

optional readonly refresh_token: string;
繼承自

Account.refresh_token

scope?

optional readonly scope: string;
繼承自

Account.scope

token_type?

optional readonly token_type: Lowercase<string>;

注意:因為該值不區分大小寫,所以總是返回小寫

繼承自

Account.token_type

type

type: AdapterAccountType;

此帳戶的供應商類型

覆寫

Account.type

userId

userId: string;

此帳戶所屬使用者的 ID

請參閱

https://authjs.dev.org.tw/reference/core/adapters#adapteruser

覆寫

Account.userId


AdapterAuthenticator

驗證器表示分配給使用者的憑證驗證器。

擴展

屬性

counter

counter: number;

驗證器已使用的次數。

繼承自

Authenticator.counter

credentialBackedUp

credentialBackedUp: boolean;

客戶端驗證器是否備份憑證。

繼承自

Authenticator.credentialBackedUp

credentialDeviceType

credentialDeviceType: string;

驗證器的裝置類型。

繼承自

Authenticator.credentialDeviceType

credentialID

credentialID: string;

Base64 編碼的憑證 ID。

繼承自

Authenticator.credentialID

credentialPublicKey

credentialPublicKey: string;

Base64 編碼的憑證公開金鑰。

繼承自

Authenticator.credentialPublicKey

providerAccountId

providerAccountId: string;

連線到驗證器的供應商帳戶 ID。

繼承自

Authenticator.providerAccountId

transports?

optional transports: null | string;

串連的傳輸標誌。

繼承自

Authenticator.transports

userId

userId: string;

驗證器的使用者 ID。

覆寫

Authenticator.userId


AdapterSession

會話會保存使用者目前登入狀態的相關資訊。

屬性

expires

expires: Date;

會話過期的絕對日期。

如果在過期日期之前存取會話,則會根據 SessionOptions.maxAge 中定義的 maxAge 選項延長會話。在 SessionOptions.updateAge 定義的時間段內,絕不會延長超過一次。

如果存取的會話已過期,則會從資料庫中刪除該會話,以清除不活動的會話。

sessionToken

sessionToken: string;

當使用 "database" AuthConfig.strategy 選項時,用於在資料庫中查閱會話的隨機產生值。此值會儲存在用戶端上的安全 HTTP-Only Cookie 中。

userId

userId: string;

將活動會話連接到資料庫中的使用者


AdapterUser

使用者代表可以登入應用程式的人。如果使用者尚不存在,則當他們第一次登入時,將會使用身分識別提供者傳回的資訊(個人資料資料)建立該使用者。也會建立對應的帳戶並連結到該使用者。

擴展

屬性

email

email: string;

使用者的電子郵件地址。

覆寫

User.email

emailVerified

emailVerified: null | Date;

使用者是否透過 電子郵件供應商驗證了他們的電子郵件地址。如果使用者尚未透過電子郵件供應商登入,則此值為 null,或者為第一次成功登入的日期。

id

id: string;

使用者的唯一識別碼。

覆寫

User.id

圖片?

optional image: null | string;
繼承自

User.image

名稱?

optional name: null | string;
繼承自

User.name


驗證令牌

驗證令牌是一種臨時令牌,用於通過使用者的電子郵件地址登入。當使用者使用電子郵件提供者登入時會建立此令牌。當使用者點擊電子郵件中的連結時,令牌和電子郵件會被送回伺服器,在那裡進行雜湊並與資料庫中的值進行比較。如果令牌和電子郵件匹配,並且令牌尚未過期,則使用者登入。然後從資料庫中刪除該令牌。

屬性

到期時間

expires: Date;

令牌到期的絕對日期。

識別碼

identifier: string;

使用者的電子郵件地址。

令牌

token: string;

使用 AuthConfig.secret 值進行雜湊的令牌。


AdapterAccountType

type AdapterAccountType: Extract<ProviderType, "oauth" | "oidc" | "email" | "webauthn">;

帳戶的類型。


isDate()

isDate(value): value is string

判斷給定的值是否可以解析為 Date

參數

参数类型
未知

回傳

值為字串

Auth.js © Balázs Orbán 及團隊 -2024