I am not a Typescript or Angular developer. This example code is based on ChatGPT-generated code (usually wrong on the
first dozen tries), copying examples, reading the docs, and a little help from an actual Angular developer.
Feel free to submit PRs to fix the code!
For this demo, I've selected
@openapitools/openapi-generator-cli
which does a really great job of generating Angular models and services
based on the OAS 3.x schema generated by spectacular.
Unlike the JSONAPI-specific packages, this one doesn't know the details of a JSONAPI.org spec so you need to drill down into the
response a bit as in this example. The good news is all the necessary models were created based on the OAS schema document.
Per the Quickstart we can use the configuration wizard
to do intial setup:
1 2 3 4 5 6 7 8 910111213141516171819202122232425
oas-angular-app$ ng add angular-auth-oidc-client
✔ Determining Package Manager
› Using package manager: npm
✔ Searching for compatible package version
› Found compatible package version: angular-auth-oidc-client@18.0.2.
✔ Loading package information from registry
✔ Confirming installation
✔ Installing package
? What flow to use? OIDC Code Flow PKCE using refresh tokens
? Please enter your authority URL or Azure tenant id or Http config URL http://localhost:8000/o/.well-known/openid-configuration/
🔎 Running checks...
✅️ Project found, working with 'oas-angular-app'
✅️ Added "angular-auth-oidc-client" 18.0.2
🔍 Installing packages...
✅️ Installed
✅️ 'src/app/auth/auth-config.module.ts' will be created
✅️ 'AuthConfigModule' is imported in 'src/app/app.module.ts'
✅️ All imports done, please add the 'RouterModule' as well if you don't have it imported yet.
✅️ No silent-renew entry in assets array needed
✅️ No 'silent-renew.html' needed
CREATE src/app/auth/auth-config.module.ts (753 bytes)
UPDATE package.json (1311 bytes)
UPDATE src/app/app.module.ts (1153 bytes)
✔ Packages installed successfully.
Then edit src/app/auth/auth-config.module.ts to fill in the ClientId, Scopes, etc.
Note
Because this library implements OAuth2 best practices, there is no ClientSecret. Change the existing
OAuth2 AS setup to remove the secret if you currently have one.
Make the various routes visible only when logged in by adding
1
canActivate: [AutoLoginPartialRoutesGuard]
to each route.
Most importantly, make sure the OAuth2 token not only determines if routes are visible but is also
passed to the backend API via the Authorization header by adding something like this to
the top-level app.module. This glues the OIDC service and the generated API together.
import{ApiModule,Configuration,ConfigurationParameters}from'./core/api/v1';import{OidcSecurityService}from'angular-auth-oidc-client';// ...exportfunctionapiConfigFactory():Configuration{constoidcSecurityService=inject(OidcSecurityService);varconf:any=null;// I'm sure this is not the right way to do this.oidcSecurityService.getAccessToken().subscribe((token)=>{conf=newConfiguration({basePath:"http://localhost:8000",credentials:{'oauth2':token}});});returnconf;}@NgModule({declarations:[AppComponent,// ...],imports:[CommonModule,BrowserModule,AppRoutingModule,ApiModule.forRoot(apiConfigFactory),HttpClientModule,AuthConfigModule,],// ...})