I'm new on SAP Spartacus storefront, and I'm trying to make a custom API call to check if CNPJ is valid on customer database. My backend is ready and needs to receive a Token on request header.
But I don't find on Spartacus documentation how to use a default token service or interceptor to make this call.
Anyone can help me? Thanks for your time, have a good day.
You can take a look at the Spartacus Session Management documentation.
AuthHttpHeaderService
more specifically the
isOccUrl
method so that it returns "true" for occ endpoints and
your external endpoints. It will look like this:[...]
@Injectable({
providedIn: 'root',
})
export class CustomAuthHttpHeaderService extends AuthHttpHeaderService {
constructor(
protected authService: AuthService,
protected authStorageService: AuthStorageService,
protected oAuthLibWrapperService: OAuthLibWrapperService,
protected routingService: RoutingService,
protected occEndpoints: OccEndpointsService,
protected globalMessageService: GlobalMessageService
) {
super(...);
}
protected isOccUrl(url: string): boolean {
return url.includes(this.occEndpoints.getBaseUrl()) || url === 'my-custom-url';
}
}
And for the injection:
{
provide: AuthHttpHeaderService,
useClass: CustomAuthHttpHeaderService
}
'cx-use-client-token'
in your request's header. If it's
there and the user is anonymous, the ClientTokenInterceptor
will
add the client token. It should be used as follow:[...]
let headers = new HttpHeaders({
'Content-Type': 'application/json',
});
headers = InterceptorUtil.createHeader(USE_CLIENT_TOKEN, true, headers);
// Your request
return this.http.post('/your-request-endpoints', { headers });