While working on implementing an AuthGuard in Angular, I encountered the following Error:
Type 'typeof AuthServiceService' is not assignable to type '(request?: HttpRequest) => string | Promise'. Type 'typeof AuthServiceService' provides no match for the signature '(request?: HttpRequest): string
This snippet shows my auth-service.service.ts file:
@Injectable({
providedIn: 'root'
})
export class AuthServiceService {
constructor(
private http:HttpClient,
public jwtHelper: JwtHelperService) { }
login(data):Observable<any>{
return this.http.post(baseUrl + 'ApplicationUser/login' ,data)
}
handleError(error: HttpErrorResponse) {
return throwError(error);
}
public isAuthenticated(): boolean {
const token = localStorage.getItem('token');
return !this.jwtHelper.isTokenExpired(token);
}
}
And here is my AuthGuard Service implementation:
import { AuthServiceService } from 'src/services/auth/auth/auth-service.service';
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AuthGuardService implements CanActivate {
constructor(public auth: AuthServiceService, public router: Router) { }
canActivate(): boolean {
if (!this.auth.isAuthenticated()) {
this.router.navigate(['']);
return false;
}
return true;
}
}
The error arises on the line defining tokenGetter in JWT_Module_Options:
const JWT_Module_Options: JwtModuleOptions = {
config: {
tokenGetter: AuthServiceService
}
};
I'm seeking assistance in understanding what the response from AuthService should entail. Any guidance would be greatly appreciated.