@desarrollofdc/ng-auth-v10 v10.0.23
FDC Authentication Library - v10.0.1 (fdc-auth)
Updated to Angular version 10.1.0 as well as PrimeNg version 10.0.2.
This library is intended to decrease the development time required to start a new application based on angular. It provides authentication under the hood and helpers to specify which operations are available for the logged user.
This new version recover the whole functionality that have the previous one.
Implementation
Install from npm running npm i @desarrollofdc/ng-auth
In app.module.ts add:
@NgModule({
imports: [
NgAuthModule.forRoot({
apiGateway: API_GATEWAY,
applicationId: APPLICATION_ID,
authUri: AUTHENTICATION_URL,
environment: ENVIRONMENT,
redirectUrl: REDIRECT_URL,
})
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}Note:
API_GATEWAYis the api gateway's url normally https://gateway.fdcteam.comAPPLICATION_IDis the id registered in the databaseAUTHENTICATION_URLis the url where theAuthentication Serviceis runningENVIRONMENTis the environment used for the authentication (test, production, demo)REDIRECT_URLis the url where the user will be redirected once has logged (normally where the SPA is running)
In app.component.html add:
<fdc-auth-wrapper>
<--!YOUR CODE, NORMALLY -->
<router-outlet></router-outlet>
</fdc-auth-wrapper>Note: It will make sure to load the application once the user is logged, otherwise the user will be redirected to the
authentication serviceto request a token
Features
This library provides the following features:
Auth Wrapper
To start protecting the frontend just add the fdc-auth-wrapper component and wrap what you expect to protect, for instance:
<fdc-auth-wrapper>
<h1>Angular Router</h1>
<nav>
<a routerLink="/crisis-center" routerLinkActive="active">Crisis Center</a>
<a routerLink="/heroes" routerLinkActive="active">Heroes</a>
</nav>
<router-outlet></router-outlet>
</fdc-auth-wrapper>Route Guards
This library provides AuthGuard to protect routes, this way, you can ensure that certain route will only be accessible if the user has access to the module related to that route.
const routes: Routes = [
{
path: '', canActivateChild: [AuthGuard],
children: [
{path: 'crisis-center', component: CrisisListComponent},
{
path: 'heroes',
component: HeroListComponent,
data: {title: 'Heroes List', moduleId: MODULE_ID, componentId: COMPONENT_ID, operationId: OPERATION_ID}
},
{
path: '',
redirectTo: '/heroes',
pathMatch: 'full'
},
]
},
{path: '**', component: PageNotFoundComponent}
];Note: Provide moduleId, componentId or operationId in data, this way the library will automatically fetch the user accesses and will only render if the user has access to that module, component and/or operation
Token Injection
An interceptor is automatically configured to inject a Bearer token to every request, this includes GraphQL requests.
Render conditional components
This library provides fdc-auth-conditional-component which renders a child only if a user has access to a respective operation, component, or module. It is used as follows:
<fdc-auth-conditional-component [operationId]="YOUR_OPERATION_ID" [componentId]="YOUR_COMPONENT_ID" [condition]="YOUR_CONDITION" >
<your-depentent-components></your-depentent-components>
</fdc-auth-conditional-component>Note:
your-dependent-componentswill render if the user has access to the operation and/or component specified through props.operationId, componentId and condition are optional,
conditional-componentonly takes in count the props provided.
Base layout
The library a FcdBaseLayout component, which includes a base styling for the whole app, this component accepts the following parameters:
- name the app name.
- hideStaticSidebarByDefault show/hide the sidebar menu. It's a
booleantype.falseby default. - showLanguage show a component for select language, it's a
booleantype.falseby default. - routes which is of type
MenuItem[] | MenuItem.
<fdc-auth-base-layout [hideStaticSidebarByDefault]="true" [showLanguage]="showLanguage" [routes]="routes" applicationName="Test">
<h1>Angular Router</h1>
<nav>
<a routerLink="/crisis-center" routerLinkActive="active">Crisis Center</a>
<a routerLink="/heroes" routerLinkActive="active">Heroes</a>
</nav>
<router-outlet></router-outlet>
</fdc-auth-base-layout>This base layout already includes a logout option, and display the apps the logged user can access to. You have two options to change the styles used by your application
- add specific styles in
angular.json, this way those styles will be added to your application styles
{
"projects": {
"your_project": {
"architect": {
"build": {
"options": {
"styles": [
"node_modules/@desarrollofdc/ng-auth/assets/theme/theme-fdc-indigo.css",
"node_modules/@desarrollofdc/ng-auth/assets/layout/css/layout-fdc-indigo.css"
]
}
}
}
}
}
}- Specify assets to copy to the final build and configure your project to use them
{
"projects": {
"your_project": {
"architect": {
"build": {
"options": {
"assets": [
{
"glob": "layout-joomla-v4.css",
"input": "./node_modules/@desarrollofdc/ng-auth/assets/layout/css",
"output": "./assets/layout/css"
},
{
"glob": "layout-orange-v4.css",
"input": "./node_modules/@desarrollofdc/ng-auth/assets/layout/css",
"output": "./assets/layout/css"
}
]
}
}
}
}
}
}In main.ts add those styles to your html;
const {layout, theme, version} = environment.style;
const layoutElement = document.createElement('link');
layoutElement.href = `assets/layout/css/layout-${layout}.css`;
layoutElement.rel = 'stylesheet';
layoutElement.type = 'text/css';
const themeElement = document.createElement('link');
themeElement.href = `assets/theme/theme-${theme}.css`;
themeElement.rel = 'stylesheet';
themeElement.type = 'text/css';
document.head.append(layoutElement, themeElement);Further help
To get more help contact Jeremy Perez
Pending Features
- Request new token when token expires
Development
run the app
To run the app you have to the following commands in sequence:
ng build ng-auth --watchthis will watch changes and rebuild the library. Run the following commands in a new terminal.npm run build-assetsorgulp build-assetsng serve
Structure
This namespace has two projects auth-showcase and ng-auth-v10. ng-auth-v10 has the following structure:
ng-auth
├─ src
├─ assets
├─ lib
├─ api
├─ guard
├─ interceptors
├─ rest
├─ service
├─ store
├─ AuthLibConfig.ts
├─ fdc-auth-components
├─ fdc-auth-base-layout
├─ fdc-auth-conditional-component
├─ fdc-auth-unauthorized
├─ fdc-auth-wrapper
├─ fdc-auth-components
├─ fdc-splitbutton
├─ types
├─ ng-auth-v10.module.ts
├─ public-api.tsassetscontains all the assets used by the apps, it contains layouts, custom-ui, and themes.guardonly containauth-guardwhich is a guard to protect routesinterceptoscontains two interceptorsresponse.interceptorandtoken.interceptorbut onlytoken.interceptoris being used and configured under the hood.restcontains services used to fetch data fromauthentication-serviceservicecontains services with auth related logicfdc-auth-componentscontains the auth-components provided by the libraryfdc-auth-wrapperchecks if the user is logged, otherwise it will request a new token. check authorization flowfdc-auth-base-layoutis the layout structure used in order to keep the same styles across the apps developed by fdc-teamfdc-auth-conditional-componentrenders a component conditionally if and only if the user has access to the componentId or operationId specified by propsfdc-auth-unauthorizedcontains components rendered byauth-guardwhen the user does not have access to a module, operation or component.typescontains interfaces defining types used across the libraryng-auth-v10.module.tsis the module used to configure the library
Deployment
Deployments are made manually to npmjs.org where libraries are hosted, first is necessary to build the library and its assets
and then upload the library's new version, the whole process can be made by running npm run build:publish
Make sure to specify the new library's version in
projects/ng-auth/package.jsonMake sure to add the token to your .npmrc config file
Components
FdcSplitButton
FdcSplitButton groups a set of commands in an overlay with a default command. Use the same PrimeNg styles, events and properties. This include a new property.
| Name | Type | Default | Description |
|---|---|---|---|
| showDefaultBtn | Boolean | false | Show the first button |
<fdc-splitButton [showDefaultBtn]="false" [model]="actions" styleClass="ui-button-secondary"></fdc-splitButton>4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago