gov-sso-login
v1.1.0
Published
IAM-GOV SSO Login library for React/Next.js applications
Maintainers
Readme
gov-sso-login
IAM-GOV SSO Login library สำหรับ React/Next.js applications
Installation
npm install gov-sso-loginQuick Start
1. Config
import type { GovSsoConfig } from 'gov-sso-login';
const ssoConfig: GovSsoConfig = {
iamAuthUrl: 'https://auth.govcenter.co',
serviceCode: 'my-service-code',
apiBaseUrl: 'https://api.example.com',
callbackUrl: 'https://example.com/auth/callback',
};2. Login Button
import { SsoLoginButton } from 'gov-sso-login';
export default function LoginPage() {
return (
<SsoLoginButton
config={ssoConfig}
label="เข้าสู่ระบบด้วย Gov Center"
/>
);
}3. Callback Handler
import { SsoCallbackHandler } from 'gov-sso-login';
export default function CallbackPage() {
return (
<SsoCallbackHandler
config={ssoConfig}
onSuccess={(result) => {
console.log('Login success:', result.user);
window.location.href = '/dashboard';
}}
onError={(err) => {
console.error('Login failed:', err.message);
window.location.href = '/login';
}}
/>
);
}4. Using Hooks (Custom UI)
import { useGovSso, useGovSsoCallback } from 'gov-sso-login';
// Login hook
function LoginPage() {
const { login, getLoginUrl } = useGovSso(ssoConfig);
return <button onClick={login}>Login</button>;
}
// Callback hook
function CallbackPage() {
const { status, user, error, message } = useGovSsoCallback(ssoConfig, {
onSuccess: (result) => { /* ... */ },
onError: (err) => { /* ... */ },
});
if (status === 'loading') return <p>Loading...</p>;
if (status === 'error') return <p>Error: {error?.message}</p>;
return <p>Welcome, {user?.firstName}!</p>;
}5. Core Client (Non-React)
import { GovSsoClient } from 'gov-sso-login';
const client = new GovSsoClient(ssoConfig);
// Get login URL
const url = client.getLoginUrl();
// Handle callback
const result = await client.handleCallback(code);API Reference
GovSsoConfig
| Property | Type | Required | Description |
|---|---|---|---|
| iamAuthUrl | string | ✅ | IAM-GOV auth frontend URL |
| serviceCode | string | ✅ | Service code registered with IAM-GOV |
| apiBaseUrl | string | ✅ | Backend API URL |
| callbackUrl | string | ✅ | Frontend callback URL |
| ssoCallbackPath | string | ❌ | Backend callback path (default: /auth/sso-callback) |
Exports
GovSsoClient— Core client classuseGovSso(config)— Hook for login initiationuseGovSsoCallback(config, options?)— Hook for callback handlingSsoLoginButton— Ready-made login button componentSsoCallbackHandler— Ready-made callback page component
Backend Requirements
Your backend needs a POST /auth/sso-callback endpoint that:
- Receives
{ code }from the frontend - Sends the code to IAM-GOV
/e-services/sso/verify-codewith API key - Returns
{ success: true, user: { ... } }and sets JWT cookies
License
MIT
