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

類型

此模組包含核心套件的公開類型和介面。

安裝

npm install @auth/core

然後,您可以從 @auth/core/types 匯入此子模組。

使用方式

即使您不使用 TypeScript,像 VS Code 這樣的 IDE 也會挑選類型,為您提供更好的開發人員體驗。當您輸入時,您會收到有關某些物件/函式外觀的建議,有時還會連結到文件、範例和其他有價值的資源。

一般來說,您不需要從此模組匯入類型。大多在使用 Auth 函式和選擇性的 AuthConfig 介面時,其中的所有內容都已經過類型化。

💡

Auth 函式內部,您不需要使用此模組中的任何單一類型。

範例

import { Auth } from "@auth/core"
 
const request = new Request("https://example.com")
const response = await Auth(request, {
  callbacks: {
    jwt(): JWT { // <-- This is unnecessary!
      return { foo: "bar" }
    },
    session(
       { session, token }: { session: Session; token: JWT } // <-- This is unnecessary!
    ) {
      return session
    },
  }
})

資源

AuthConfig

重新匯出 AuthConfig

帳戶

通常包含有關正在使用的提供者的資訊,並且還會擴充 TokenSet,這是 OAuth 提供者傳回的不同權杖。

繼承

屬性

access_token?

optional readonly access_token: string;
繼承自

Partial.access_token

authorization_details?

optional readonly authorization_details: AuthorizationDetails[];
繼承自

Partial.authorization_details

expires_at?

optional expires_at: number;

根據 TokenEndpointResponse.expires_in 計算出的值。

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

此值可用於與 TokenEndpointResponse.refresh_token 一起實作權杖輪替。

參見

expires_in?

optional readonly expires_in: number;
繼承自

Partial.expires_in

id_token?

optional readonly id_token: string;
繼承自

Partial.id_token

provider

provider: string;

此帳戶的提供者 ID。例如,「google」。請參閱 https://authjs.dev.org.tw/reference/core/providers 上的完整清單

providerAccountId

providerAccountId: string;

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

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

refresh_token?

optional readonly refresh_token: string;
繼承自

Partial.refresh_token

scope?

optional readonly scope: string;
繼承自

Partial.scope

token_type?

optional readonly token_type: Lowercase<string>;

注意:因為此值不區分大小寫,所以一律以小寫字母傳回

繼承自

Partial.token_type

type

type: ProviderType;

此帳戶的供應商類型

userId?

optional userId: string;

此帳戶所屬的使用者 ID

請參閱

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


Authenticator

WebAuthn 驗證器。表示能夠驗證其所參考帳戶的實體,並包含驗證器的憑證和相關資訊。

請參閱

https://www.w3.org/TR/webauthn/#authenticator

由以下擴充

屬性

counter

counter: number;

驗證器已被使用的次數。

credentialBackedUp

credentialBackedUp: boolean;

用戶端驗證器是否備份了憑證。

credentialDeviceType

credentialDeviceType: string;

驗證器的裝置類型。

credentialID

credentialID: string;

Base64 編碼的憑證 ID。

credentialPublicKey

credentialPublicKey: string;

Base64 編碼的憑證公開金鑰。

providerAccountId

providerAccountId: string;

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

transports?

optional transports: null | string;

串連的傳輸旗標。

userId?

optional userId: string;

此驗證器所屬的使用者 ID。


CookieOption

文件

屬性

name

name: string;

options

options: SerializeOptions;

CookiesOptions

文件

屬性

callbackUrl

callbackUrl: Partial<CookieOption>;

csrfToken

csrfToken: Partial<CookieOption>;

nonce

nonce: Partial<CookieOption>;

pkceCodeVerifier

pkceCodeVerifier: Partial<CookieOption>;

sessionToken

sessionToken: Partial<CookieOption>;

state

state: Partial<CookieOption>;

webauthnChallenge

webauthnChallenge: Partial<CookieOption>;

DefaultSession

由以下擴充

屬性

expires

expires: string;

user?

optional user: User;

LoggerInstance

覆寫任何方法,其餘方法將使用預設的記錄器。

文件

擴充

屬性

debug()

debug: (message, metadata?) => void;
參數
參數類型
messagestring
metadata?unknown
傳回

void

error()

error: (error) => void;
參數
參數類型
errorError
傳回

void

warn()

warn: (code) => void;
參數
參數類型
codeWarningCode
傳回

void


PagesOptions

屬性

error

error: string;

錯誤頁面的路徑。

可選的「error」查詢參數會設定為可用的值之一。

預設
"/error"

newUser

newUser: string;

如果設定,新使用者首次登入時將會被導向到此處

signIn

signIn: string;

登入頁面的路徑。

可選的「error」查詢參數會設定為可用的值之一。

預設
"/signin"

signOut

signOut: string;

verifyRequest

verifyRequest: string;

Profile

從您的 OAuth 供應商傳回的使用者資訊。

請參閱

https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims

可索引

[claim: string]: unknown

屬性

address?

optional address: null | {
  country: null | string;
  formatted: null | string;
  locality: null | string;
  postal_code: null | string;
  region: null | string;
  street_address: null | string;
};

birthdate?

optional birthdate: null | string;

email?

optional email: null | string;

email_verified?

