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

Can't intercept http requests since v3.0.0-dev #336

Open
DeinChristian opened this issue Nov 11, 2021 · 5 comments
Open

Can't intercept http requests since v3.0.0-dev #336

DeinChristian opened this issue Nov 11, 2021 · 5 comments

Comments

@DeinChristian
Copy link

DeinChristian commented Nov 11, 2021

We can't intercept requests in version 3.0.0-dev anymore (Angular). That works perfectly in 2.2.3.

example interceptor:

@Injectable({
  providedIn: 'root'
})
export class TokenInterceptor implements HttpInterceptor {

  constructor(private authService: OAuthService) {
  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    if (this.authService.hasValidAccessToken()) {
      request = request.clone({
        setHeaders: {
          Authorization: 'Bearer ' + this.authService.getAccessToken()
        }
      })
    }

    return next.handle(request)
  }

}
@joulewei
Copy link

Fix or riot!

@niskah-energies
Copy link

Same problem, any solution ??

@eduardo-r-web
Copy link

it happens me too

@niskah-energies
Copy link

Someone have any solution ??

@niskah-energies
Copy link

Found a solution.
The problem is that this library use axios http client and HttpInterceptor not work.

I use a Service derived class like this one and it works.

axios-authentication.service.ts

import { Injectable } from '@angular/core';
import { Resource, Service, } from 'ngx-jsonapi';
import axios from 'axios';

@Injectable({
  providedIn: 'root'
})
export abstract class AxiosAuthenticatedService<R extends Resource = Resource> extends Service<R> {

  constructor() {
    super();
    axios.interceptors.request.use(
      function (request) {
         request.headers = {
          'Authorization': 'Bearer ' + YOUR TOKEN HERE
        };
        console.debug('axios interceptor-- ', request);
        return request;
      }
    );
  }}

and use this one to your resources services like this:

import { Injectable } from '@angular/core';
import { Service, } from 'ngx-jsonapi';
import { User } from './users.resource';
import { AxiosAuthenticatedService } from '@app/core/services/axios-authentication/axios-authentication.service';

@Injectable({
    providedIn: 'root'
  })
export class UsersService extends AxiosAuthenticatedService<User> {
    public resource = User;
    public type = 'users';
    public collections_ttl = 1;
}

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

No branches or pull requests

4 participants