Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Bearer token included with includeBearerTokenInterceptor #610

Open
relivvius opened this issue Jan 8, 2025 · 11 comments
Open

No Bearer token included with includeBearerTokenInterceptor #610

relivvius opened this issue Jan 8, 2025 · 11 comments
Labels
question This issue is a question.

Comments

@relivvius
Copy link

relivvius commented Jan 8, 2025

- [x] bug report -> please search for issues before submitting
- [ ] feature request

Versions.

19.0.2

Repro steps.

Create a configuration that looks like:

export const appConfig: ApplicationConfig = {
  providers: [
    provideKeycloak({
      config: environmentClient.keycloak,
      initOptions: {
        onLoad: 'check-sso',
        silentCheckSsoRedirectUri: `${window.location.origin}/assets/silent-check-sso.html`
      },
      features: [
        withAutoRefreshToken({
          onInactivityTimeout: 'logout',
          sessionTimeout: 60000
        })
      ],
      providers: [
        AutoRefreshTokenService,
        UserActivityService,
        {
          provide: INCLUDE_BEARER_TOKEN_INTERCEPTOR_CONFIG,
          useValue: [localhostCondition] as IncludeBearerTokenCondition[] // <-- Note that multiple conditions might be added.
        },
      ]
    }),
    provideHttpClient(withInterceptors([includeBearerTokenInterceptor])),
    provideAnimations(),
    provideRouter(appRoutes),
    provideStore(),
    provideStoreDevtools({ logOnly: !isDevMode() }),
  ]
}

The log given by the failure.

There is no bearer token included in the api call

GET /api/workspace/findAll HTTP/1.1
Accept: application/json, text/plain, /
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: en-GB,en
Connection: keep-alive
DNT: 1
Host: localhost:4200
Referer: http://localhost:4200/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
Sec-GPC: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
sec-ch-ua: "Brave";v="131", "Chromium";v="131", "Not_A Brand";v="24"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "macOS"

Desired functionality.

The Bearer token should be included in urls starting with http://localhost:4200

@mauriciovigolo
Copy link
Owner

Hi @relivvius,
I noticed that your localhostCondition value doesn’t seem to be present. Could you double-check if it’s defined correctly? Additionally, is the regex aligned with your intended functionality?

If you’re unsure, I recommend reviewing the example-standalone project. It includes a working implementation that might help clarify or resolve the issue. Let me know if you’d like further guidance!

@mauriciovigolo mauriciovigolo added the question This issue is a question. label Jan 8, 2025
@relivvius
Copy link
Author

relivvius commented Jan 9, 2025

Hi @mauriciovigolo thanks for your answer!

Here is the localhostCondition that I use:

const localhostCondition = createInterceptorCondition<IncludeBearerTokenCondition>({
  urlPattern: /^http:\/\/localhost:4200\/.*$/
});

@relivvius
Copy link
Author

relivvius commented Jan 9, 2025

@mauriciovigolo could it be the provideStore()? The service hitting the http server is injected into a function effect

export const loadWorkspaces = createEffect(
    (actions$ = inject(Actions), workspaceService = inject(WorkspaceService)) => {
        return actions$.pipe(
            ofType(WorkspacesActions.initWorkspaces),
            switchMap(() =>
                workspaceService.getAllWorkspaceItems().pipe(
                    map((workspaces) =>
                      WorkspacesActions.loadWorkspacesSuccess({ workspaces })),
                    catchError((error) => {
                        console.error('Error', error);
                        return of(WorkspacesActions.loadWorkspacesFailure({ error }));
                    })
                )
            )
        );
    },
    { functional: true }
);

@panospcm
Copy link

I am having same issue. Authentication Bearer token not added to request.

@w-barros
Copy link

w-barros commented Feb 2, 2025

Having the same issue.

@FloppyNotFound
Copy link

FloppyNotFound commented Feb 3, 2025

Seems like the urlPattern-Parsing is broken.
When I use

 urlPattern: /^(.*)?$/i,

everything works just fine.

Edit:
prabably a relative/absolute URL issue:
Image

@RDUser-dev
Copy link

I think @FloppyNotFound is right
We have the same problem and everything works with the suggested pattern

@m3astwood
Copy link

If I put a breakpoint at the function @FloppyNotFound highlights above, for me the url is only the path. So if I navigate to http://localhost:8080/bingo the url argument in the findMatchingCondition method is /bingo.

For my usecase this is fine, I can rewrite the regex, but is this the expected functionality ?

@FloppyNotFound
Copy link

@m3astwood that's exactly what I meant by "relative/absolute URL issue" ;) for me, the url is "graphql" instead of "http://localhost:4200/graphql" (as visible in the right block of the screenshot)

@m3astwood
Copy link

@FloppyNotFound ah yeah my bad missed that ! 🤦‍♂️

@relivvius
Copy link
Author

Seems like the urlPattern-Parsing is broken. When I use

 urlPattern: /^(.*)?$/i,

everything works just fine.

Edit: prabably a relative/absolute URL issue: Image

Thanks a lot @FloppyNotFound, with the mentioned urlPattern works for me also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question This issue is a question.
Projects
None yet
Development

No branches or pull requests

7 participants