node-wpapi
node-wpapi copied to clipboard
Authentication using jwt-auth plugin
I am working with WP REST API to use it in my node js application. I am using the following piece of code to get token. It is working fine on postman but in node js I m getting an error .
var WPAPI = require('wpapi'); var wp = new WPAPI({ endpoint: 'https://domain.com/wp-json' });
wp.myCustomResource = wp.registerRoute('jwt-auth/v1', '/token',
{ methods: 'POST', params: ['username', 'password'] })
wp.myCustomResource()
.username('myUserName').password('myPassword')
.then(res => console.log(res))
.catch(err => console.error(err))
})
}
getting error: code:"rest_no_route" data:Object {status: 404} message:"No route was found matching the URL and request method"
Anyone here to know solution...?
Hey @muzamalidrees , may I ask which JET plugin you are using? And which version of this library?
@kadamwhite I too am having the same issue. I am testing against a headless wordpress app using https://ca.wordpress.org/plugins/jwt-authentication-for-wp-rest-api/ V1
I can test the plugin with postman and axios in react but cant get it to work here
var WPAPI = require("wpapi");
var wp = new WPAPI({ endpoint: "http://localhost:1038/wp-json" });
wp.login = wp.registerRoute("jwt-auth/v1", "/token", {
methods: "post",
params: ["username", "password"],
});
// wp.posts()
// .then(data => console.log(data)) // this works
// .catch(e => console.log);
wp.login()
.username("admin")
.password("password")
.then((res) => console.log(res))
.catch((err) => console.error(err));
Getting
{
code: 'rest_no_route',
message: 'No route was found matching the URL and request method',
data: { status: 404 }
}
@muzamalidrees did you ever get this to work? otherwise it looks promising
When calling .then it makes a GET call while this lib expects a POST.
Here's what I used in a react native app.
instance.wp.login().create(`username=${username}&password=${password}`, (err, res) => {
// do your stuff
});
Will anyone reply to this? Kadam? It's a great lib but we can't go anywhere without some real auth like JWT ... and so far all efforts failed ... HELP!!! At least give us some directions ....???