zova-module-a-interceptor 5.1.29 → 5.1.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/bean/interceptor.jwt.ts +1 -1
package/dist/index.js
CHANGED
|
@@ -70,7 +70,7 @@ var InterceptorJwt = (_dec$3 = Interceptor({ dependencies: "a-interceptor:header
|
|
|
70
70
|
}
|
|
71
71
|
async prepareAccessToken(config, authToken) {
|
|
72
72
|
if (!this.sys.config.api.jwt) return;
|
|
73
|
-
if (this
|
|
73
|
+
if (this.$ssr.cookieDisabledOnServer) return;
|
|
74
74
|
const authTokenCurrent = authToken ?? this.scope.config.authToken.default;
|
|
75
75
|
if (process.env.SERVER) config.headers[$protocolKey("x-vona-jwt-authtoken")] = typeof authTokenCurrent === "string" ? true : authTokenCurrent;
|
|
76
76
|
if (typeof authTokenCurrent === "string") return authTokenCurrent;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/bean/interceptor.body.ts","../src/bean/interceptor.headers.ts","../src/bean/interceptor.jwt.ts","../src/bean/interceptor.mock.ts","../src/bean/interceptor.performAction.ts","../src/config/config.ts","../src/.metadata/index.ts"],"sourcesContent":["var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\nimport { cast } from 'zova';\nimport { BeanInterceptorBase, Interceptor, SymbolInterceptorBodyResponseFlag } from 'zova-module-a-fetch';\nexport let InterceptorBody = (_dec = Interceptor({\n dependencies: 'a-interceptor:performAction'\n}), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class InterceptorBody extends BeanInterceptorBase {\n async onResponse(response, _options, next) {\n response = await next();\n const contentType = response.headers['content-type'];\n if (!contentType || typeof contentType !== 'string' || !contentType.includes('application/json')) {\n response[SymbolInterceptorBodyResponseFlag] = true;\n return response;\n }\n if (response.data.code !== 0) {\n const error = new Error();\n error.code = response.data.code;\n error.message = response.data.message;\n throw error;\n }\n // return data\n return response.data.data ?? null;\n }\n async onResponseError(error, _options, next) {\n error = await next();\n if (!(error instanceof Error)) return error;\n if (error.response) {\n error.code = cast(error.response.data)?.code ?? error.response.status;\n error.message = cast(error.response.data)?.message ?? error.response.statusText;\n }\n return error;\n }\n}) || _class) || _class);","var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\nimport { $protocolKey } from 'zova';\nimport { BeanInterceptorBase, Interceptor } from 'zova-module-a-fetch';\nexport let InterceptorHeaders = (_dec = Interceptor({\n dependencies: 'a-interceptor:mock'\n}), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class InterceptorHeaders extends BeanInterceptorBase {\n async onRequest(config, options, next) {\n // locale\n const keyLocale = this.sys.env.APP_LOCALE_HEADER_KEY;\n if (keyLocale && !config.headers[keyLocale]) {\n config.headers[keyLocale] = this.app.meta.locale.current;\n }\n // tz\n const keyTz = this.sys.env.APP_TZ_HEADER_KEY;\n if (keyTz && !config.headers[keyTz]) {\n config.headers[keyTz] = this.app.meta.locale.tz;\n }\n // openapi schema\n if (options.openapiSchema) {\n config.headers[$protocolKey('x-vona-openapi-schema')] = true;\n }\n // next\n return next(config);\n }\n}) || _class) || _class);","var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\nimport { $protocolKey } from 'zova';\nimport { BeanInterceptorBase, Interceptor } from 'zova-module-a-fetch';\nexport let InterceptorJwt = (_dec = Interceptor({\n dependencies: 'a-interceptor:headers'\n}), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class InterceptorJwt extends BeanInterceptorBase {\n constructor(...args) {\n super(...args);\n this._beanJwtAdapter = void 0;\n this._refreshAuthTokenPromise = void 0;\n }\n async __init__(_beanFetch, options) {\n const jwtAdapter = options.jwtAdapter || this.scope.config.jwtAdapter;\n const beanFullName = jwtAdapter.replace(':', '.service.');\n // singleton\n this._beanJwtAdapter = await this.app.bean._getBean(beanFullName, true);\n }\n async onRequest(config, options, next) {\n try {\n const accessToken = await this.prepareAccessToken(config, options.authToken);\n if (accessToken) {\n config.headers.Authorization = `Bearer ${accessToken}`;\n }\n } catch (error) {\n error.config = config;\n error.request = undefined;\n error.response = undefined;\n throw error;\n }\n return next(config);\n }\n async prepareAccessToken(config, authToken) {\n if (!this.sys.config.api.jwt) return;\n if (this.sys.config.ssr.cookieDisabledOnServer) return;\n // use default in scope.config rather than IInterceptorOptionsJwt.options\n const authTokenCurrent = authToken ?? this.scope.config.authToken.default;\n // authToken = authToken ?? this.scope.config.authToken.default;\n if (process.env.SERVER) {\n config.headers[$protocolKey('x-vona-jwt-authtoken')] = typeof authTokenCurrent === 'string' ? true : authTokenCurrent;\n }\n // // authToken: false\n // if (authTokenCurrent === false) return;\n // authToken: string\n if (typeof authTokenCurrent === 'string') return authTokenCurrent;\n // if (typeof authToken === 'string') return authToken;\n // authToken: true\n let jwtInfo = await this._beanJwtAdapter.getJwtInfo();\n if (!jwtInfo) {\n if (authToken === true) {\n this.app.throw(401); // 401 rather than 403\n }\n return;\n }\n // accessToken\n if (process.env.SERVER || !jwtInfo.expireTime || jwtInfo.expireTime > Date.now()) {\n if (!jwtInfo.accessToken) {\n if (authToken === true) this.app.throw(401);\n return;\n }\n return jwtInfo.accessToken;\n }\n // refreshToken\n if (!jwtInfo.refreshToken) {\n if (authToken === true) this.app.throw(401);\n return;\n } else {\n if (authToken === false) {\n // need not refreshAuthToken, such as captcha.create\n return;\n }\n }\n jwtInfo = await this._refreshAuthToken(jwtInfo.refreshToken);\n return jwtInfo.accessToken;\n }\n async _refreshAuthToken(refreshToken) {\n if (!this._refreshAuthTokenPromise) {\n this._refreshAuthTokenPromise = this._refreshAuthTokenInner(refreshToken);\n }\n return await this._refreshAuthTokenPromise;\n }\n async _refreshAuthTokenInner(refreshToken) {\n try {\n return await this._beanJwtAdapter.refreshAuthToken(refreshToken);\n } finally {\n this._refreshAuthTokenPromise = undefined;\n }\n }\n}) || _class) || _class);","var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\nimport { BeanInterceptorBase, Interceptor, SymbolInterceptorBodyResponseFlag } from 'zova-module-a-fetch';\n// not include: ERR_BAD_RESPONSE\nconst __ErrorsShouldBeMocked = ['ECONNREFUSED', 'ERR_NETWORK', '404'];\nexport let InterceptorMock = (_dec = Interceptor(), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class InterceptorMock extends BeanInterceptorBase {\n async onResponseError(error, _options, next) {\n if (!(error instanceof Error)) return next();\n if (this.sys.env.MOCK_ENABLED === 'true') {\n if (process.env.DEV || process.env.PROD && this.sys.env.MOCK_BUILD === 'true') {\n if (__ErrorsShouldBeMocked.includes(String(error.code)) || error.status === 404) {\n const config = error.config;\n if (config.baseURL) {\n const port = process.env.DEV ? this.sys.env.DEV_SERVER_PORT : this.sys.env.MOCK_BUILD_PORT;\n let baseURL = `http://localhost:${port}`;\n if (config.baseURL.endsWith(this.sys.env.API_PREFIX)) {\n baseURL = `${baseURL}${this.sys.env.API_PREFIX}`;\n }\n if (config.baseURL !== baseURL && config.baseURL !== this.sys.env.API_PREFIX) {\n const response = await this.$fetch.request(Object.assign({}, config, {\n baseURL\n }));\n if (response && response[SymbolInterceptorBodyResponseFlag]) {\n return response.data;\n } else {\n return response;\n }\n }\n }\n }\n }\n }\n return next();\n }\n}) || _class) || _class);","var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\nimport { catchError } from '@cabloy/utils';\nimport { cast } from 'zova';\nimport { BeanInterceptorBase, Interceptor } from 'zova-module-a-fetch';\nexport let InterceptorPerformAction = (_dec = Interceptor({\n dependencies: 'a-interceptor:jwt'\n}), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class InterceptorPerformAction extends BeanInterceptorBase {\n async onRequest(config, _options, next) {\n if (process.env.CLIENT) return next();\n const performAction = this.ctx.meta.$ssr.getPerformAction(config.baseURL);\n if (!performAction) return next();\n const data = {\n method: config.method,\n path: config.url,\n query: config.params,\n body: config.data,\n headers: config.headers\n };\n const [result, error] = await catchError(() => {\n return performAction(data);\n });\n if (error) {\n cast(error).config = config;\n throw error;\n }\n throw result;\n }\n}) || _class) || _class);","export const config = _sys => {\n return {\n jwtAdapter: 'home-api:jwtAdapter',\n authToken: {\n default: true\n }\n };\n};","var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\n// eslint-disable\n/** interceptor: begin */\nexport * from '../bean/interceptor.body.js';\nexport * from '../bean/interceptor.headers.js';\nexport * from '../bean/interceptor.jwt.js';\nexport * from '../bean/interceptor.mock.js';\nexport * from '../bean/interceptor.performAction.js';\nimport 'zova-module-a-fetch';\n/** interceptor: end */\n/** interceptor: begin */\nimport { InterceptorBody } from '../bean/interceptor.body.js';\nimport { InterceptorHeaders } from '../bean/interceptor.headers.js';\nimport { InterceptorJwt } from '../bean/interceptor.jwt.js';\nimport { InterceptorMock } from '../bean/interceptor.mock.js';\nimport { InterceptorPerformAction } from '../bean/interceptor.performAction.js';\nimport 'zova';\n/** interceptor: end */\n/** config: begin */\nexport * from '../config/config.js';\n/** config: end */\n/** scope: begin */\nimport { BeanScopeBase } from 'zova';\nimport { Scope } from 'zova-module-a-bean';\nexport let ScopeModuleAInterceptor = (_dec = Scope(), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class ScopeModuleAInterceptor extends BeanScopeBase {}) || _class) || _class);\nimport 'zova';\n\n/** scope: end */"],"mappings":";;;;;AAAA,IAAI,QAAM,SAAO;AAIjB,IAAW,mBAAmB,SAAO,YAAY,EAC/C,cAAc,8BAChB,CAAC,GAAG,UAAQ,SAAa,EACvB,QAAQ,gBAC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,SAAQ;CAC5B,OAAO;EACL,YAAY;EACZ,WAAW,EACT,SAAS,KACX;CACF;AACF;;;;;ACPA,IAAI,MAAM,OAAO;AAyBjB,IAAW,2BAA2B,OAAO,MAAM,GAAG,QAAQ,SAAa,EACzE,QAAQ,gBACV,CAAC,GAAG,KAAK,SAAS,MAAM,SAAS,MAAM,gCAAgC,cAAc,CAAC,CAAC,KAAK,MAAM,KAAK"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/bean/interceptor.body.ts","../src/bean/interceptor.headers.ts","../src/bean/interceptor.jwt.ts","../src/bean/interceptor.mock.ts","../src/bean/interceptor.performAction.ts","../src/config/config.ts","../src/.metadata/index.ts"],"sourcesContent":["var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\nimport { cast } from 'zova';\nimport { BeanInterceptorBase, Interceptor, SymbolInterceptorBodyResponseFlag } from 'zova-module-a-fetch';\nexport let InterceptorBody = (_dec = Interceptor({\n dependencies: 'a-interceptor:performAction'\n}), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class InterceptorBody extends BeanInterceptorBase {\n async onResponse(response, _options, next) {\n response = await next();\n const contentType = response.headers['content-type'];\n if (!contentType || typeof contentType !== 'string' || !contentType.includes('application/json')) {\n response[SymbolInterceptorBodyResponseFlag] = true;\n return response;\n }\n if (response.data.code !== 0) {\n const error = new Error();\n error.code = response.data.code;\n error.message = response.data.message;\n throw error;\n }\n // return data\n return response.data.data ?? null;\n }\n async onResponseError(error, _options, next) {\n error = await next();\n if (!(error instanceof Error)) return error;\n if (error.response) {\n error.code = cast(error.response.data)?.code ?? error.response.status;\n error.message = cast(error.response.data)?.message ?? error.response.statusText;\n }\n return error;\n }\n}) || _class) || _class);","var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\nimport { $protocolKey } from 'zova';\nimport { BeanInterceptorBase, Interceptor } from 'zova-module-a-fetch';\nexport let InterceptorHeaders = (_dec = Interceptor({\n dependencies: 'a-interceptor:mock'\n}), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class InterceptorHeaders extends BeanInterceptorBase {\n async onRequest(config, options, next) {\n // locale\n const keyLocale = this.sys.env.APP_LOCALE_HEADER_KEY;\n if (keyLocale && !config.headers[keyLocale]) {\n config.headers[keyLocale] = this.app.meta.locale.current;\n }\n // tz\n const keyTz = this.sys.env.APP_TZ_HEADER_KEY;\n if (keyTz && !config.headers[keyTz]) {\n config.headers[keyTz] = this.app.meta.locale.tz;\n }\n // openapi schema\n if (options.openapiSchema) {\n config.headers[$protocolKey('x-vona-openapi-schema')] = true;\n }\n // next\n return next(config);\n }\n}) || _class) || _class);","var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\nimport { $protocolKey } from 'zova';\nimport { BeanInterceptorBase, Interceptor } from 'zova-module-a-fetch';\nexport let InterceptorJwt = (_dec = Interceptor({\n dependencies: 'a-interceptor:headers'\n}), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class InterceptorJwt extends BeanInterceptorBase {\n constructor(...args) {\n super(...args);\n this._beanJwtAdapter = void 0;\n this._refreshAuthTokenPromise = void 0;\n }\n async __init__(_beanFetch, options) {\n const jwtAdapter = options.jwtAdapter || this.scope.config.jwtAdapter;\n const beanFullName = jwtAdapter.replace(':', '.service.');\n // singleton\n this._beanJwtAdapter = await this.app.bean._getBean(beanFullName, true);\n }\n async onRequest(config, options, next) {\n try {\n const accessToken = await this.prepareAccessToken(config, options.authToken);\n if (accessToken) {\n config.headers.Authorization = `Bearer ${accessToken}`;\n }\n } catch (error) {\n error.config = config;\n error.request = undefined;\n error.response = undefined;\n throw error;\n }\n return next(config);\n }\n async prepareAccessToken(config, authToken) {\n if (!this.sys.config.api.jwt) return;\n if (this.$ssr.cookieDisabledOnServer) return;\n // use default in scope.config rather than IInterceptorOptionsJwt.options\n const authTokenCurrent = authToken ?? this.scope.config.authToken.default;\n // authToken = authToken ?? this.scope.config.authToken.default;\n if (process.env.SERVER) {\n config.headers[$protocolKey('x-vona-jwt-authtoken')] = typeof authTokenCurrent === 'string' ? true : authTokenCurrent;\n }\n // // authToken: false\n // if (authTokenCurrent === false) return;\n // authToken: string\n if (typeof authTokenCurrent === 'string') return authTokenCurrent;\n // if (typeof authToken === 'string') return authToken;\n // authToken: true\n let jwtInfo = await this._beanJwtAdapter.getJwtInfo();\n if (!jwtInfo) {\n if (authToken === true) {\n this.app.throw(401); // 401 rather than 403\n }\n return;\n }\n // accessToken\n if (process.env.SERVER || !jwtInfo.expireTime || jwtInfo.expireTime > Date.now()) {\n if (!jwtInfo.accessToken) {\n if (authToken === true) this.app.throw(401);\n return;\n }\n return jwtInfo.accessToken;\n }\n // refreshToken\n if (!jwtInfo.refreshToken) {\n if (authToken === true) this.app.throw(401);\n return;\n } else {\n if (authToken === false) {\n // need not refreshAuthToken, such as captcha.create\n return;\n }\n }\n jwtInfo = await this._refreshAuthToken(jwtInfo.refreshToken);\n return jwtInfo.accessToken;\n }\n async _refreshAuthToken(refreshToken) {\n if (!this._refreshAuthTokenPromise) {\n this._refreshAuthTokenPromise = this._refreshAuthTokenInner(refreshToken);\n }\n return await this._refreshAuthTokenPromise;\n }\n async _refreshAuthTokenInner(refreshToken) {\n try {\n return await this._beanJwtAdapter.refreshAuthToken(refreshToken);\n } finally {\n this._refreshAuthTokenPromise = undefined;\n }\n }\n}) || _class) || _class);","var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\nimport { BeanInterceptorBase, Interceptor, SymbolInterceptorBodyResponseFlag } from 'zova-module-a-fetch';\n// not include: ERR_BAD_RESPONSE\nconst __ErrorsShouldBeMocked = ['ECONNREFUSED', 'ERR_NETWORK', '404'];\nexport let InterceptorMock = (_dec = Interceptor(), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class InterceptorMock extends BeanInterceptorBase {\n async onResponseError(error, _options, next) {\n if (!(error instanceof Error)) return next();\n if (this.sys.env.MOCK_ENABLED === 'true') {\n if (process.env.DEV || process.env.PROD && this.sys.env.MOCK_BUILD === 'true') {\n if (__ErrorsShouldBeMocked.includes(String(error.code)) || error.status === 404) {\n const config = error.config;\n if (config.baseURL) {\n const port = process.env.DEV ? this.sys.env.DEV_SERVER_PORT : this.sys.env.MOCK_BUILD_PORT;\n let baseURL = `http://localhost:${port}`;\n if (config.baseURL.endsWith(this.sys.env.API_PREFIX)) {\n baseURL = `${baseURL}${this.sys.env.API_PREFIX}`;\n }\n if (config.baseURL !== baseURL && config.baseURL !== this.sys.env.API_PREFIX) {\n const response = await this.$fetch.request(Object.assign({}, config, {\n baseURL\n }));\n if (response && response[SymbolInterceptorBodyResponseFlag]) {\n return response.data;\n } else {\n return response;\n }\n }\n }\n }\n }\n }\n return next();\n }\n}) || _class) || _class);","var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\nimport { catchError } from '@cabloy/utils';\nimport { cast } from 'zova';\nimport { BeanInterceptorBase, Interceptor } from 'zova-module-a-fetch';\nexport let InterceptorPerformAction = (_dec = Interceptor({\n dependencies: 'a-interceptor:jwt'\n}), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class InterceptorPerformAction extends BeanInterceptorBase {\n async onRequest(config, _options, next) {\n if (process.env.CLIENT) return next();\n const performAction = this.ctx.meta.$ssr.getPerformAction(config.baseURL);\n if (!performAction) return next();\n const data = {\n method: config.method,\n path: config.url,\n query: config.params,\n body: config.data,\n headers: config.headers\n };\n const [result, error] = await catchError(() => {\n return performAction(data);\n });\n if (error) {\n cast(error).config = config;\n throw error;\n }\n throw result;\n }\n}) || _class) || _class);","export const config = _sys => {\n return {\n jwtAdapter: 'home-api:jwtAdapter',\n authToken: {\n default: true\n }\n };\n};","var _dec, _dec2, _class;\nimport { \"BeanInfo\" as __z_BeanInfo } from \"zova\";\n// eslint-disable\n/** interceptor: begin */\nexport * from '../bean/interceptor.body.js';\nexport * from '../bean/interceptor.headers.js';\nexport * from '../bean/interceptor.jwt.js';\nexport * from '../bean/interceptor.mock.js';\nexport * from '../bean/interceptor.performAction.js';\nimport 'zova-module-a-fetch';\n/** interceptor: end */\n/** interceptor: begin */\nimport { InterceptorBody } from '../bean/interceptor.body.js';\nimport { InterceptorHeaders } from '../bean/interceptor.headers.js';\nimport { InterceptorJwt } from '../bean/interceptor.jwt.js';\nimport { InterceptorMock } from '../bean/interceptor.mock.js';\nimport { InterceptorPerformAction } from '../bean/interceptor.performAction.js';\nimport 'zova';\n/** interceptor: end */\n/** config: begin */\nexport * from '../config/config.js';\n/** config: end */\n/** scope: begin */\nimport { BeanScopeBase } from 'zova';\nimport { Scope } from 'zova-module-a-bean';\nexport let ScopeModuleAInterceptor = (_dec = Scope(), _dec2 = __z_BeanInfo({\n module: \"a-interceptor\"\n}), _dec(_class = _dec2(_class = class ScopeModuleAInterceptor extends BeanScopeBase {}) || _class) || _class);\nimport 'zova';\n\n/** scope: end */"],"mappings":";;;;;AAAA,IAAI,QAAM,SAAO;AAIjB,IAAW,mBAAmB,SAAO,YAAY,EAC/C,cAAc,8BAChB,CAAC,GAAG,UAAQ,SAAa,EACvB,QAAQ,gBACV,CAAC,GAAG,OAAK,WAAS,QAAM,WAAS,MAAM,wBAAwB,oBAAoB;CACjF,MAAM,WAAW,UAAU,UAAU,MAAM;EACzC,WAAW,MAAM,KAAK;EACtB,MAAM,cAAc,SAAS,QAAQ;EACrC,IAAI,CAAC,eAAe,OAAO,gBAAgB,YAAY,CAAC,YAAY,SAAS,kBAAkB,GAAG;GAChG,SAAS,qCAAqC;GAC9C,OAAO;EACT;EACA,IAAI,SAAS,KAAK,SAAS,GAAG;GAC5B,MAAM,wBAAQ,IAAI,MAAM;GACxB,MAAM,OAAO,SAAS,KAAK;GAC3B,MAAM,UAAU,SAAS,KAAK;GAC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IAAI,YAAY,SAAS,oCACvB,OAAO,SAAS;YAEhB,OAAO;MAEX;KACF;IACF;;;EAGJ,OAAO,KAAK;CACd;AACF,CAAC,KAAK,QAAM,KAAK;;;ACpCjB,IAAI,QAAM,SAAO;AAKjB,IAAW,4BAA4B,SAAO,YAAY,EACxD,cAAc,oBAChB,CAAC,GAAG,UAAQ,SAAa,EACvB,QAAQ,gBACV,CAAC,GAAG,OAAK,WAAS,QAAM,WAAS,MAAM,iCAAiC,oBAAoB;CAC1F,MAAM,UAAU,QAAQ,UAAU,MAAM;EACtC,IAAI,QAAQ,IAAI,QAAQ,OAAO,KAAK;EACpC,MAAM,gBAAgB,KAAK,IAAI,KAAK,KAAK,iBAAiB,OAAO,OAAO;EACxE,IAAI,CAAC,eAAe,OAAO,KAAK;EAChC,MAAM,OAAO;GACX,QAAQ,OAAO;GACf,MAAM,OAAO;GACb,OAAO,OAAO;GACd,MAAM,OAAO;GACb,SAAS,OAAO;EAClB;EACA,MAAM,CAAC,QAAQ,SAAS,MAAM,iBAAiB;GAC7C,OAAO,cAAc,IAAI;EAC3B,CAAC;EACD,IAAI,OAAO;GACT,KAAK,KAAK,CAAC,CAAC,SAAS;GACrB,MAAM;EACR;EACA,MAAM;CACR;AACF,CAAC,KAAK,QAAM,KAAK;;;AC9BjB,IAAa,UAAS,SAAQ;CAC5B,OAAO;EACL,YAAY;EACZ,WAAW,EACT,SAAS,KACX;CACF;AACF;;;;;ACPA,IAAI,MAAM,OAAO;AAyBjB,IAAW,2BAA2B,OAAO,MAAM,GAAG,QAAQ,SAAa,EACzE,QAAQ,gBACV,CAAC,GAAG,KAAK,SAAS,MAAM,SAAS,MAAM,gCAAgC,cAAc,CAAC,CAAC,KAAK,MAAM,KAAK"}
|
package/package.json
CHANGED
|
@@ -55,7 +55,7 @@ export class InterceptorJwt
|
|
|
55
55
|
authToken: string | boolean | undefined,
|
|
56
56
|
): Promise<string | undefined> {
|
|
57
57
|
if (!this.sys.config.api.jwt) return;
|
|
58
|
-
if (this
|
|
58
|
+
if (this.$ssr.cookieDisabledOnServer) return;
|
|
59
59
|
// use default in scope.config rather than IInterceptorOptionsJwt.options
|
|
60
60
|
const authTokenCurrent = authToken ?? this.scope.config.authToken.default;
|
|
61
61
|
// authToken = authToken ?? this.scope.config.authToken.default;
|