optional email_verified: null | boolean;

family_name?

optional family_name: null | string;

gender?

optional gender: null | string;

given_name?

optional given_name: null | string;

id?

optional id: null | string;

locale?

optional locale: null | string;

middle_name?

optional middle_name: null | string;

name?

optional name: null | string;

nickname?

optional nickname: null | string;

phone_number?

optional phone_number: null | string;

picture?

optional picture: any;

preferred_username?

optional preferred_username: null | string;

profile?

optional profile: null | string;

sub?

optional sub: null | string;

updated_at?

optional updated_at: null | string | number | Date;

website?

optional website: null | string;

zoneinfo?

optional zoneinfo: null | string;

PublicProvider

屬性

callbackUrl

callbackUrl: string;

id

id: string;

name

name: string;

signinUrl

signinUrl: string;

type

type: string;

ResponseInternal<Body>

類型參數

類型參數
Body 繼承自 string | Record<string, any> | any[] | nullany

屬性

body?

optional body: Body;

cookies?

optional cookies: Cookie[];

headers?

optional headers: HeadersInit;

redirect?

optional redirect: string;

status?

optional status: number;

Session

已登入使用者的活動工作階段。

繼承自

屬性

expires

expires: string;
繼承自

DefaultSession.expires

user?

optional user: User;
繼承自

DefaultSession.user


主題

變更內建頁面的主題。

文件 | 頁面

屬性

brandColor?

optional brandColor: string;

buttonText?

optional buttonText: string;

colorScheme?

optional colorScheme: "auto" | "dark" | "light";
optional logo: string;

使用者

在 OAuth 提供者的 profile 回呼中返回的物件形狀,可用於 jwtsession 回呼中,或在使用資料庫時,可用於 session 回呼的第二個參數。

擴充自

屬性

email?

optional email: null | string;

id?

optional id: string;

image?

optional image: null | string;

name?

optional name: null | string;

AuthAction

type AuthAction: 
  | "callback"
  | "csrf"
  | "error"
  | "providers"
  | "session"
  | "signin"
  | "signout"
  | "verify-request"
  | "webauthn-options";

Auth.js 支援的動作。每個動作都對應到一個 REST API 端點。有些動作有 GETPOST 變體,取決於該動作是否會變更伺服器的狀態。

  • "callback":
  • "csrf":傳回原始 CSRF 權杖,該權杖儲存在 Cookie 中(已加密)。它用於 CSRF 保護,實作 雙重提交 Cookie 技術。

某些框架具有內建的 CSRF 保護,因此可以停用此動作。在這種情況下,對應的端點將傳回 404 回應。在 skipCSRFCheck 中閱讀更多資訊。⚠ 我們不建議手動停用 CSRF 保護,除非您知道自己在做什麼。

  • "error":轉譯內建的錯誤頁面。
  • "providers":傳回所有已設定提供者的用戶端安全清單。
  • "session":
    • GET:如果使用者工作階段存在,則傳回使用者工作階段,否則傳回 null
    • POST:更新使用者工作階段並傳回更新後的工作階段。
  • "signin":
    • GET:轉譯內建的登入頁面。
    • POST:啟動登入流程。
  • "signout":
    • GET:轉譯內建的登出頁面。
    • POST:啟動登出流程。這將使使用者工作階段失效(刪除 Cookie,如果資料庫中有工作階段,也會一併刪除)。
  • "verify-request":轉譯內建的驗證請求頁面。
  • "webauthn-options":
    • GET:傳回 WebAuthn 驗證和註冊流程的選項。

Awaitable<T>

type Awaitable<T>: T | PromiseLike<T>;

型別參數

類型參數
T

Awaited<T>

type Awaited<T>: T extends Promise<infer U> ? U : T;

型別參數

類型參數
T

ErrorPageParam

type ErrorPageParam: "Configuration" | "AccessDenied" | "Verification";

TODO:檢查是否全部使用/正確


SemverString

type SemverString: v${number} | v${number}.${number} | v${number}.${number}.${number};

SignInPageErrorParam

type SignInPageErrorParam: 
  | "Signin"
  | "OAuthSignin"
  | "OAuthCallbackError"
  | "OAuthCreateAccount"
  | "EmailCreateAccount"
  | "Callback"
  | "OAuthAccountNotLinked"
  | "EmailSignin"
  | "CredentialsSignin"
  | "SessionRequired";

TODO:檢查是否全部使用/正確


TokenSet

type TokenSet: Partial<TokenEndpointResponse> & {
  expires_at: number;
};

OAuth 提供者傳回的不同權杖。其中一些權杖以不同的大小寫可用,但它們指的是相同的值。

型別宣告

expires_at?

optional expires_at: number;

access_token 的到期日期(以秒為單位)。此值是從 expires_in 值計算得出的。

參見

https://www.ietf.org/rfc/rfc6749.html#section-4.2.2


WebAuthnOptionsResponseBody

type WebAuthnOptionsResponseBody: {
  action: WebAuthnAuthenticate;
  options: PublicKeyCredentialRequestOptionsJSON;
  } | {
  action: WebAuthnRegister;
  options: PublicKeyCredentialCreationOptionsJSON;
};
Auth.js © Balázs Orbán 和團隊 -2024