{"id":18261,"library":"d2l-fetch-auth","title":"d2l-fetch-auth","description":"Middleware function for wrapping a window.Request object with Brightspace (D2L) authentication for use with d2l-fetch. Current stable version is 1.9.1. This package is specifically designed for D2L/Brightspace ecosystems, providing seamless integration with their authentication system for fetch requests. It offers two variants: a standard auth middleware and a framed version for iFramed Free Range Applications (iFRA). Release cadence follows semantic-release, triggered by `fix:` and `feat:` prefixed commits. Key differentiators: handles D2L-specific auth tokens and headers, works with both top-level and iframed contexts.","status":"active","version":"1.9.1","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","fetch","middleware","auth"],"install":[{"cmd":"npm install d2l-fetch-auth","lang":"bash","label":"npm"},{"cmd":"yarn add d2l-fetch-auth","lang":"bash","label":"yarn"},{"cmd":"pnpm add d2l-fetch-auth","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This middleware is designed to be used with d2l-fetch; it intercepts requests and adds authentication.","package":"d2l-fetch","optional":false}],"imports":[{"note":"The module exports a default function. Named import will fail. For iFRAs, use 'd2l-fetch-auth/d2l-fetch-auth-framed.js'.","wrong":"import { auth } from 'd2l-fetch-auth';","symbol":"auth (default)","correct":"import auth from 'd2l-fetch-auth/d2l-fetch-auth.js';"},{"note":"If your app runs in an iframed context (iFRA), you must use the framed version.","wrong":"import auth from 'd2l-fetch-auth/d2l-fetch-auth.js';","symbol":"auth (default) for iFRA","correct":"import auth from 'd2l-fetch-auth/d2l-fetch-auth-framed.js';"},{"note":"CommonJS require is available for older environments; but ESM is recommended.","wrong":null,"symbol":"auth (CommonJS)","correct":"const auth = require('d2l-fetch-auth/d2l-fetch-auth.js');"}],"quickstart":{"code":"// Ensure d2l-fetch is loaded first\nimport d2lfetch from 'd2l-fetch';\nimport auth from 'd2l-fetch-auth/d2l-fetch-auth.js';\n\nd2lfetch.use({name: 'auth', fn: auth});\n\nconst response = await d2lfetch.fetch(\n\tnew Request('https://api.example.com/entity/1')\n);\n\nconst data = await response.json();\nconsole.log(data);","lang":"typescript","description":"Shows how to import and use the auth middleware with d2l-fetch to make authenticated requests."},"warnings":[{"fix":"If your app runs in an iframe, use `'d2l-fetch-auth/d2l-fetch-auth-framed.js'`.","message":"Using wrong import path for iframed vs non-iframed context will cause authentication failures in iFRAs.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Call `d2lfetch.use({name: 'auth', fn: auth})` after importing.","message":"The auth middleware must be used via `d2lfetch.use()`; importing alone does not activate it.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Upgrade to version 1.0.0 or later.","message":"Older versions may not support ES module imports; check your d2l-fetch version compatibility.","severity":"deprecated","affected_versions":"<1.0.0"},{"fix":"Ensure d2l-fetch is installed and imported before using d2l-fetch-auth.","message":"If d2l-fetch is not initialized or loaded, the middleware will not function.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Update HTML script tags to use `type=\"module\"` and the new import paths.","message":"Version 1.0.0 changed the import path from legacy scripts to ES modules. Old script tags may break.","severity":"breaking","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use `import auth from 'd2l-fetch-auth/d2l-fetch-auth.js';`","cause":"Trying named import `{ auth }` instead of default import.","error":"Uncaught SyntaxError: The requested module 'd2l-fetch-auth/d2l-fetch-auth.js' does not provide an export named 'auth'"},{"fix":"Use the full path ending with '.js': `import auth from 'd2l-fetch-auth/d2l-fetch-auth.js';`","cause":"Import path point to a directory instead of the .js file, or the server is misconfigured.","error":"Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of \"text/html\""},{"fix":"Import d2l-fetch first: `import d2lfetch from 'd2l-fetch';`","cause":"d2l-fetch is not imported or initialized before calling `d2lfetch.use`.","error":"TypeError: Cannot read properties of undefined (reading 'use')"},{"fix":"Call `d2lfetch.use({name: 'auth', fn: auth});` after importing the correct auth middleware for your context.","cause":"Auth middleware not installed or using wrong variant (non-framed vs framed).","error":"401 Unauthorized when making requests with d2l-fetch"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}