JS Wrapper for wp-api-jwt-auth
Please, make sure that you have:
- installed wp-api-jwt-auth and enabled on your wp site
- enabled HTTP Authorization Header
- configurated Secret Key and CORs Support
install this package:
npm install wordpress-jwt-auth
Authenticate using JWT
import { connectToJwt } from 'wordpress-jwt-auth';
// Promise
connectToJwt('https://www.myhosting.com/wordpress').then((jwtConnection) => {
jwtConnection.generateToken('admin', 'password').then(userConnection) => {
console.log(userConnection.token);
// generated token
jwtConnection.validate().then(validated => {
console.log(validate);
// true
});
});
});
// Await/Async
const jwtConnection = await connectToJwt('https://www.myhosting.com/wordpress');
const { token } = await jwtConnection.generateToken('admin', 'password');
console.log(jwtConnection.validate(token));
// true
You can import generateToken
directly from library
import { generateToken } from 'wordpress-jwt-auth';
const { token } = generateToken('admin', 'root');
Deleting a post with id 32
from wordpress using REST API
import axios from 'axios';
import { connectToJwt } from 'wordpress-jwt-auth';
const WP_URL = 'https://www.mywordpress.com';
const POST_ID_TO_DELETE = 32;
const { generateToken } = await connectToJwt(WP_URL);
const { token } = await generateToken('superadmin', '2489cs12mklz');
const authHeader = { headers: { Authorization: `bearer ${token}` } };
axios.delete(`${WP_URL}/wp-json/wp/v2/posts/${POST_ID_TO_DELETE}`, {}, authHeader);