Simple OAuth2 is a robust Node.js client library that provides a straightforward interface for interacting with OAuth 2.0 authorization servers. It supports various standard grant types including Authorization Code, Resource Owner Password Credentials, and Client Credentials, making it adaptable for diverse application architectures from web services to CLI tools. The package is currently stable at version 5.1.0, which mandates Node.js 14.x or higher, with a development branch (6.x) targeting Node.js 16.x and above. While a strict release cadence is not published, active development and maintenance are evident. Its primary differentiator lies in simplifying complex OAuth2 flows into an easy-to-use, promise-based API specifically for Node.js environments.
npm install simple-oauth2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the Authorization Code grant type, covering redirecting a user for authorization, handling the callback, exchanging the authorization code for an access token, and refreshing an expired token. It uses environment variables for sensitive configuration and basic error handling.
Update import statements and instantiation patterns. Instead of `const oauth2 = require('simple-oauth2').create(config);`, use `const { AuthorizationCode } = require('simple-oauth2'); const client = new AuthorizationCode(config);` or `import { AuthorizationCode } from 'simple-oauth2'; const client = new AuthorizationCode(config);`.Ensure your configuration object includes `auth: { tokenHost: 'https://your-oauth-server.com' }`. This was optional or handled differently in earlier versions.Upgrade your Node.js runtime to at least version 14.x. For future compatibility, consider Node.js 16.x or later.
Generate a unique, cryptographically secure random string for the `state` parameter when initiating the authorization flow, store it (e.g., in a session), and verify it upon callback before exchanging the code for a token.
Use the `accessToken.expired()` helper and `accessToken.refresh()` method to programmatically refresh tokens. Ensure your configuration allows for refresh tokens if needed.
Change your import to use named exports and instantiate the grant type directly: `import { AuthorizationCode } from 'simple-oauth2'; const client = new AuthorizationCode(config);`Add `tokenHost: 'https://your-oauth-server.com'` to the `auth` section of your configuration object.
Double-check your `client.id` and `client.secret` against your OAuth2 provider's registration details. Ensure they are correctly set in your `config` object or environment variables.
Refresh tokens can be single-use or expire. If a refresh fails, the user needs to re-authenticate through the full OAuth2 flow. Check your OAuth2 provider's policy on refresh token validity.
No dependency data recorded yet.