指南

用戶資料

Profile 片段(可選)

啟用 Profile 後,可使用 access token 讀寫終端用戶自訂資料。以下片段假設登入後 token 存於 sessionStorage.hs_access_token

前置

  • Auth 已啟用且使用者已完成 OAuth 登入
  • Profile 已啟用
  • 使用 Bearer token + X-Client-Id(公開 API,不含 secret)

profile-snippet.html

<button type="button" id="load-profile">讀取 Profile</button>
<button type="button" id="save-profile">寫入示範資料</button>
<pre id="profile-out"></pre>
<script>
(async function () {
  const API_BASE = "{{api_base}}";
  const CLIENT_ID = "{{client_id}}";
  const out = document.getElementById("profile-out");

  function token() {
    return sessionStorage.getItem("hs_access_token") || "";
  }

  function userIdFromJwt(jwt) {
    try {
      const payload = JSON.parse(atob(jwt.split(".")[1].replace(/-/g, "+").replace(/_/g, "/")));
      return payload.sub;
    } catch {
      return "";
    }
  }

  async function api(path, init) {
    const res = await fetch(API_BASE + path, {
      ...init,
      headers: {
        Authorization: "Bearer " + token(),
        "X-Client-Id": CLIENT_ID,
        ...(init?.headers || {}),
      },
    });
    const data = await res.json().catch(() => ({}));
    return { ok: res.ok, status: res.status, data };
  }

  document.getElementById("load-profile").addEventListener("click", async () => {
    const sub = userIdFromJwt(token());
    if (!sub) { out.textContent = "請先登入"; return; }
    const r = await api("/v1/profile/" + sub);
    out.textContent = JSON.stringify(r, null, 2);
  });

  document.getElementById("save-profile").addEventListener("click", async () => {
    const sub = userIdFromJwt(token());
    if (!sub) { out.textContent = "請先登入"; return; }
    const r = await api("/v1/profile/" + sub, {
      method: "PUT",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ data: { display_name: "示範用戶", updated_at: new Date().toISOString() } }),
    });
    out.textContent = JSON.stringify(r, null, 2);
  });
})();
</script>

目前為模擬模式,回應為範例資料

登入 Dev 以真實測試

Try-it

模擬模式:不會發送真實請求

Request

GET https://api.hsystem.halphastech.com/v1/profile/usr_demo123
Authorization: Bearer eyJhbG…
X-Client-Id: hs_demo_client

Response

(尚未發送)