You can try below steps for your api testing using postman. it's worked of me.
http://localhost:3000/api/auth/session
http://localhost:3000/api/auth/signin
Pre-request Script:
const jar = pm.cookies.jar();
console.log("Pre request called...");
pm.globals.set("csrfToken", "Hello World");
pm.globals.unset("sessionToken");
jar.clear(pm.request.url, function (error) {
console.log(error);
});
Description: This script sets the csrfToken
in the global environment variable and clears the sessionToken
you can check that in your postman console.
Post-response Script:
console.log("Post response called...");
pm.cookies.each(cookie => console.log(cookie));
let csrfToken = pm.cookies.get("next-auth.csrf-token");
let csrfTokenValue = csrfToken.split('|')[0];
console.log('csrf token value: ', csrfTokenValue);
pm.globals.set("csrfToken", csrfTokenValue);
Description: This script retrieves the csrfToken
from the cookies and sets it in the global environment variable.
http://localhost:3000/api/auth/callback/credentials
{
"email":"{{userEmail}}" ,
"password": "{{userPassword}}",
"redirect": "false",
"csrfToken": "{{csrfToken}}",
"callbackUrl": "http://localhost:3000/",
"json": "true"
}
const jar = pm.cookies.jar();
jar.unset(pm.request.url, 'next-auth.session-token', function (error) {
// error - <Error>
});
pm.cookies.each(cookie => console.log(cookie));
let sessionTokenValue = pm.cookies.get("next-auth.session-token");
console.log('session token value: ', sessionTokenValue);
pm.globals.set("sessionToken", sessionTokenValue);
sessionToken
in the global environment variable.http://localhost:3000/api/auth/session
http://localhost:3000/api/auth/signout
{
"csrfToken": "{{csrfToken}}",
"callbackUrl": "http://localhost:3000/dashboard",
"json": "true"
}
https://asset.cloudinary.com/dugkwrefy/6266f043c7092d1d3856bdad6448fa89