This is a simple Node.js/Express.js example demonstrating how to implement WordPress.com Connect (OAuth2 authentication) in your application.
- Node.js (v14 or higher recommended)
- npm (comes with Node.js)
- A WordPress.com Developer Application
-
Clone this repository (if you haven't already):
git clone https://github.com/Automattic/wpcom-connect-examples.git cd wpcom-connect-examples/express.js -
Register a new application at WordPress.com Developer Console:
- Set the Redirect URL to:
http://localhost:3000/connected - Note your Client ID and Client Secret.
- Set the Redirect URL to:
-
Configure your credentials:
- Open
config.jsin this folder. - Replace the
client_id,client_secret,login_url, andredirect_urlvalues with those from your app's settings. - Adjust
login_urlandredirect_urlif you use a different port or domain.
- Open
-
Install dependencies:
npm install
-
Start the server:
npm start
- This will serve the demo at http://localhost:3000
-
Test the demo:
- Open http://localhost:3000 in your browser.
- Click the "Connect with WordPress.com" button.
- Authorize the app and you should see a "Connected to WordPress.com!" message.
- This demo is for educational purposes. In a real application, you should securely store the access token and handle errors appropriately.
- If you change the port or domain, update
login_urlandredirect_urlinconfig.jsand in your WordPress.com app settings. - You can set a custom session secret by defining the
SESSION_SECRETenvironment variable. - For more details, see the official documentation.