VanillaJS Auth SDK for GH Platform
npm install @gh-platform/auth-sdkSDK JavaScript hỗ trợ đăng nhập (login), làm mới token (refresh), kiểm tra token (introspect),
và tự động xác thực request HTTP trong kiến trúc multi‑tenant của GH Platform.
---
- ✅ Hỗ trợ login, refresh, introspect
- ✅ Tenant‑aware client: mọi API tự động gắn {tenant} vào URL
- ✅ Middleware AuthFetch tự động gắn Bearer token + auto refresh
- ✅ Hoạt động cả browser & Node.js
- ✅ Hỗ trợ nhúng trực tiếp (auth-sdk.min.js) hoặc cài qua npm
---
Mọi API của GH Platform Authenticate đều sử dụng dạng:
```
/api/v1/{tenant}/auth/login
/api/v1/{tenant}/auth/refresh
{service_url}/introspect
JavaScript SDK tự động truyền tenant trong mọi request.
`js
const tenant = "demo"; // hoặc trích xuất từ email: alice@example.com → example.com
const client = new AuthClient({
baseUrl: "https://auth.example.com",
tenant
});
const storage = new TokenStorage("auth", tenant);
const fetcher = new AuthFetch(client, storage);
`
`js
// Lưu token theo từng tenant
storage.accessToken = res.access_token;
storage.refreshToken = res.refresh_token;
// Key lưu trong localStorage sẽ giống:
// auth.demo.access_token
// auth.demo.refresh_token
`
---
`bash`
npm install @gh-platform/auth-sdk
Import:
`js`
import { AuthClient, AuthFetch, TokenStorage } from "@gh-platform/auth-sdk";
---
`html`
---
`js
const tenant = "example";
const client = new AuthClient({
baseUrl: "https://auth.example.com",
tenant
});
async function login() {
const res = await client.login(
"alice@example.com", // identifier
"User@123", // password
"gh-platform-admin" // client_id
);
storage.accessToken = res.access_token;
storage.refreshToken = res.refresh_token;
console.log("Login success!", res);
}
`
---
`js
const fetcher = new AuthFetch(client, storage);
const resp = await fetcher.fetch(
client.baseUrl + "/api/v1/" + tenant + "/auth/me"
);
const user = await resp.json();
console.log("User:", user);
`
Tự động:
- Gắn Authorization: Bearer
- Nếu token hết hạn → tự refresh → retry request
- Lưu token đúng tenant
---
`bash`
npm install vite terser -D
npm run build
npx terser dist/auth-sdk.umd.js -o dist/auth-sdk.min.js --compress --mangle
---
`html
``
---
Bản quyền © 2025 GH Platform – Phát hành theo giấy phép MIT