node-wpapi icon indicating copy to clipboard operation
node-wpapi copied to clipboard

Authentication using jwt-auth plugin

Open muzamalidrees opened this issue 5 years ago • 6 comments
trafficstars

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...?

muzamalidrees avatar Dec 12 '19 11:12 muzamalidrees

Hey @muzamalidrees , may I ask which JET plugin you are using? And which version of this library?

kadamwhite avatar Dec 14 '19 07:12 kadamwhite

@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 }
}

webface avatar Jun 07 '20 23:06 webface

@muzamalidrees did you ever get this to work? otherwise it looks promising

webface avatar Jun 07 '20 23:06 webface

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
});

oleprohon avatar Dec 03 '20 04:12 oleprohon

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 ....???

ahmedmusawir avatar Jan 03 '22 02:01 ahmedmusawir