First install the library:
npm install @auth0/angular-jwt
Then in app.module.ts file, add the codes below. Note that my app has an authentication class (AuthService) that manages all the authentication functionalities, including retrieving user JWT access token.
. . . import { JwtModule, JWT_OPTIONS } from '@auth0/angular-jwt'; import { AuthService } from './services/auth.service'; export function jwtOptionsFactory(authService: AuthService) { return { tokenGetter: () => { return authService.getUserAccessToken(); }, allowedDomains: ["localhost:5555"] // my local dev environment } } @NgModule({ imports: [ JwtModule.forRoot({ jwtOptionsProvider: { provide: JWT_OPTIONS, useFactory: jwtOptionsFactory, deps: [AuthService] } }), . . . ], . . . }) export class AppModule { }
Now all the HTTP requests will have 'Authorization: Bearer [TOKEN]' added in the headers.
No comments:
Post a Comment