@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/README.md +191 -116
- package/fesm2022/hichchi-ngx-auth.mjs +35 -12
- package/fesm2022/hichchi-ngx-auth.mjs.map +1 -1
- package/package.json +5 -5
- package/types/hichchi-ngx-auth.d.ts +39 -13
|
@@ -1881,22 +1881,24 @@ function authGuard(param, state, fallbackRedirect) {
|
|
|
1881
1881
|
* Creates a role-based authorization guard function for Angular route protection
|
|
1882
1882
|
*
|
|
1883
1883
|
* This function creates a route guard that protects routes based on user roles.
|
|
1884
|
-
* It checks
|
|
1885
|
-
* it evaluates the provided options to determine the
|
|
1886
|
-
* or sign out).
|
|
1884
|
+
* It first checks whether the current user's role matches the required role(s).
|
|
1885
|
+
* If it does not match, it evaluates the provided options to determine the
|
|
1886
|
+
* appropriate action (redirect or sign out).
|
|
1887
1887
|
*
|
|
1888
1888
|
* The guard integrates with the AuthState service to check the current user's role
|
|
1889
|
-
* and uses the Angular Router for navigation when redirects are needed. If
|
|
1890
|
-
*
|
|
1889
|
+
* and uses the Angular Router for navigation when redirects are needed. If the
|
|
1890
|
+
* required role check fails and no matching redirect option is found, the user is
|
|
1891
|
+
* automatically signed out.
|
|
1891
1892
|
*
|
|
1892
1893
|
* Key features:
|
|
1893
1894
|
* - Role-based route protection
|
|
1895
|
+
* - Supports both a single required role and multiple allowed roles
|
|
1894
1896
|
* - Configurable redirect options based on user roles
|
|
1895
1897
|
* - Automatic sign-out for unauthorized access
|
|
1896
1898
|
* - Integration with AuthState for reactive role checking
|
|
1897
1899
|
* - Support for multiple role-based redirect scenarios
|
|
1898
1900
|
*
|
|
1899
|
-
* @param role - The required role name
|
|
1901
|
+
* @param role - The required role name (or list of allowed roles) for route access
|
|
1900
1902
|
* @param options - Array of role guard options that define redirect behavior for different user roles
|
|
1901
1903
|
* @returns A CanActivateFn that evaluates role-based authorization and handles navigation
|
|
1902
1904
|
*
|
|
@@ -1942,6 +1944,28 @@ function authGuard(param, state, fallbackRedirect) {
|
|
|
1942
1944
|
* ];
|
|
1943
1945
|
* ```
|
|
1944
1946
|
*
|
|
1947
|
+
* @example
|
|
1948
|
+
* ```typescript
|
|
1949
|
+
* // Allowing multiple roles
|
|
1950
|
+
* const routes: Routes = [
|
|
1951
|
+
* {
|
|
1952
|
+
* path: 'reports',
|
|
1953
|
+
* component: ReportsComponent,
|
|
1954
|
+
* canActivate: [roleGuard(['admin', 'manager'], [
|
|
1955
|
+
* { state: 'user', redirect: '/dashboard' }
|
|
1956
|
+
* ])]
|
|
1957
|
+
* }
|
|
1958
|
+
* ];
|
|
1959
|
+
* ```
|
|
1960
|
+
*
|
|
1961
|
+
* @example
|
|
1962
|
+
* ```typescript
|
|
1963
|
+
* // Evaluation order:
|
|
1964
|
+
* // 1) role match => allow
|
|
1965
|
+
* // 2) role mismatch + matching option.state => redirect
|
|
1966
|
+
* // 3) role mismatch + no matching option.state => sign out
|
|
1967
|
+
* ```
|
|
1968
|
+
*
|
|
1945
1969
|
* @see {@link AuthState} Service that provides authentication and role state information
|
|
1946
1970
|
* @see {@link RoleGuardOption} Interface for configuring role-based redirect options
|
|
1947
1971
|
*/
|
|
@@ -1950,15 +1974,14 @@ function roleGuard(role, options) {
|
|
|
1950
1974
|
return async (_route, _state) => {
|
|
1951
1975
|
const router = inject(Router);
|
|
1952
1976
|
const authState = inject(AuthState);
|
|
1953
|
-
|
|
1977
|
+
const currentRole = authState.roleName();
|
|
1978
|
+
if (Array.isArray(role) ? role.includes(currentRole) : role === currentRole) {
|
|
1954
1979
|
return true;
|
|
1955
1980
|
}
|
|
1956
|
-
for
|
|
1957
|
-
if (
|
|
1958
|
-
return true;
|
|
1959
|
-
}
|
|
1960
|
-
else if (option.state === authState.roleName()) {
|
|
1981
|
+
for (const option of options) {
|
|
1982
|
+
if (option.state === currentRole) {
|
|
1961
1983
|
const redirectPath = option.redirect.startsWith("/") ? option.redirect : `/${option.redirect}`;
|
|
1984
|
+
// eslint-disable-next-line no-await-in-loop
|
|
1962
1985
|
await router.navigateByUrl(redirectPath);
|
|
1963
1986
|
return false;
|
|
1964
1987
|
}
|