How to use Postman with Nuxt auth module based on JWT
- Request login endpoint and intercept JWT
- Save the token in cookies
- Request your restricted endpoints
If you haven't implemented authentication in Nuxt yet first check how to authenticate your nuxt app with auth module and express.js.
Request login endpoint and intercept JWT
Check what endpoint your login page reqests when a user input credentials in nuxt. Use this url in Postman, select a proper method (GET or POST) and pass this credentials accordingly. Depending on your case for the POST use body tab and for GET use params tab.
After sending request you should get your token as a response. Copy the value of the token which will be needed in a minute.
If Postman sends back some html doc it means that you hit invalid url, probably in such case you used a nuxt route instead of backend endpoint.
Save the token in cookies
Find cookies button near Send button in Postman. It will get you modal with options to add cookie. Search the modal for your domain (or add one if it doesn't exist yet) and add a new cookie named auth._token.local.
Postman will generate a default cookie template and in the body you should pass the acquired token from the login endpoint with Bearer prefix and single space (you can also use equivalent of s single space written as %20).
// default template cookie generate by Postman Cookie_4=value; Path=/; Domain=.your domain; Expires=Tue, 02 Nov 2021 12:10:27 GMT;
// cookie with your token auth._token.local=Bear yourTokenStringGoesHere; Path=/; Domain=.yourDomain; Expires=Tue, 02 Nov 2021 11:18:30 GMT;
Before going further you can also check if your cookies are properly saved by going to the code section (button next to the cookies). This way you will get the full info what's sending to the server. Information about cookies will be displayed as well. Double check that you passed the required cookie in the header and your token start with proper prefix (your probably get more info that is visible on the image below because I erased some info for a better reading).
Request your restricted endpoints
You are ready to requests your guarded endpoints using the saved config. Check what endpoints need authentication and return json for a easy reading. Then make a request to them. Your nuxt app should get you all the requested data as if you were logged using a browser. Now that you now how to use Postman to behave as an authenticated user in nuxt you have a great opportunity for easier testing of different aspects of your authentication mechanism like password resetting, scope access and so on.