1

I have a problem with Cube.JS:

A very common use case is to only request the CubeJS token once the user is authenticated to my app. Then I can provide a token initialize CubeJS.

The way it is on the docs (https://cube.dev/docs/@cubejs-client-ngx#api), you set the config on the app.module, hence, before the user has a chance to login to the app.

How would I set the token AFTER I know who the user is (after he has authenticated to my app)?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
HobojoeBr
  • 599
  • 9
  • 23

1 Answers1

1

If i understand you, you simply need an interceptor like this:

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

    if (!req.url.toLowerCase().endsWith('/login')
        && req.url.toLowerCase().indexOf('/authenticate') <= 0
        && req.url.toLowerCase().indexOf('/i18n/') <= 0) {
        // Get the auth header from the service.
        const authHeader = this.tokenService.getBearerToken();
        const authReq = req.clone({
             headers: new HttpHeaders({
            'Content-Type':  'application/json',
            'Authorization': authHeader,
            'Accept': '*'
          })}
        );
        return next.handle(authReq);
    }

    return next.handle(req);
}
RealSeik
  • 147
  • 1
  • 8
  • The CubeJS library probably doesn't use HttpClient, hence it doesn't get caught by the interceptor :( but thanks for you help! – HobojoeBr Feb 04 '21 at 17:29