• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

How to Get Authentication Working Using the Node WPApi Package

Wordpress · September 30, 2021

If you are working with WordPress version 5 and up, you might be using the REST API. I love the in-built REST API WordPress provides, especially for creating applications on top of WordPress.

The Node WPApi package makes this a breeze, especially when it comes to authentication based actions.

My first test with this package was creating a new post, and I got this error message:

Sorry, you are not allowed to create posts as this user.

I was confused at first because I entered the correct username and password for my WordPress installation. Well, as you will discover, WordPress won’t just allow you to perform authentication-based requests using your standard credentials.

Once you are logged in, you need to go down to the application passwords section and generate a new password in your user profile section.

Once you enter a descriptive label, you will be presented with a randomly generated password. You will not be able to retrieve this, so copy and paste it somewhere. You will then use your currently logged in username as your username value and your newly generated application password as your password (instead of the password you use when you log in).

import WPAPI from 'wpapi';

const wp = new WPAPI({
    endpoint: 'https://mycoolwebsite.com/wp-json',
    username: 'user',
    password: 'lcSd JKgZG w49DE TbqZ A95eM 64HJ3a'
});

This is what your code would look like. I am using my generated application password but my standard username, and it works.

Dwayne

Leave a Reply Cancel reply

0 Comments
Inline Feedbacks
View all comments

Primary Sidebar

Popular

  • Testing Event Listeners In Jest (Without Using A Library)
  • Handling Errors with the Fetch API
  • Thoughts on the Flipper Zero
  • Waiting for an Element to Exist With JavaScript
  • How To Paginate An Array In Javascript

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz