用户登录应用后如何设置令牌?

问题描述

我对 Cube.JS 有问题:

一个非常常见的用例是仅在用户通过我的应用程序的身份验证后才请求 CubeJS 令牌。然后我可以提供一个令牌初始化CubeJS。

按照文档 (https://cube.dev/docs/@cubejs-client-ngx#api) 的方式,您在 app.module 上设置了配置,因此,在用户有机会登录应用程序之前。

在我知道用户是谁之后(在他对我的应用程序进行身份验证之后),我将如何设置令牌?

解决方法

如果我理解你,你只需要一个像这样的拦截器:

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);
}