取得組織內的使用者資訊
適用場景
這通常用於使用者個人資料頁面,需要顯示其所屬組織資訊時。
如何實作
有兩種方式可以取得組織內的使用者資訊。
解碼 ID 權杖 (ID token)
ID 權杖 (ID token) 是一個標準的 JWT,內含使用者個人資料資訊與組織相關宣告 (claims)。呼叫 SDK 方法 decodeIdToken() 可取得如下的 JSON 物件:
{
"sub": "aauqbb63vg4s",
"name": "John Doe",
"picture": "https://example.com/johndoe.png",
"email": "johndoe@example.com",
// ...
"organizations": [
"organization-id-1",
"organization-id-2",
"organization-id-3"
// ...
],
"organization_roles": [
"organization-id-1:admin",
"organization-id-2:member",
"organization-id-3:viewer"
// ...
],
"aud": "admin-console"
// ...
}
但請注意,ID 權杖 (ID token) 僅於驗證 (Authentication) 時簽發,若使用者個人資料後續有變更,權杖內容可能會過時。
若需取得最新資訊,請參考下方第二種方式,或呼叫 clearAllTokens() 並重新啟動驗證流程以取得新的 ID 權杖 (ID token)。
await logtoClient.clearAllTokens();
logtoClient.signIn({
redirectUri: 'https://your-app.com/callback',
prompt: 'consent',
});
如果會話仍有效,signIn 呼叫會直接導回你的應用程式,無需再次輸入憑證。對使用者來說,應用程式僅會刷新畫面,並於背景自動取得新的 ID 權杖 (ID token)。
從 /oidc/me 端點取得使用者資訊
你也可以請求 /oidc/me,以組織情境下即時取得使用者資訊。請呼叫 SDK 方法 fetchUserInfo()。