[](https://travis-ci.org/damienbod/angular-auth-oidc-client) [](https://www.npmjs.com/package/angul
npm install angular-auth-oidc-client-renew
package.json and type
typescript
npm install angular-auth-oidc-client
`
or with yarn
`typescript
yarn add angular-auth-oidc-client
`
Documentation
- Quickstart
- Samples
- Silent renew
- Guards
- Features
- Logout
- Using and revoking the access token
- CSP & CORS
- Public API
- Configuration
- Migration
- Changelog
Quickstart
> For the example of the Code Flow. For further examples please check the Samples Section
Import the module and services in your module.
`typescript
import { HttpClientModule } from '@angular/common/http';
import { APP_INITIALIZER, NgModule } from '@angular/core';
import { AuthModule, LogLevel, OidcConfigService } from 'angular-auth-oidc-client';
// ...
export function configureAuth(oidcConfigService: OidcConfigService) {
return () =>
oidcConfigService.withConfig({
stsServer: 'https://offeringsolutions-sts.azurewebsites.net',
redirectUrl: window.location.origin,
postLogoutRedirectUri: window.location.origin,
clientId: 'angularClient',
scope: 'openid profile email',
responseType: 'code',
silentRenew: true,
silentRenewUrl: ${window.location.origin}/silent-renew.html,
logLevel: LogLevel.Debug,
});
}
@NgModule({
// ...
imports: [
// ...
AuthModule.forRoot(),
],
providers: [
OidcConfigService,
{
provide: APP_INITIALIZER,
useFactory: configureAuth,
deps: [OidcConfigService],
multi: true,
},
],
// ...
})
export class AppModule {}
`
And call the method checkAuth() from your app.component.ts
`typescript
import { Component, OnDestroy, OnInit } from '@angular/core';
import { OidcClientNotification, OidcSecurityService, PublicConfiguration } from 'angular-auth-oidc-client';
import { Observable } from 'rxjs';
@Component({
/**/
})
export class AppComponent implements OnInit {
constructor(public oidcSecurityService: OidcSecurityService) {}
ngOnInit() {
this.oidcSecurityService.checkAuth().subscribe((auth) => console.log('is authenticated', auth));
}
login() {
this.oidcSecurityService.authorize();
}
logout() {
this.oidcSecurityService.logoff();
}
}
`
Using the access token
You can get the access token by calling the method getToken() on the OidcSecurityService
`typescript
const token = this.oidcSecurityService.getToken();
`
And then you can use it in the HttpHeaders
`typescript
import { HttpHeaders } from '@angular/common/http';
const token = this.oidcSecurityServices.getToken();
const httpOptions = {
headers: new HttpHeaders({
Authorization: 'Bearer ' + token,
}),
};
``