1

In my Interceptor file, I am getting an error as :

ERROR in ./src/app/auth/auth.interceptor.js 8:0
Module parse failed: Unexpected character '@' (8:0)
You may need an appropriate loader to handle this file type.
| import { UserService } from "./../shared/user.service";
|
> @Injectable()
| export class AuthInterceptor implements HttpInterceptor {
|

How to fix this? here is my ts file:

import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { tap } from "rxjs/operatos";
import { Router } from "@angular/router";

import { UserService } from "./../shared/user.service";

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

    constructor(private userService:UserService, private router:Router){}

    intercept(req:HttpRequest<any>, next:HttpHandler){
        if(req.headers.get('noauth')){
            return next.handle(req.clone());
        }
        else{
            const clonedreq = req.clone({
                headers:req.headers.set('Authorization', 'Bearer ' + this.userService.getToken())
            })
            return next.handle(clonedreq).pipe(tap(event => {}, err => {
                if(err.error.auth === false ){
                    this.router.navigateByUrl('/login');
                }
            } ))
        }
    }
}

1 Answer 1

6

I don't see anything wrong, this syntax seems valid to me in Typescript. But you seem to be using that as javascript: change your file extension from js to ts and the message should disappear.

Sign up to request clarification or add additional context in comments.

3 Comments

Facing same issue could someone tell me how to resolve this. i don't have any webpack configuration so far. its just simple and straight forward Angular 7 app.
Open a new question with details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.