@hichchi/ngx-auth 0.0.9 → 0.0.11

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hichchi/ngx-auth",
3
3
  "description": "A utility library for Angular applications with common services, interceptors, and state management",
4
- "version": "0.0.9",
4
+ "version": "0.0.11",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -19,10 +19,10 @@
19
19
  "@angular/core": "^21.1.4",
20
20
  "@angular/forms": "^21.1.4",
21
21
  "@angular/router": "^21.1.4",
22
- "@hichchi/nest-connector": "0.0.9",
23
- "@hichchi/ngx-ui": "0.0.9",
24
- "@hichchi/ngx-utils": "0.0.9",
25
- "@hichchi/utils": "0.0.9",
22
+ "@hichchi/nest-connector": "0.0.11",
23
+ "@hichchi/ngx-ui": "0.0.11",
24
+ "@hichchi/ngx-utils": "0.0.11",
25
+ "@hichchi/utils": "0.0.11",
26
26
  "@ngrx/signals": "^21.0.1",
27
27
  "rxjs": "^7.8.2"
28
28
  },
@@ -194,10 +194,10 @@ interface AuthGuardOption {
194
194
  redirect: string;
195
195
  }
196
196
 
197
- interface RoleGuardOption {
198
- /** The required state of the role */
199
- state?: string;
200
- /** The route to redirect to if the role exist */
197
+ interface RoleGuardOption<T = string> {
198
+ /** Role name that should be redirected when the required role check fails */
199
+ state?: T;
200
+ /** Route path to navigate to when `state` matches the current role */
201
201
  redirect: string;
202
202
  }
203
203
 
@@ -568,22 +568,24 @@ declare function authGuard(condition: AuthGuardCondition, state: boolean, fallba
568
568
  * Creates a role-based authorization guard function for Angular route protection
569
569
  *
570
570
  * This function creates a route guard that protects routes based on user roles.
571
- * It checks if the current user's role matches the required role, and if not,
572
- * it evaluates the provided options to determine the appropriate action (redirect
573
- * or sign out).
571
+ * It first checks whether the current user's role matches the required role(s).
572
+ * If it does not match, it evaluates the provided options to determine the
573
+ * appropriate action (redirect or sign out).
574
574
  *
575
575
  * The guard integrates with the AuthState service to check the current user's role
576
- * and uses the Angular Router for navigation when redirects are needed. If no
577
- * matching role or redirect option is found, the user is automatically signed out.
576
+ * and uses the Angular Router for navigation when redirects are needed. If the
577
+ * required role check fails and no matching redirect option is found, the user is
578
+ * automatically signed out.
578
579
  *
579
580
  * Key features:
580
581
  * - Role-based route protection
582
+ * - Supports both a single required role and multiple allowed roles
581
583
  * - Configurable redirect options based on user roles
582
584
  * - Automatic sign-out for unauthorized access
583
585
  * - Integration with AuthState for reactive role checking
584
586
  * - Support for multiple role-based redirect scenarios
585
587
  *
586
- * @param role - The required role name that the user must have to access the route
588
+ * @param role - The required role name (or list of allowed roles) for route access
587
589
  * @param options - Array of role guard options that define redirect behavior for different user roles
588
590
  * @returns A CanActivateFn that evaluates role-based authorization and handles navigation
589
591
  *
@@ -629,10 +631,32 @@ declare function authGuard(condition: AuthGuardCondition, state: boolean, fallba
629
631
  * ];
630
632
  * ```
631
633
  *
634
+ * @example
635
+ * ```typescript
636
+ * // Allowing multiple roles
637
+ * const routes: Routes = [
638
+ * {
639
+ * path: 'reports',
640
+ * component: ReportsComponent,
641
+ * canActivate: [roleGuard(['admin', 'manager'], [
642
+ * { state: 'user', redirect: '/dashboard' }
643
+ * ])]
644
+ * }
645
+ * ];
646
+ * ```
647
+ *
648
+ * @example
649
+ * ```typescript
650
+ * // Evaluation order:
651
+ * // 1) role match => allow
652
+ * // 2) role mismatch + matching option.state => redirect
653
+ * // 3) role mismatch + no matching option.state => sign out
654
+ * ```
655
+ *
632
656
  * @see {@link AuthState} Service that provides authentication and role state information
633
657
  * @see {@link RoleGuardOption} Interface for configuring role-based redirect options
634
658
  */
635
- declare function roleGuard(role: string, options: RoleGuardOption[]): CanActivateFn;
659
+ declare function roleGuard<T = string>(role: T | T[], options: RoleGuardOption<T>[]): CanActivateFn;
636
660
 
637
661
  /**
638
662
  * Array of authentication error codes that should trigger token refresh instead of immediate redirect
@@ -895,7 +919,7 @@ declare const AuthState: _angular_core.Type<{
895
919
  /**
896
920
  * Public typed shape of the `AuthState` signal store.
897
921
  */
898
- type AuthState<D = unknown, R extends string = string, P extends string = string, U extends User<R, P> = User<R, P>> = {
922
+ type AuthState<D = unknown, R extends string = string, P extends string = string, U = User<R, P>> = {
899
923
  signedIn: Signal<boolean>;
900
924
  sessionId: Signal<string | null>;
901
925
  user: Signal<U | null>;
@@ -905,7 +929,9 @@ type AuthState<D = unknown, R extends string = string, P extends string = string
905
929
  refreshTokenExpiresOn: Signal<Date | null>;
906
930
  data: Signal<D>;
907
931
  hasAccessToken: Signal<boolean>;
908
- role: Signal<U["role"] | null | undefined>;
932
+ role: Signal<U extends {
933
+ role: infer T;
934
+ } ? T : null | undefined>;
909
935
  roleName: Signal<R | null | undefined>;
910
936
  permissions: Signal<P[]>;
911
937
  emailVerified: Signal<boolean>;