@alphaz/interceptor
v0.0.2
Published
Angular interceptor
Downloads
115
Readme
Interceptor
Usage Bearer auth interceptor
- Create Storage Service implements
IStorage
for example:
@Injectable({
providedIn: 'root',
})
export class StorageService implements IStorage {
constructor() {}
setToken(token: string): void {
localStorage.setItem('token', token);
}
getToken(): string | null {
return localStorage.getItem('token');
}
}
- in app.config.ts in providers section add this
providers: [
...
provideInterceptor(new StorageService(), <ACCESS_TOKEN_KEY>),
provideHttpClient(withInterceptors([withAuthorizationHeader])),
...
]
where new StorageService()
- service for token
<ACCESS_TOKEN_KEY>
- string key for access token property in response object
OR
providers: [
...
{
provide: STORAGE_TOKEN,
useFactory: (service: StorageService) => service,
deps: [StorageService],
},
{
provide: ACCESS_TOKEN_KEY_NAME,
useValue: <ACCESS_TOKEN_KEY>,
},
...
]