跳至主要內容

透過 Account API 管理帳戶設定

什麼是 Logto Account API

Logto Account API 是一套完整的 API,讓終端使用者可以直接透過 API 存取帳戶,而無需經過 Management API,重點如下:

  • 直接存取:Account API 讓終端使用者可以直接存取並管理自己的帳戶資料,無需透過 Management API 中繼。
  • 使用者資料與身分管理:使用者可完整管理個人資料與安全設定,包括更新電子郵件、手機、密碼等身分資訊,以及管理社交連結。MFA 與 SSO 支援即將推出。
  • 全域存取控制:管理員可對存取設定進行完整、全域的控制,並可自訂每個欄位。
  • 無縫授權 (Seamless authorization):授權變得前所未有地簡單!只需使用 client.getAccessToken() 取得 Logto 的不透明權杖 (Opaque token),並以 Bearer <access_token> 附加於 Authorization 標頭即可。

透過 Logto Account API,你可以打造與 Logto 完整整合的自訂帳戶管理系統,例如個人資料頁面。

常見的使用情境如下:

  • 取得使用者資料
  • 更新使用者資料
  • 更新使用者密碼
  • 更新使用者身分資訊(包含電子郵件、手機、社交連結)

想瞭解更多可用 API,請參閱 Logto Account API ReferenceLogto Verification API Reference

備註:

以下設定專用的 Account API 即將推出:MFA、多重要素驗證 (MFA)、單一登入 (SSO)、自訂資料(使用者)、帳戶刪除。在此之前,你可以透過 Logto Management API 實作這些功能。詳情請參閱 透過 Management API 管理帳戶設定

如何啟用 Account API

預設情況下,Account API 是關閉的。若要啟用,需透過 Management API 更新全域設定。

API 端點 /api/account-center 可用於取得與更新帳戶中心設定,你可以用它來啟用 / 關閉 Account API,並自訂欄位。

範例請求:

curl -X PATCH https://[tenant-id].logto.app/api/account-center \
-H 'authorization: Bearer <access_token for Logto Management API>' \
-H 'content-type: application/json' \
--data-raw '{"enabled":true,"fields":{"username":"Edit"}}'

enabled 欄位用於啟用或關閉 Account API,fields 欄位用於自訂欄位,值可為 OffEditReadOnly,預設為 Off。欄位清單如下:

  • name:姓名欄位。
  • avatar:頭像欄位。
  • profile:個人資料欄位,包含其子欄位。
  • username:使用者名稱欄位。
  • email:電子郵件欄位。
  • phone:手機欄位。
  • password:密碼欄位,查詢時若使用者已設密碼則回傳 true,否則為 false
  • social:社交連結。

更多 API 細節請參閱 Logto Management API Reference

如何存取 Account API

取得存取權杖 (Access token)

在應用程式中設定好 SDK 後,可使用 client.getAccessToken() 方法取得存取權杖。此權杖為不透明權杖 (Opaque token),可用於存取 Account API。

若未使用官方 SDK,則在向 /oidc/token 請求權杖時,resource 應設為空。

使用存取權杖存取 Account API

與 Account API 互動時,請將存取權杖放在 HTTP 標頭的 Authorization 欄位,格式為 Bearer (Bearer YOUR_TOKEN)。

以下為取得使用者帳戶資訊的範例:

curl https://[tenant-id].logto.app/api/my-account \
-H 'authorization: Bearer <access_token>'

管理基本帳戶資訊

取得使用者帳戶資訊

curl https://[tenant-id].logto.app/api/my-account \
-H 'authorization: Bearer <access_token>'

回應內容範例如下:

{
"id": "...",
"username": "...",
"name": "...",
"avatar": "..."
}

回應欄位會依帳戶中心設定而有所不同。

更新基本帳戶資訊

基本帳戶資訊包含使用者名稱、姓名、頭像與個人資料。

若要更新使用者名稱、姓名、頭像,可使用 PATCH /api/my-account 端點。

curl -X PATCH https://[tenant-id].logto.app/api/my-account \
-H 'authorization: Bearer <access_token>' \
-H 'content-type: application/json' \
--data-raw '{"username":"...","name":"...","avatar":"..."}'

若要更新個人資料,可使用 PATCH /api/my-account/profile 端點。

curl -X PATCH https://[tenant-id].logto.app/api/my-account/profile \
-H 'authorization: Bearer <access_token>' \
-H 'content-type: application/json' \
--data-raw '{"familyName":"...","givenName":"..."}'

管理識別資訊與其他敏感資料

出於安全考量,Account API 在涉及識別資訊與其他敏感操作時,需額外授權。

取得驗證記錄 ID

首先需取得驗證記錄 ID,該 ID 可用於更新識別資訊時驗證使用者身分。

取得驗證記錄 ID 的方式:驗證使用者密碼,或向使用者電子郵件 / 手機發送驗證碼。

驗證使用者密碼

curl -X POST https://[tenant-id].logto.app/api/verifications/password \
-H 'authorization: Bearer <access_token>' \
-H 'content-type: application/json' \
--data-raw '{"password":"..."}'

