symfony don’t fetch token in header

I’m figuring a problem using jwt. in fact, I use angular to consume an API made with Symfony 3.4 and it generates a token but when I send it I set header with interceptor like this.

import {Injectable} from '@angular/core';
import {HttpHandler, HttpInterceptor, HttpRequest} from '@angular/common/http';
import {LoginService} from '../../services/authServices/login.service';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

    constructor(private auth: LoginService) {}

    intercept(req: HttpRequest<any>, next: HttpHandler) {

        if (req.url.indexOf('/admin') !== -1) {
            const authToken: string = `Bearer ${this.auth.getJwtToken()}`;

            if (authToken) {
                console.log('sending token to perform API fetch...');

                const authReq = req.clone({ setHeaders: { 'Authorization': authToken, 'Content-Type': 'application/json'} });
                console.log(authReq);

                return next.handle(
                    authReq
                );
            }

        }

        return next.handle(req);
    }
}

but Symfony returns a response 401 unauthorized when I tried to fetch resources from a protected route it seems like Symfony doesn’t extract token as well

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *