ngx-cho-security
v1.0.0
Published
This library provides authentication and authorization services for CHO Angular applications using OpenID Connect (OIDC).
Readme
NgxChoSecurity
This library provides authentication and authorization services for CHO Angular applications using OpenID Connect (OIDC).
Generated with Angular CLI version 17.3.0.
Overview
NgxChoSecurity includes:
AuthServicefor OIDC authentication flowsAuthUsermodel with role and claim checking- Authentication guards and interceptors
- Authorization policies and context
- Sign-in/sign-out components and callbacks
Dependencies
This library depends on:
- ngx-cho-common (must be built first)
- oidc-client
Installation
npm install ngx-cho-security ngx-cho-common oidc-clientBreaking Changes
ngx-spinner and ngx-toastr are no longer used directly by this package. Applications that want security sign-in redirects to show loading state should provide CHO_LOADING_ADAPTER. Applications that want unauthorized-route messages should provide CHO_NOTIFICATION_ADAPTER.
Build
Important: Build ngx-cho-common first before building this library.
# Build dependencies first
ng build ngx-cho-common
# Then build this library
ng build ngx-cho-securityBuild artifacts will be stored in the dist/ngx-cho-security directory.
Development
Code Scaffolding
ng generate component component-name --project ngx-cho-security
ng generate service service-name --project ngx-cho-security
ng generate guard guard-name --project ngx-cho-securityNote: Always include
--project ngx-cho-securityor the component will be added to the default project.
Running Tests
ng test ngx-cho-securityPublishing
- Update version in
projects/ngx-cho-security/package.json. Use a major version for the UI dependency removal. - Build dependencies and library:
ng build ngx-cho-common ng build ngx-cho-security --configuration production - Publish to npm:
cd dist/ngx-cho-security npm publish
Configuration
The library requires configuration for OIDC settings:
import { CHO_SECURITY_CONFIG } from 'ngx-cho-security';
// In your app.module.ts or main.ts
providers: [
{
provide: CHO_SECURITY_CONFIG,
useValue: {
userManagerSettings: {
authority: 'your-oidc-authority',
client_id: 'your-client-id',
redirect_uri: 'your-redirect-uri',
// ... other OIDC settings
},
policies: [
// Define your authorization policies
]
}
}
]Usage
import { AuthService, AuthUser } from 'ngx-cho-security';
export class MyComponent {
constructor(private authService: AuthService) {}
async login() {
await this.authService.startAuthentication();
}
checkUserRole() {
const user = this.authService.currentUser;
if (user.hasAnyRole('ch:Employee', 'ch:Admin')) {
// User has required role
}
}
}