@biza-gov/sso-login 0.0.0-stage → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +632 -2
- package/dist/index.d.mts +384 -0
- package/dist/index.d.ts +384 -0
- package/dist/index.js +670 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +636 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +43 -4
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import React$1 from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* gov-sso-login - IAM-GOV SSO Configuration & Types
|
|
6
|
+
*/
|
|
7
|
+
/** Configuration for the GOV SSO client */
|
|
8
|
+
interface GovSsoConfig {
|
|
9
|
+
/** URL ของ IAM-GOV auth frontend เช่น https://auth.govcenter.co */
|
|
10
|
+
iamAuthUrl: string;
|
|
11
|
+
/** Service code ที่ลงทะเบียนกับ IAM-GOV */
|
|
12
|
+
serviceCode: string;
|
|
13
|
+
/** URL ของ Backend API เช่น https://api.example.com */
|
|
14
|
+
apiBaseUrl: string;
|
|
15
|
+
/** Path ของ backend SSO callback endpoint (default: '/auth/sso-callback') */
|
|
16
|
+
ssoCallbackPath?: string;
|
|
17
|
+
/** URL ที่จะ redirect กลับหลัง IAM-GOV login สำเร็จ (ฝั่ง frontend) */
|
|
18
|
+
callbackUrl: string;
|
|
19
|
+
/** Path ของ backend session check endpoint (default: '/auth/me') */
|
|
20
|
+
checkSessionPath?: string;
|
|
21
|
+
}
|
|
22
|
+
/** User data returned from SSO login */
|
|
23
|
+
interface SsoUser {
|
|
24
|
+
id: string;
|
|
25
|
+
firstName?: string;
|
|
26
|
+
lastName?: string;
|
|
27
|
+
role?: string;
|
|
28
|
+
phone?: string;
|
|
29
|
+
pictureUrl?: string;
|
|
30
|
+
[key: string]: any;
|
|
31
|
+
}
|
|
32
|
+
/** Result of SSO callback verification */
|
|
33
|
+
interface SsoCallbackResult {
|
|
34
|
+
success: boolean;
|
|
35
|
+
user?: SsoUser;
|
|
36
|
+
message?: string;
|
|
37
|
+
/** SSO payload from IAM-GOV (memberships, municipality, etc.) */
|
|
38
|
+
ssoPayload?: Record<string, any>;
|
|
39
|
+
/** Allow additional fields from backend response */
|
|
40
|
+
[key: string]: any;
|
|
41
|
+
}
|
|
42
|
+
/** Status of the SSO callback process */
|
|
43
|
+
type SsoCallbackStatus = 'idle' | 'loading' | 'success' | 'error';
|
|
44
|
+
/** Status of the auto-login process */
|
|
45
|
+
type AutoLoginStatus = 'checking' | 'authenticated' | 'redirecting' | 'unauthenticated';
|
|
46
|
+
/** Options for the auto-login hook */
|
|
47
|
+
interface AutoLoginOptions {
|
|
48
|
+
/** Auto-redirect ไป SSO ถ้าไม่มี local session (default: true) */
|
|
49
|
+
autoRedirect?: boolean;
|
|
50
|
+
/** Called เมื่อ user มี session อยู่แล้ว */
|
|
51
|
+
onAuthenticated?: (user: SsoUser, data?: Record<string, any>) => void;
|
|
52
|
+
/** Called เมื่อ user ไม่มี session และ autoRedirect เป็น false */
|
|
53
|
+
onUnauthenticated?: () => void;
|
|
54
|
+
}
|
|
55
|
+
/** Color theme for SSO components */
|
|
56
|
+
interface SsoTheme {
|
|
57
|
+
/** Primary color (default: '#1E7D55') */
|
|
58
|
+
primaryColor?: string;
|
|
59
|
+
/** Primary hover color (default: '#256B48') */
|
|
60
|
+
primaryHoverColor?: string;
|
|
61
|
+
/** Error color (default: '#ef4444') */
|
|
62
|
+
errorColor?: string;
|
|
63
|
+
/** Text color (default: '#1e293b') */
|
|
64
|
+
textColor?: string;
|
|
65
|
+
/** Secondary text color (default: '#64748b') */
|
|
66
|
+
secondaryTextColor?: string;
|
|
67
|
+
/** Background color (default: '#f8fafc') */
|
|
68
|
+
backgroundColor?: string;
|
|
69
|
+
/** Card background color (default: '#ffffff') */
|
|
70
|
+
cardBackgroundColor?: string;
|
|
71
|
+
/** Border radius (default: '0.5rem') */
|
|
72
|
+
borderRadius?: string;
|
|
73
|
+
}
|
|
74
|
+
/** Default theme values */
|
|
75
|
+
declare const DEFAULT_THEME: Required<SsoTheme>;
|
|
76
|
+
/** Options for the SSO login button component */
|
|
77
|
+
interface SsoLoginButtonProps {
|
|
78
|
+
/** SSO config (optional if wrapped in GovSsoProvider) */
|
|
79
|
+
config?: GovSsoConfig;
|
|
80
|
+
/** Custom label text (default: 'เข้าสู่ระบบด้วย Gov Center') */
|
|
81
|
+
label?: string;
|
|
82
|
+
/** Custom CSS class name */
|
|
83
|
+
className?: string;
|
|
84
|
+
/** Custom inline styles */
|
|
85
|
+
style?: React.CSSProperties;
|
|
86
|
+
/** Custom icon element */
|
|
87
|
+
icon?: React.ReactNode;
|
|
88
|
+
/** Callback before redirect */
|
|
89
|
+
onBeforeLogin?: () => void;
|
|
90
|
+
/** Custom theme colors */
|
|
91
|
+
theme?: SsoTheme;
|
|
92
|
+
}
|
|
93
|
+
/** Options for the SSO callback handler component */
|
|
94
|
+
interface SsoCallbackHandlerProps {
|
|
95
|
+
/** SSO config (optional if wrapped in GovSsoProvider) */
|
|
96
|
+
config?: GovSsoConfig;
|
|
97
|
+
/** Called when login succeeds */
|
|
98
|
+
onSuccess?: (result: SsoCallbackResult) => void;
|
|
99
|
+
/** Called when login fails */
|
|
100
|
+
onError?: (error: Error) => void;
|
|
101
|
+
/** Custom loading message */
|
|
102
|
+
loadingMessage?: string;
|
|
103
|
+
/** Custom success message */
|
|
104
|
+
successMessage?: string;
|
|
105
|
+
/** Custom error message */
|
|
106
|
+
errorMessage?: string;
|
|
107
|
+
/** Custom theme colors */
|
|
108
|
+
theme?: SsoTheme;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Custom error class for SSO-related errors */
|
|
112
|
+
declare class GovSsoError extends Error {
|
|
113
|
+
code: string;
|
|
114
|
+
statusCode?: number;
|
|
115
|
+
constructor(message: string, code: string, statusCode?: number);
|
|
116
|
+
}
|
|
117
|
+
declare class GovSsoClient {
|
|
118
|
+
private config;
|
|
119
|
+
constructor(config: GovSsoConfig);
|
|
120
|
+
/**
|
|
121
|
+
* สร้าง random state string สำหรับป้องกัน CSRF
|
|
122
|
+
*/
|
|
123
|
+
private generateState;
|
|
124
|
+
/**
|
|
125
|
+
* บันทึก state ลง sessionStorage
|
|
126
|
+
*/
|
|
127
|
+
private saveState;
|
|
128
|
+
/**
|
|
129
|
+
* ตรวจสอบ state ว่าตรงกับที่บันทึกไว้
|
|
130
|
+
* @returns true ถ้า state ตรง / false ถ้าไม่ตรงหรือไม่พบ
|
|
131
|
+
*/
|
|
132
|
+
verifyState(state: string | null): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* สร้าง URL สำหรับ redirect ไปหน้า IAM-GOV login
|
|
135
|
+
* พร้อม state parameter สำหรับป้องกัน CSRF
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* const client = new GovSsoClient(config);
|
|
140
|
+
* const url = client.getLoginUrl();
|
|
141
|
+
* // https://auth.govcenter.co/auth/login?service=xxx&callback=yyy&state=zzz
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
getLoginUrl(): string;
|
|
145
|
+
/**
|
|
146
|
+
* Redirect ไปหน้า IAM-GOV login ทันที
|
|
147
|
+
*/
|
|
148
|
+
redirectToLogin(): void;
|
|
149
|
+
/**
|
|
150
|
+
* เช็คว่า user มี session อยู่แล้วหรือไม่ (เช็ค JWT cookie กับ backend)
|
|
151
|
+
* ใช้สำหรับตรวจสอบก่อนทำ SSO flow ใหม่
|
|
152
|
+
*
|
|
153
|
+
* @returns { authenticated: true, user } ถ้ามี session / { authenticated: false } ถ้าไม่มี
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* const session = await client.checkSession();
|
|
158
|
+
* if (session.authenticated) {
|
|
159
|
+
* console.log('Already logged in:', session.user);
|
|
160
|
+
* } else {
|
|
161
|
+
* client.redirectToLogin();
|
|
162
|
+
* }
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
checkSession(): Promise<{
|
|
166
|
+
authenticated: boolean;
|
|
167
|
+
user?: SsoUser;
|
|
168
|
+
[key: string]: any;
|
|
169
|
+
}>;
|
|
170
|
+
/**
|
|
171
|
+
* Delay helper สำหรับ retry
|
|
172
|
+
*/
|
|
173
|
+
private delay;
|
|
174
|
+
/**
|
|
175
|
+
* ตรวจสอบว่าควร retry หรือไม่ (500, 502, 503, 504)
|
|
176
|
+
*/
|
|
177
|
+
private isRetryableStatus;
|
|
178
|
+
/**
|
|
179
|
+
* ส่ง authorization code ไปยัง backend เพื่อ verify กับ IAM-GOV
|
|
180
|
+
* มี auto-retry 2 ครั้งถ้าเจอ server error (500/503)
|
|
181
|
+
*
|
|
182
|
+
* @param code - Authorization code จาก IAM-GOV callback
|
|
183
|
+
* @returns SsoCallbackResult พร้อมข้อมูล user
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```ts
|
|
187
|
+
* const result = await client.handleCallback(code);
|
|
188
|
+
* if (result.success) {
|
|
189
|
+
* console.log('Logged in as:', result.user);
|
|
190
|
+
* }
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
handleCallback(code: string): Promise<SsoCallbackResult>;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
interface GovSsoProviderProps {
|
|
197
|
+
config: GovSsoConfig;
|
|
198
|
+
/** Default theme สำหรับทุก component ใน Provider */
|
|
199
|
+
theme?: SsoTheme;
|
|
200
|
+
children: React$1.ReactNode;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Provider สำหรับ share config ไปทั้ง app
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```tsx
|
|
207
|
+
* import { GovSsoProvider } from '@biza-gov/sso-login';
|
|
208
|
+
*
|
|
209
|
+
* const ssoConfig = {
|
|
210
|
+
* iamAuthUrl: 'https://auth.govcenter.co',
|
|
211
|
+
* serviceCode: 'my-service',
|
|
212
|
+
* apiBaseUrl: 'https://api.example.com',
|
|
213
|
+
* callbackUrl: 'https://example.com/auth/callback',
|
|
214
|
+
* };
|
|
215
|
+
*
|
|
216
|
+
* export default function RootLayout({ children }) {
|
|
217
|
+
* return (
|
|
218
|
+
* <GovSsoProvider config={ssoConfig}>
|
|
219
|
+
* {children}
|
|
220
|
+
* </GovSsoProvider>
|
|
221
|
+
* );
|
|
222
|
+
* }
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
declare function GovSsoProvider({ config, theme, children }: GovSsoProviderProps): react_jsx_runtime.JSX.Element;
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* gov-sso-login - React Hooks
|
|
229
|
+
*
|
|
230
|
+
* Provides useGovSso and useGovSsoCallback hooks for
|
|
231
|
+
* easy integration with React/Next.js applications.
|
|
232
|
+
*
|
|
233
|
+
* Supports both:
|
|
234
|
+
* - Direct config: useGovSso(config)
|
|
235
|
+
* - Provider context: useGovSso() (when wrapped in GovSsoProvider)
|
|
236
|
+
*/
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Hook สำหรับเริ่มต้น SSO login flow
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```tsx
|
|
243
|
+
* // วิธี 1: ส่ง config ตรง
|
|
244
|
+
* const { login } = useGovSso(config);
|
|
245
|
+
*
|
|
246
|
+
* // วิธี 2: ใช้กับ GovSsoProvider (ไม่ต้องส่ง config)
|
|
247
|
+
* const { login } = useGovSso();
|
|
248
|
+
*
|
|
249
|
+
* return <button onClick={login}>เข้าสู่ระบบ</button>;
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
declare function useGovSso(config?: GovSsoConfig): {
|
|
253
|
+
login: () => void;
|
|
254
|
+
getLoginUrl: () => string;
|
|
255
|
+
client: GovSsoClient;
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
258
|
+
* Hook สำหรับจัดการ SSO callback
|
|
259
|
+
* จะอ่าน `code` จาก URL search params และส่งไปยัง backend อัตโนมัติ
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```tsx
|
|
263
|
+
* // วิธี 1: ส่ง config ตรง
|
|
264
|
+
* const { status, user } = useGovSsoCallback(config);
|
|
265
|
+
*
|
|
266
|
+
* // วิธี 2: ใช้กับ GovSsoProvider
|
|
267
|
+
* const { status, user } = useGovSsoCallback();
|
|
268
|
+
*
|
|
269
|
+
* // วิธี 3: พร้อม callbacks
|
|
270
|
+
* const { status, user } = useGovSsoCallback(config, {
|
|
271
|
+
* onSuccess: (result) => router.push('/dashboard'),
|
|
272
|
+
* onError: (err) => router.push('/login'),
|
|
273
|
+
* });
|
|
274
|
+
* ```
|
|
275
|
+
*/
|
|
276
|
+
declare function useGovSsoCallback(configOrOptions?: GovSsoConfig | {
|
|
277
|
+
onSuccess?: (result: SsoCallbackResult) => void;
|
|
278
|
+
onError?: (error: Error) => void;
|
|
279
|
+
code?: string | null;
|
|
280
|
+
}, maybeOptions?: {
|
|
281
|
+
onSuccess?: (result: SsoCallbackResult) => void;
|
|
282
|
+
onError?: (error: Error) => void;
|
|
283
|
+
code?: string | null;
|
|
284
|
+
}): {
|
|
285
|
+
status: SsoCallbackStatus;
|
|
286
|
+
user: SsoUser | undefined;
|
|
287
|
+
data: Record<string, any> | undefined;
|
|
288
|
+
error: Error | undefined;
|
|
289
|
+
message: string;
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Hook สำหรับ auto-login: เช็ค session ที่มีอยู่ก่อน ถ้าไม่มีก็ redirect ไป SSO อัตโนมัติ
|
|
293
|
+
*
|
|
294
|
+
* ใช้ในหน้าที่ต้องการ protect เช่น dashboard, layout
|
|
295
|
+
* ถ้า user เคย login SSO ไว้แล้ว (session ยังไม่หมดอายุ) จะเข้าเว็บได้ทันทีไม่ต้องกดปุ่ม login ใหม่
|
|
296
|
+
*
|
|
297
|
+
* Flow:
|
|
298
|
+
* 1. เช็ค backend session (GET /auth/me) → ถ้ามี → authenticated ทันที
|
|
299
|
+
* 2. ถ้าไม่มี session + autoRedirect=true → redirect ไป IAM-GOV
|
|
300
|
+
* - ถ้า user login SSO ค้างอยู่ → IAM-GOV จะ redirect กลับมาพร้อม code ทันที (ไม่ต้องกรอกอะไร)
|
|
301
|
+
* - ถ้า user ไม่ได้ login SSO → จะเจอหน้า login ของ IAM-GOV
|
|
302
|
+
* 3. ถ้าไม่มี session + autoRedirect=false → status = 'unauthenticated'
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```tsx
|
|
306
|
+
* // วิธี 1: Auto-redirect (default) — ใส่ในหน้า protected
|
|
307
|
+
* function DashboardPage() {
|
|
308
|
+
* const { status, user } = useAutoLogin({
|
|
309
|
+
* onAuthenticated: (user) => console.log('Welcome back:', user.firstName),
|
|
310
|
+
* });
|
|
311
|
+
*
|
|
312
|
+
* if (status === 'checking') return <p>กำลังตรวจสอบ...</p>;
|
|
313
|
+
* if (status === 'redirecting') return <p>กำลังเปลี่ยนเส้นทาง...</p>;
|
|
314
|
+
* if (status !== 'authenticated') return null;
|
|
315
|
+
*
|
|
316
|
+
* return <div>สวัสดี {user?.firstName}</div>;
|
|
317
|
+
* }
|
|
318
|
+
*
|
|
319
|
+
* // วิธี 2: ไม่ auto-redirect — ให้ user กดปุ่มเอง
|
|
320
|
+
* function HomePage() {
|
|
321
|
+
* const { status, user, login } = useAutoLogin({
|
|
322
|
+
* autoRedirect: false,
|
|
323
|
+
* onAuthenticated: (user) => router.push('/dashboard'),
|
|
324
|
+
* });
|
|
325
|
+
*
|
|
326
|
+
* if (status === 'checking') return <p>กำลังตรวจสอบ...</p>;
|
|
327
|
+
* if (status === 'unauthenticated') return <button onClick={login}>เข้าสู่ระบบ</button>;
|
|
328
|
+
*
|
|
329
|
+
* return <div>สวัสดี {user?.firstName}</div>;
|
|
330
|
+
* }
|
|
331
|
+
*
|
|
332
|
+
* // วิธี 3: ส่ง config ตรง (ไม่ใช้ Provider)
|
|
333
|
+
* const { status, user } = useAutoLogin(ssoConfig, { autoRedirect: true });
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
declare function useAutoLogin(configOrOptions?: GovSsoConfig | AutoLoginOptions, maybeOptions?: AutoLoginOptions): {
|
|
337
|
+
status: AutoLoginStatus;
|
|
338
|
+
user: SsoUser | undefined;
|
|
339
|
+
data: Record<string, any> | undefined;
|
|
340
|
+
login: () => void;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* ปุ่มเข้าสู่ระบบด้วย Gov Center SSO
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```tsx
|
|
348
|
+
* // พื้นฐาน
|
|
349
|
+
* <SsoLoginButton config={ssoConfig} />
|
|
350
|
+
*
|
|
351
|
+
* // กับ Provider (ไม่ต้องส่ง config)
|
|
352
|
+
* <SsoLoginButton />
|
|
353
|
+
*
|
|
354
|
+
* // Custom theme
|
|
355
|
+
* <SsoLoginButton theme={{ primaryColor: '#0066CC' }} />
|
|
356
|
+
*
|
|
357
|
+
* // Custom className (ใช้ CSS เอง)
|
|
358
|
+
* <SsoLoginButton className="my-btn" />
|
|
359
|
+
* ```
|
|
360
|
+
*/
|
|
361
|
+
declare function SsoLoginButton({ config, label, className, style, icon, onBeforeLogin, theme: propTheme, }: SsoLoginButtonProps): react_jsx_runtime.JSX.Element;
|
|
362
|
+
/**
|
|
363
|
+
* Component สำหรับจัดการ SSO callback
|
|
364
|
+
* จะอ่าน code จาก URL params, ส่ง verify, และแสดง loading/success/error
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```tsx
|
|
368
|
+
* // พื้นฐาน
|
|
369
|
+
* <SsoCallbackHandler
|
|
370
|
+
* config={ssoConfig}
|
|
371
|
+
* onSuccess={(result) => router.push('/home')}
|
|
372
|
+
* onError={(err) => router.push('/login')}
|
|
373
|
+
* />
|
|
374
|
+
*
|
|
375
|
+
* // กับ Provider + custom theme
|
|
376
|
+
* <SsoCallbackHandler
|
|
377
|
+
* onSuccess={(result) => router.push('/home')}
|
|
378
|
+
* theme={{ primaryColor: '#0066CC' }}
|
|
379
|
+
* />
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
declare function SsoCallbackHandler({ config, onSuccess, onError, loadingMessage, successMessage, errorMessage, theme: propTheme, }: SsoCallbackHandlerProps): react_jsx_runtime.JSX.Element;
|
|
383
|
+
|
|
384
|
+
export { type AutoLoginOptions, type AutoLoginStatus, DEFAULT_THEME, GovSsoClient, type GovSsoConfig, GovSsoError, GovSsoProvider, type GovSsoProviderProps, SsoCallbackHandler, type SsoCallbackHandlerProps, type SsoCallbackResult, type SsoCallbackStatus, SsoLoginButton, type SsoLoginButtonProps, type SsoTheme, type SsoUser, useAutoLogin, useGovSso, useGovSsoCallback };
|