@kengcom202/kcore-ui 0.5.0 → 0.6.0

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.
@@ -1409,6 +1409,7 @@ function resolveKcAuthConfig(o) {
1409
1409
  thaid: o.thaid ? { label: o.thaid.label || "ThaiD" } : null,
1410
1410
  passwordLogin: o.passwordLogin !== false || !o.sso && !o.thaid,
1411
1411
  register: o.register ?? "none",
1412
+ profileOrgChange: o.profileOrgChange === true,
1412
1413
  orgLabel: o.orgLabel || "หน่วยงาน",
1413
1414
  adminRoles: o.adminRoles ?? ["admin"],
1414
1415
  passwordMinLength: o.passwordMinLength ?? 4,
@@ -2229,6 +2230,8 @@ var KcLoginComponent = class KcLoginComponent {
2229
2230
  <p class="kc-auth-footer">ยังไม่มีบัญชี? <a class="kc-auth-link" [routerLink]="paths.register">ลงทะเบียน</a></p>
2230
2231
  } @else if (config.register === 'sso' && config.sso && methods().sso) {
2231
2232
  <p class="kc-auth-footer">ผู้ใช้ใหม่ กดปุ่ม {{ config.sso.label }} เพื่อลงทะเบียน</p>
2233
+ } @else if (config.register === 'thaid' && config.thaid && methods().thaid) {
2234
+ <p class="kc-auth-footer">ผู้ใช้ใหม่ กดปุ่ม {{ config.thaid.label }} เพื่อลงทะเบียน</p>
2232
2235
  }
2233
2236
  <ng-content />
2234
2237
  </div>
@@ -2377,6 +2380,8 @@ i0.ɵɵngDeclareClassMetadata({
2377
2380
  <p class="kc-auth-footer">ยังไม่มีบัญชี? <a class="kc-auth-link" [routerLink]="paths.register">ลงทะเบียน</a></p>
2378
2381
  } @else if (config.register === 'sso' && config.sso && methods().sso) {
2379
2382
  <p class="kc-auth-footer">ผู้ใช้ใหม่ กดปุ่ม {{ config.sso.label }} เพื่อลงทะเบียน</p>
2383
+ } @else if (config.register === 'thaid' && config.thaid && methods().thaid) {
2384
+ <p class="kc-auth-footer">ผู้ใช้ใหม่ กดปุ่ม {{ config.thaid.label }} เพื่อลงทะเบียน</p>
2380
2385
  }
2381
2386
  <ng-content />
2382
2387
  </div>
@@ -2409,7 +2414,7 @@ function kcContactProblem(email, mobile) {
2409
2414
  var KcRegisterComponent = class KcRegisterComponent {
2410
2415
  config = inject(KC_AUTH_CONFIG);
2411
2416
  paths = KC_AUTH_PATHS;
2412
- ssoLabel = this.config.sso?.label ?? "SSO";
2417
+ ssoLabel = this.config.register === "thaid" ? this.config.thaid?.label ?? "ThaiD" : this.config.sso?.label ?? "SSO";
2413
2418
  auth = inject(KcAuthService);
2414
2419
  http = inject(HttpClient);
2415
2420
  router = inject(Router);
@@ -2442,7 +2447,7 @@ var KcRegisterComponent = class KcRegisterComponent {
2442
2447
  this.error.set("ระบบนี้ไม่เปิดให้ลงทะเบียนเอง กรุณาติดต่อผู้ดูแลระบบ");
2443
2448
  return;
2444
2449
  }
2445
- if (mode === "sso") {
2450
+ if (mode === "sso" || mode === "thaid") {
2446
2451
  this.ticket = this.auth.getTemp(KC_SSO_TICKET_KEY);
2447
2452
  if (!this.ticket) {
2448
2453
  this.error.set(`ยังไม่ได้ยืนยันตัวตน กรุณากลับไปกด "เข้าสู่ระบบด้วย ${this.ssoLabel}" ที่หน้าเข้าสู่ระบบก่อน`);
@@ -2830,13 +2835,7 @@ var KcExternalCallbackBase = class KcExternalCallbackBase {
2830
2835
  }
2831
2836
  if (!code) {
2832
2837
  this.message.set(`กำลังเชื่อมต่อกับ ${this.label}...`);
2833
- this.http.get(`${this.config.apiBase}/user/${this.endpoint}/authorize-url`).subscribe({
2834
- next: ({ url, state }) => {
2835
- this.auth.setTemp(this.stateKey, state);
2836
- this.auth.redirect(url);
2837
- },
2838
- error: () => this.fail(`ไม่สามารถเชื่อมต่อ ${this.label} ได้ กรุณาเริ่มเข้าสู่ระบบใหม่`)
2839
- });
2838
+ this.startFlow(this.endpoint, this.stateKey, this.label);
2840
2839
  return;
2841
2840
  }
2842
2841
  if (!expectedState || expectedState !== params.get("state")) {
@@ -2845,6 +2844,20 @@ var KcExternalCallbackBase = class KcExternalCallbackBase {
2845
2844
  }
2846
2845
  this.exchange(code);
2847
2846
  }
2847
+ startFlow(endpoint, stateKey, label) {
2848
+ this.http.get(`${this.config.apiBase}/user/${endpoint}/authorize-url`).subscribe({
2849
+ next: ({ url, state }) => {
2850
+ this.auth.setTemp(stateKey, state);
2851
+ this.auth.redirect(url);
2852
+ },
2853
+ error: () => this.fail(`ไม่สามารถเชื่อมต่อ ${label} ได้ กรุณาเริ่มเข้าสู่ระบบใหม่`)
2854
+ });
2855
+ }
2856
+ toRegister(ticket) {
2857
+ this.auth.setTemp(KC_SSO_TICKET_KEY, ticket);
2858
+ this.message.set("ยังไม่มีบัญชีในระบบ กำลังไปหน้าลงทะเบียน...");
2859
+ this.router.navigateByUrl(KC_AUTH_PATHS.register, { replaceUrl: true });
2860
+ }
2848
2861
  signIn(session) {
2849
2862
  this.auth.start(session);
2850
2863
  this.router.navigateByUrl(this.auth.safeReturnUrl(this.auth.takeTemp(KC_RETURN_URL_KEY)), { replaceUrl: true });
@@ -2892,9 +2905,13 @@ var KcSsoCallbackComponent = class KcSsoCallbackComponent extends KcExternalCall
2892
2905
  return;
2893
2906
  }
2894
2907
  if (res.mode === "register" && res.ticket) {
2895
- this.auth.setTemp(KC_SSO_TICKET_KEY, res.ticket);
2896
- this.message.set("ยังไม่มีบัญชีในระบบ กำลังไปหน้าลงทะเบียน...");
2897
- this.router.navigateByUrl(KC_AUTH_PATHS.register, { replaceUrl: true });
2908
+ this.toRegister(res.ticket);
2909
+ return;
2910
+ }
2911
+ if (res.mode === "thaid" && this.config.register === "thaid" && this.config.thaid) {
2912
+ const thaid = this.config.thaid.label;
2913
+ this.message.set(`ไม่พบบัญชีผู้ใช้ในระบบ กำลังไปยืนยันตัวตนด้วย ${thaid} เพื่อลงทะเบียน...`);
2914
+ this.startFlow("thaid", KC_THAID_STATE_KEY, thaid);
2898
2915
  return;
2899
2916
  }
2900
2917
  this.fail("ไม่สามารถเข้าสู่ระบบได้ กรุณาลองใหม่อีกครั้ง");
@@ -2962,7 +2979,17 @@ var KcThaidCallbackComponent = class KcThaidCallbackComponent extends KcExternal
2962
2979
  stateKey = KC_THAID_STATE_KEY;
2963
2980
  exchange(code) {
2964
2981
  this.http.post(`${this.config.apiBase}/user/thaid/callback`, { code }).subscribe({
2965
- next: (res) => res?.token && res.user ? this.signIn(res) : this.fail("ไม่สามารถเข้าสู่ระบบได้ กรุณาลองใหม่อีกครั้ง"),
2982
+ next: (res) => {
2983
+ if (res && "token" in res && res.token && res.user) {
2984
+ this.signIn(res);
2985
+ return;
2986
+ }
2987
+ if (res && "mode" in res && res.mode === "register" && res.ticket && this.config.register === "thaid") {
2988
+ this.toRegister(res.ticket);
2989
+ return;
2990
+ }
2991
+ this.fail("ไม่สามารถเข้าสู่ระบบได้ กรุณาลองใหม่อีกครั้ง");
2992
+ },
2966
2993
  error: (err) => this.fail(kcErrorMessage(err, `ไม่สามารถเข้าสู่ระบบผ่าน ${this.label} ได้`))
2967
2994
  });
2968
2995
  }
@@ -3055,10 +3082,12 @@ var KcProfileComponent = class KcProfileComponent {
3055
3082
  success = signal("", ...ngDevMode ? [{ debugName: "success" }] : /* istanbul ignore next */ []);
3056
3083
  fullName = signal("", ...ngDevMode ? [{ debugName: "fullName" }] : /* istanbul ignore next */ []);
3057
3084
  orgName = signal("", ...ngDevMode ? [{ debugName: "orgName" }] : /* istanbul ignore next */ []);
3085
+ orgs = signal([], ...ngDevMode ? [{ debugName: "orgs" }] : /* istanbul ignore next */ []);
3058
3086
  form = inject(FormBuilder).nonNullable.group({
3059
3087
  email: ["", [Validators.required, Validators.email]],
3060
3088
  mobile: ["", [Validators.required, Validators.pattern(/^0\d{9}$/)]],
3061
3089
  username: ["", [Validators.required, Validators.pattern(/^\S+$/)]],
3090
+ org: [""],
3062
3091
  password: [""],
3063
3092
  confirmPassword: [""]
3064
3093
  });
@@ -3070,10 +3099,12 @@ var KcProfileComponent = class KcProfileComponent {
3070
3099
  next: (me) => {
3071
3100
  this.fullName.set(me.fullName);
3072
3101
  this.orgName.set(me.orgName || me.org);
3102
+ if (this.config.profileOrgChange && me.orgs?.length) this.orgs.set(me.orgs);
3073
3103
  this.form.patchValue({
3074
3104
  email: me.email ?? "",
3075
3105
  mobile: me.mobile ?? "",
3076
- username: me.username ?? ""
3106
+ username: me.username ?? "",
3107
+ org: me.org ?? ""
3077
3108
  });
3078
3109
  this.loading.set(false);
3079
3110
  },
@@ -3093,7 +3124,7 @@ var KcProfileComponent = class KcProfileComponent {
3093
3124
  this.success.set("");
3094
3125
  this.form.markAllAsTouched();
3095
3126
  const v = this.form.getRawValue();
3096
- const problem = kcContactProblem(v.email, v.mobile) || kcUsernameProblem(v.username) || kcPasswordProblem(v.password, v.confirmPassword, this.config.passwordMinLength, false);
3127
+ const problem = kcContactProblem(v.email, v.mobile) || kcUsernameProblem(v.username) || kcPasswordProblem(v.password, v.confirmPassword, this.config.passwordMinLength, false) || (this.orgs().length && !v.org ? `กรุณาเลือก${this.config.orgLabel}ที่สังกัด` : "");
3097
3128
  this.error.set(problem);
3098
3129
  if (problem) return;
3099
3130
  const body = {
@@ -3102,6 +3133,7 @@ var KcProfileComponent = class KcProfileComponent {
3102
3133
  mobile: v.mobile.trim(),
3103
3134
  password: v.password
3104
3135
  };
3136
+ if (this.orgs().length) body.org = v.org;
3105
3137
  this.saving.set(true);
3106
3138
  this.http.put(`${this.config.apiBase}/user/me`, body).subscribe({
3107
3139
  next: (res) => {
@@ -3150,11 +3182,19 @@ var KcProfileComponent = class KcProfileComponent {
3150
3182
  <input class="kc-auth-input" type="text" [value]="fullName()" disabled />
3151
3183
  </label>
3152
3184
 
3185
+ @if (orgs().length) {
3186
+ <div class="kc-auth-field">
3187
+ <label class="kc-auth-label" for="kc-profile-org">{{ config.orgLabel }}ที่สังกัด <span class="req">*</span></label>
3188
+ <app-search-select inputId="kc-profile-org" [options]="orgs()" formControlName="org"
3189
+ [placeholder]="'-- เลือก' + config.orgLabel + 'ที่สังกัด --'" />
3190
+ </div>
3191
+ } @else {
3153
3192
  <label class="kc-auth-field">
3154
3193
  <span>{{ config.orgLabel }}ที่สังกัด</span>
3155
3194
  <input class="kc-auth-input" type="text" [value]="orgName()" disabled />
3156
3195
  <small class="kc-auth-hint">หากต้องการย้าย{{ config.orgLabel }} กรุณาติดต่อผู้ดูแลระบบ</small>
3157
3196
  </label>
3197
+ }
3158
3198
 
3159
3199
  <label class="kc-auth-field">
3160
3200
  <span>อีเมล <span class="req">*</span></span>
@@ -3259,6 +3299,17 @@ var KcProfileComponent = class KcProfileComponent {
3259
3299
  ],
3260
3300
  outputs: ["ngModelChange"]
3261
3301
  },
3302
+ {
3303
+ kind: "component",
3304
+ type: SearchSelectComponent,
3305
+ selector: "app-search-select",
3306
+ inputs: [
3307
+ "options",
3308
+ "placeholder",
3309
+ "searchHint",
3310
+ "inputId"
3311
+ ]
3312
+ },
3262
3313
  {
3263
3314
  kind: "component",
3264
3315
  type: KcPasswordToggleComponent,
@@ -3277,7 +3328,11 @@ i0.ɵɵngDeclareClassMetadata({
3277
3328
  args: [{
3278
3329
  selector: "kc-profile",
3279
3330
  standalone: true,
3280
- imports: [ReactiveFormsModule, KcPasswordToggleComponent],
3331
+ imports: [
3332
+ ReactiveFormsModule,
3333
+ SearchSelectComponent,
3334
+ KcPasswordToggleComponent
3335
+ ],
3281
3336
  template: `
3282
3337
  <div class="kc-auth-page single">
3283
3338
  <div class="kc-auth-panel">
@@ -3294,11 +3349,19 @@ i0.ɵɵngDeclareClassMetadata({
3294
3349
  <input class="kc-auth-input" type="text" [value]="fullName()" disabled />
3295
3350
  </label>
3296
3351
 
3352
+ @if (orgs().length) {
3353
+ <div class="kc-auth-field">
3354
+ <label class="kc-auth-label" for="kc-profile-org">{{ config.orgLabel }}ที่สังกัด <span class="req">*</span></label>
3355
+ <app-search-select inputId="kc-profile-org" [options]="orgs()" formControlName="org"
3356
+ [placeholder]="'-- เลือก' + config.orgLabel + 'ที่สังกัด --'" />
3357
+ </div>
3358
+ } @else {
3297
3359
  <label class="kc-auth-field">
3298
3360
  <span>{{ config.orgLabel }}ที่สังกัด</span>
3299
3361
  <input class="kc-auth-input" type="text" [value]="orgName()" disabled />
3300
3362
  <small class="kc-auth-hint">หากต้องการย้าย{{ config.orgLabel }} กรุณาติดต่อผู้ดูแลระบบ</small>
3301
3363
  </label>
3364
+ }
3302
3365
 
3303
3366
  <label class="kc-auth-field">
3304
3367
  <span>อีเมล <span class="req">*</span></span>