@kne/fastify-oidc 0.1.18 → 0.1.19

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.
@@ -272,9 +272,31 @@ module.exports = async ({ fastify, options, identity, translator, jwks, getLocal
272
272
  }
273
273
  return globalThis.fetch(url, init);
274
274
  },
275
+ // 浏览器授权失败(如 interaction 过期)时不要吐 JSON;跳 SPA 回调页由前端引导重新登录。
276
+ // 纯 API 客户端(Accept 只要 JSON、不要 HTML)仍返回 JSON。
275
277
  renderError: async (ctx, out) => {
276
- ctx.type = 'json';
277
- ctx.body = out;
278
+ const accept = String(ctx.get('accept') || '');
279
+ const wantsJson = accept.includes('application/json') && !accept.includes('text/html');
280
+ if (wantsJson) {
281
+ ctx.type = 'json';
282
+ ctx.body = out;
283
+ return;
284
+ }
285
+ const params = new URLSearchParams();
286
+ if (out.error) {
287
+ params.set('error', out.error);
288
+ }
289
+ if (out.error_description) {
290
+ params.set('error_description', out.error_description);
291
+ }
292
+ if (out.state) {
293
+ params.set('state', out.state);
294
+ }
295
+ const path = options.errorRedirectPath || '/oidc-callback';
296
+ const target = new URL(path, `${runtime.issuerOrigin}/`);
297
+ target.search = params.toString();
298
+ ctx.status = 303;
299
+ ctx.redirect(target.toString());
278
300
  }
279
301
  };
280
302
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-oidc",
3
- "version": "0.1.18",
3
+ "version": "0.1.19",
4
4
  "description": "基于 oidc-provider 的 Fastify OIDC 认证插件,支持 standalone / central 多项目单点登录、租户与跨项目令牌",
5
5
  "main": "index.js",
6
6
  "scripts": {