回應內容範例如下:

{
"verificationRecordId": "...",
"expiresAt": "..."
}

透過發送驗證碼至電子郵件或手機驗證

備註:

使用此方法前,需先設定電子郵件連接器設定簡訊連接器,並確保已設定 UserPermissionValidation 範本。

以電子郵件為例,請求新驗證碼並取得驗證記錄 ID:

curl -X POST https://[tenant-id].logto.app/api/verifications/verification-code \
-H 'authorization: Bearer <access_token>' \
-H 'content-type: application/json' \
--data-raw '{"identifier":{"type":"email","value":"..."}}'

回應內容範例如下:

{
"verificationRecordId": "...",
"expiresAt": "..."
}

收到驗證碼後,可用於更新驗證記錄的驗證狀態。

curl -X POST https://[tenant-id].logto.app/api/verifications/verification-code/verify \
-H 'authorization: Bearer <access_token>' \
-H 'content-type: application/json' \
--data-raw '{"identifier":{"type":"email","value":"..."},"verificationId":"...","code":"123456"}'

驗證成功後,即可使用驗證記錄 ID 更新使用者識別資訊。

夾帶驗證記錄 ID 發送請求

在發送更新使用者識別資訊的請求時,需將驗證記錄 ID 夾帶於請求標頭的 logto-verification-id 欄位。

備註:

使用此方法前,需先設定電子郵件連接器,並確保已設定 BindNewIdentifier 範本。

更新或綁定新電子郵件前,需先驗證該電子郵件的擁有權。

呼叫 POST /api/verifications/verification-code 端點請求驗證碼。

curl -X POST https://[tenant-id].logto.app/api/verifications/verification-code \
-H 'authorization: Bearer <access_token>' \
-H 'content-type: application/json' \
--data-raw '{"identifier":{"type":"email","value":"..."}}'

回應中會有 verificationId,並會收到驗證碼,請用於驗證電子郵件。

curl -X POST https://[tenant-id].logto.app/api/verifications/verification-code/verify \
-H 'authorization: Bearer <access_token>' \
-H 'content-type: application/json' \
--data-raw '{"identifier":{"type":"email","value":"..."},"verificationId":"...","code":"..."}'

驗證成功後,即可更新使用者電子郵件,將 verificationId 設為請求內容的 newIdentifierVerificationRecordId

curl -X PATCH https://[tenant-id].logto.app/api/my-account/primary-email \
-H 'authorization: Bearer <access_token>' \
-H 'logto-verification-id: <verification_record_id>' \
-H 'content-type: application/json' \
--data-raw '{"email":"...","newIdentifierVerificationRecordId":"..."}'

移除使用者電子郵件

移除使用者電子郵件可使用 DELETE /api/my-account/primary-email 端點。

curl -X DELETE https://[tenant-id].logto.app/api/my-account/primary-email \
-H 'authorization: Bearer <access_token>' \
-H 'logto-verification-id: <verification_record_id>'

管理手機

備註:

使用此方法前,需先設定簡訊連接器,並確保已設定 BindNewIdentifier 範本。

與更新電子郵件類似,可使用 PATCH /api/my-account/primary-phone 端點更新或綁定新手機,並可用 DELETE /api/my-account/primary-phone 端點移除使用者手機。

綁定新社交連結時,需先請求授權網址:

curl -X POST https://[tenant-id].logto.app/api/verifications/social \
-H 'authorization: Bearer <access_token>' \
-H 'content-type: application/json' \
--data-raw '{"connectorId":"...","redirectUri":"...","state":"..."}'
  • connectorId:對應 社交連接器 的 ID。
  • redirectUri:使用者授權應用程式後的導向網址,你需在此網址架設網頁並接收回呼。
  • state:授權後回傳的狀態,用於防止 CSRF 攻擊的隨機字串。

回應中會有 verificationRecordId,請妥善保存。

使用者授權應用程式後,你會在 redirectUri 收到帶有 state 參數的回呼。接著可用 POST /api/verifications/social/verify 端點驗證社交連結。

curl -X POST https://[tenant-id].logto.app/api/verifications/social/verify \
-H 'authorization: Bearer <access_token>' \
-H 'content-type: application/json' \
--data-raw '{"connectorData":"...","verificationRecordId":"..."}'

connectorData 為社交連接器授權後回傳的資料,你需在回呼頁面解析 redirectUri 的查詢參數,並以 JSON 格式包裝作為 connectorData 欄位值。

最後,可用 POST /api/my-account/identities 端點綁定社交連結。

curl -X POST https://[tenant-id].logto.app/api/my-account/identities \
-H 'authorization: Bearer <access_token>' \
-H 'logto-verification-id: <verification_record_id>' \
-H 'content-type: application/json' \
--data-raw '{"newIdentifierVerificationRecordId":"..."}'

移除社交連結

移除社交連結可使用 DELETE /api/my-account/identities 端點。

curl -X DELETE https://[tenant-id].logto.app/api/my-account/identities/[connector_target_id] \
-H 'authorization: Bearer <access_token>' \
-H 'logto-verification-id: <verification_record_id>'