Superagent D2L Session Authentication
superagent-d2l-session-auth is a JavaScript plugin designed to integrate D2L (Brightspace) session authentication with `superagent` HTTP requests. It acts as a `superagent` middleware, automatically adding the necessary D2L authentication headers to outbound requests by leveraging the `frau-jwt` library internally. The current stable version is 2.0.1, which includes a fix for `trustedHost` casing sensitivity. While specific release cadence is not explicitly stated, the project appears to be actively maintained by Brightspace, with updates addressing functionality and compatibility. Its primary differentiator is its specialized function within the Brightspace ecosystem, providing a streamlined way to handle D2L session-based authentication for applications using `superagent`, particularly within iframed contexts.
Common errors
-
Error: Cannot find module 'superagent'
cause `superagent` is a peer dependency and was not installed in the project.fixRun `npm install superagent` or `yarn add superagent` to add `superagent` to your project's dependencies. -
TypeError: auth is not a function
cause The main export of `superagent-d2l-session-auth` is a factory function that must be called to produce the actual plugin.fixCall the imported `auth` function to get the plugin instance: `const auth = require('superagent-d2l-session-auth')()` or `import createAuth from 'superagent-d2l-session-auth'; const auth = createAuth();`. -
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an ECMAScript Module (ESM) environment (e.g., Node.js with `type: module` or a browser).fixUse ESM `import` syntax: `import createAuth from 'superagent-d2l-session-auth'; const auth = createAuth();`. -
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ESM `import` syntax in a CommonJS environment (e.g., Node.js without `type: module` in `package.json`).fixUse CommonJS `require()` syntax: `const auth = require('superagent-d2l-session-auth')();`.
Warnings
- breaking Starting from version `0.12.0`, `superagent` was changed to a peer dependency. Users must explicitly install `superagent` in their project, as it is no longer bundled.
- deprecated The use of the `X-D2L-App-Id` header for authentication was deprecated starting from version `0.1.0`. Relying on this header may lead to authentication failures in newer D2L environments.
- gotcha Prior to version `1.0.1`, the `trustedHost` option in the plugin configuration was case-sensitive. Incorrect casing could prevent tokens from being sent to the specified host.
- gotcha The library primarily uses and demonstrates CommonJS `require()` syntax. While bundlers can handle ESM `import` in many environments, direct `import` might fail in older Node.js versions or certain browser contexts without proper module configuration.
Install
-
npm install superagent-d2l-session-auth -
yarn add superagent-d2l-session-auth -
pnpm add superagent-d2l-session-auth
Imports
- auth
const auth = require('superagent-d2l-session-auth')import auth from 'superagent-d2l-session-auth'
- authFactory
const auth = require('superagent-d2l-session-auth')const auth = require('superagent-d2l-session-auth')() - framedAuthFactory
const auth = require('superagent-d2l-session-auth/framed')const auth = require('superagent-d2l-session-auth/framed')()
Quickstart
const request = require('superagent');
const auth = require('superagent-d2l-session-auth')({
scope: '*:*:*',
trustedHost: 'school.brightspace.com' // Replace with your D2L domain
});
// Example using a placeholder D2L API endpoint
request
.get('https://school.brightspace.com/d2l/api/lp/1.5/users/whoami')
.use(auth)
.end(function(err, res) {
if(err) {
console.error('Failed to fetch user info: ' + err.status + ' ' + (err.response ? err.response.text : ''));
return;
}
const user = res.body;
console.log('Hello, ' + user.FirstName + ' ' + user.LastName);
});