- Notifications
You must be signed in to change notification settings - Fork0
GiskardB/coreui-angular-oidc-client
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is an example of how to integratetheangular-auth-oidc-client library withCoreUI Template for Angular version using Code Flow Authentication.
This repository has been started from clone theCoreUI Template for Angular version.To use the repository:
- Clone this repository
- Run
npm installto get the dependencies - Create an OpenID Connect App (for example onOkta ) and setting clientId and statsServer on enviroment.ts
- Run
ng serve --opento get it running onhttp://localhost:4200 - Login on page of your Identity server
The application is supposed to look somewhat like this:
Install the library angular-auth-oidc-client
npm install angular-auth-oidc-client --save
Initialize the library at startup of application modifing and adding some configurations in app.module.ts:
import{AuthModule,LogLevel,OidcConfigService}from'angular-auth-oidc-client';import{APP_INITIALIZER,NgModule}from'@angular/core';import{environment}from'../environments/environment';...exportfunctionconfigureAuth(oidcConfigService:OidcConfigService){return()=>{oidcConfigService.withConfig({stsServer:environment.stsServer,redirectUrl:window.location.origin,clientId:environment.clientId,scope:'openid profile email',responseType:'code',triggerAuthorizationResultEvent:true,postLogoutRedirectUri:`${window.location.origin}/#/login`,startCheckSession:false,silentRenew:true,silentRenewUrl:`${window.location.origin}/silent-renew.html`,postLoginRoute:'/',forbiddenRoute:'/forbidden',unauthorizedRoute:'/unauthorized',logLevel:LogLevel.Debug,historyCleanupOff:true,autoUserinfo:true});}}...@NgModule({imports:[ ...AuthModule.forRoot()],
For configure the parameters of oidc-client use the official guideOIDC Client
Add import to app.component.ts and modify the app.component.ts to redirect to login page if the user is not authenticated.
import{OidcSecurityService}from'angular-auth-oidc-client';...exportclassAppComponentimplementsOnInit{ ...ngOnInit(){ ...//Check if user is authenticate otherwise redirect to login pagethis.oidcSecurityService.checkAuth().subscribe((isAuthenticated)=>{if(!isAuthenticated){if('/login'!==window.location.pathname){this.write('redirect',window.location.pathname);this.router.navigate(['/login']);}}if(isAuthenticated){this.navigateToStoredEndpoint();}});}privatenavigateToStoredEndpoint(){constpath=this.read('redirect');if(this.router.url===path){return;}if(path.toString().includes('/unauthorized')){this.router.navigate(['/']);}else{this.router.navigate([path]);}}privateread(key:string):any{constdata=localStorage.getItem(key);if(data){returnJSON.parse(data);}return;}privatewrite(key:string,value:any):void{localStorage.setItem(key,JSON.stringify(value));}}
For redirect to the Identity server login page you can add Login component (see folder app/views/login) to app.routing.ts and on app.module.ts
import{LoginComponent}from'./views/login/login.component';...declarations:[ ...LoginComponent, ...],
Add the pagesilent-renew.html in folder src, for silent renew token and, setting the values of enviroment.ts or enviroment.prod.ts for the identity server:
- statsServer: URL of Oauth Identity server
- clientId: Client id for Identity server
In this example the user information will be shown in dashboard.component.ts and in the default-layout.component.ts will show the logout function.
About
Example of integration of OIDC Client for Oauth2 with CoreUI Angular version
Topics
Resources
Code of conduct
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.
