@brggroup/share-lib 0.0.31 → 0.0.33

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.
@@ -1,5 +1,5 @@
1
1
  import * as i0 from '@angular/core';
2
- import { inject, Injectable, Component, HostListener, ViewContainerRef, Input, ViewChild, EventEmitter, Output, Directive, Optional, Self, HostBinding, forwardRef, Pipe } from '@angular/core';
2
+ import { inject, Injectable, NgZone, Component, HostListener, ViewContainerRef, Input, ViewChild, EventEmitter, Output, Directive, Optional, Self, HostBinding, forwardRef, Pipe } from '@angular/core';
3
3
  import * as i1$3 from '@angular/router';
4
4
  import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
5
5
  import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
@@ -477,6 +477,7 @@ class TokenStorage {
477
477
 
478
478
  const URLs = {
479
479
  login: '/api/Auth/Login',
480
+ register: '/api/Auth/Register',
480
481
  pingAuth: '/api/Auth/PingAuth',
481
482
  changePassword: '/api/Auth/ChangePassword',
482
483
  setPass: '/api/Auth/SetPass',
@@ -484,6 +485,7 @@ const URLs = {
484
485
  class AuthService extends HTTPService {
485
486
  notiService = inject(NotiService);
486
487
  commonService = inject(CommonService);
488
+ ngZone = inject(NgZone);
487
489
  router = inject(Router);
488
490
  translate = inject(TranslateService);
489
491
  async refreshToken() {
@@ -492,18 +494,23 @@ class AuthService extends HTTPService {
492
494
  return null;
493
495
  const body = { rft: refreshToken };
494
496
  try {
495
- const result = (await firstValueFrom(this.httpClient.post(AppGlobals.apiEndpoint + '/api/Auth/RefreshToken', body)));
496
- if (result?.IsSuccess) {
497
- TokenStorage.saveTokenRft(result.Data);
498
- return result;
497
+ const res = await this.sendRftToken(body);
498
+ if (res?.IsSuccess) {
499
+ TokenStorage.saveTokenRft(res.Data);
500
+ return res;
499
501
  }
502
+ else {
503
+ return null;
504
+ }
505
+ }
506
+ catch (err) {
507
+ console.log(err);
500
508
  }
501
- catch { }
502
509
  return null;
503
510
  }
504
511
  async pingAuth() {
505
512
  try {
506
- const res = await this.post(AppGlobals.apiEndpoint + URLs.pingAuth);
513
+ const res = await this.get(AppGlobals.apiEndpoint + URLs.pingAuth, true);
507
514
  return res?.IsSuccess;
508
515
  }
509
516
  catch {
@@ -524,6 +531,7 @@ class AuthService extends HTTPService {
524
531
  }
525
532
  this.notiService.success('Đăng nhập thành công');
526
533
  TokenStorage.saveToken(res);
534
+ this.startWatching();
527
535
  if (returnUrl) {
528
536
  this.router.navigateByUrl(returnUrl);
529
537
  }
@@ -539,9 +547,18 @@ class AuthService extends HTTPService {
539
547
  headers: new HttpHeaders().set('Content-Type', 'application/json'),
540
548
  }));
541
549
  }
550
+ async sendRftToken(body) {
551
+ return firstValueFrom(this.httpClient.post(AppGlobals.apiEndpoint + '/api/Auth/RefreshToken', body, {
552
+ headers: new HttpHeaders().set('Content-Type', 'application/json'),
553
+ }));
554
+ }
542
555
  signOut() {
543
556
  TokenStorage.clearToken();
544
- this.router.navigate(['/login']);
557
+ this.stopWatching();
558
+ const currentUrl = this.router.routerState.snapshot.url;
559
+ this.router.navigate(['/login'], {
560
+ queryParams: { returnUrl: currentUrl || '/' },
561
+ });
545
562
  }
546
563
  getUserMenu() {
547
564
  let data = {
@@ -568,6 +585,51 @@ class AuthService extends HTTPService {
568
585
  setPass(data) {
569
586
  return this.postData(AppGlobals.apiEndpoint + URLs.setPass, data);
570
587
  }
588
+ async signUp(d) {
589
+ try {
590
+ const res = await this.sendSignUp(d);
591
+ if (!res.IsSuccess) {
592
+ this.notiService.error(res.ErrorMessage);
593
+ return;
594
+ }
595
+ this.notiService.success('Đăng ký thành công');
596
+ this.router.navigateByUrl('/login');
597
+ }
598
+ catch (err) {
599
+ console.log(err);
600
+ this.notiService.handleError(err);
601
+ }
602
+ }
603
+ async sendSignUp(d) {
604
+ return this.postData(AppGlobals.apiEndpoint + URLs.register, d);
605
+ }
606
+ idleTimer;
607
+ idleTimeInMinutes = 15; // phút
608
+ idleTime = this.idleTimeInMinutes * 60 * 1000;
609
+ startWatching() {
610
+ console.log(`-- start watching idle (${this.idleTimeInMinutes} ${this.idleTimeInMinutes == 1 ? 'min' : 'mins'}) --`);
611
+ this.resetTimer();
612
+ window.addEventListener('mousemove', () => this.resetTimer());
613
+ window.addEventListener('keydown', () => this.resetTimer());
614
+ window.addEventListener('click', () => this.resetTimer());
615
+ }
616
+ resetTimer() {
617
+ if (this.idleTimer) {
618
+ clearTimeout(this.idleTimer);
619
+ }
620
+ this.ngZone.runOutsideAngular(() => {
621
+ this.idleTimer = setTimeout(() => {
622
+ this.ngZone.run(() => {
623
+ console.log('-- idle logout --');
624
+ this.signOut(); // auto logout
625
+ });
626
+ }, this.idleTime);
627
+ });
628
+ }
629
+ stopWatching() {
630
+ console.log('-- stop watching idle --');
631
+ clearTimeout(this.idleTimer);
632
+ }
571
633
  static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.7", ngImport: i0, type: AuthService, deps: null, target: i0.ɵɵFactoryTarget.Injectable });
572
634
  static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.7", ngImport: i0, type: AuthService, providedIn: 'root' });
573
635
  }
@@ -687,7 +749,6 @@ function handle401(req, next, router, authService) {
687
749
  const res = resAny;
688
750
  isRefreshing = false;
689
751
  if (!res?.IsSuccess) {
690
- TokenStorage.clearToken();
691
752
  const currentUrl = router.routerState.snapshot.url;
692
753
  if (!initialReturnUrl && !currentUrl.startsWith('/login')) {
693
754
  initialReturnUrl = currentUrl;
@@ -695,10 +756,9 @@ function handle401(req, next, router, authService) {
695
756
  router.navigate(['/login'], {
696
757
  queryParams: { returnUrl: initialReturnUrl || '/' },
697
758
  });
759
+ refreshTokenSubject.next(null);
698
760
  return EMPTY;
699
761
  }
700
- // Lưu token mới
701
- TokenStorage.saveToken(res);
702
762
  // Phát token mới cho các request đang chờ
703
763
  refreshTokenSubject.next(TokenStorage.getToken());
704
764
  // Retry request
@@ -884,6 +944,15 @@ class BaseComponent {
884
944
  getUrlData(key) {
885
945
  return this.route.snapshot.data[key];
886
946
  }
947
+ setQueryParam(name, value) {
948
+ this.router.navigate([], {
949
+ relativeTo: this.route,
950
+ queryParams: {
951
+ [name]: value,
952
+ },
953
+ queryParamsHandling: 'merge',
954
+ });
955
+ }
887
956
  get url() {
888
957
  return this.router.url;
889
958
  }