node-weixin-auth

raw JSON →
0.6.3 verified Sat Apr 25 auth: no javascript

WeChat server authentication module for node-weixin-api and node-weixin-express. Version 0.6.3 provides token acquisition, automatic token refresh with configurable GAP, server IP retrieval, and message signature verification. It is part of the node-weixin ecosystem and requires node-weixin-settings for configuration persistence. Compared to other WeChat SDKs, it offers manual and automatic tokenization with a time gap to reduce expiration failures. The package has a moderate release cadence and is primarily used in Chinese development communities.

error Cannot read property 'access_token' of undefined
cause tokenize callback called with error but code accesses json.access_token without check.
fix
Check if (!error && json) before using json.access_token.
error nodeWeixinAuth is not a function
cause Importing incorrectly, e.g., import nodeWeixinAuth from 'node-weixin-auth' when require is expected or module has no default export.
fix
Use const nodeWeixinAuth = require('node-weixin-auth');.
error TypeError: settings.get is not a function
cause The settings object must be a node-weixin-settings instance, not a plain object.
fix
Create settings with require('node-weixin-settings') and configure it.
breaking The callback signature for determine may differ from tokenize; ensure you pass a function with no arguments.
fix Use determine(settings, app, function() { ... }) without parameters in the callback.
gotcha The TIME_GAP property must be set before calling determine; otherwise default 500 seconds is used.
fix Set nodeWeixinAuth.TIME_GAP = <desired seconds> before calling determine.
gotcha The ack method expects a data object from extract; ensure you call nodeWeixinAuth.extract(req.query) on GET request query parameters.
fix Parse query string with extract before passing to ack.
gotcha If settings are not properly configured, tokenize may throw an error without clear message; validate app object has non-empty id and secret.
fix Check that app.id, app.secret, and app.token are set to valid WeChat credentials.
npm install node-weixin-auth
yarn add node-weixin-auth
pnpm add node-weixin-auth

Initializes WeChat auth with app credentials, manually fetches a token, and sets up automatic token refresh.

const nodeWeixinAuth = require('node-weixin-auth');
const settings = require('node-weixin-settings');

const app = {
  id: process.env.WECHAT_APP_ID || '',
  secret: process.env.WECHAT_APP_SECRET || '',
  token: process.env.WECHAT_TOKEN || ''
};

// Optional: set time gap for token refresh
nodeWeixinAuth.TIME_GAP = 60;

// Manually get access token
nodeWeixinAuth.tokenize(settings, app, (error, json) => {
  if (!error) {
    console.log('Access Token:', json.access_token);
  }
});

// Automatically handle token for subsequent requests
nodeWeixinAuth.determine(settings, app, () => {
  console.log('Token is ready. You can now make API calls.');
});