4

I want to controll authentication with cookies. And In my browser working successfully. But When I tried to test with postman, Postman doesn't add cookie to new request.

  1. step - I login and response header like that:

enter image description here

But the response cookies tab like that:

enter image description here

And manage cookies window like that:

enter image description here

  1. step - I send a request to unprotected router and I get unauthorized error.

This error started today. I don't remember making any changes to the settings. Why Im getting this type error. How can I solve this?

akasaa
  • 1,282
  • 4
  • 13
  • 33

4 Answers4

3

I also had this problem, the fix is to remove the secure flag in the cookie when sending cookies from localhost as cookies set as secure can only be sent over HTTPS.

1

I had this issue when testing a local Laravel Sanctum request to /login.

I had the following .env values set

SESSION_DOMAIN=docker-api-service-name
SANCTUM_STATEFUL_DOMAINS=docker-api-service-name

However these needed to be set to localhost to match the domain of the APP_URL. After this, everything was working fine.

SESSION_DOMAIN=localhost
SANCTUM_STATEFUL_DOMAINS=localhost
Novocaine
  • 4,692
  • 4
  • 44
  • 66
0

Someone mentioned that setting the secure flag to false will solve it, and it will. The explanation however was not entirely correct.

Secure will indeed only work over secure connections (HTTPS). However, it will also work over HTTP if it's done in localhost: https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
metinmayi
  • 1
  • 1
  • 2
0

Setup your domain name inside the .env file

if you are on localhost add below

SESSION_DOMAIN=localhost

for custom domain (my domain name is qa_app.test)

SESSION_DOMAIN=qa_app.test

Note: if you are sending a request on an authenticated route where you are using Sanctum or passport middleware using Postman you need to use pass Referer.

Referer=localhost

it's identical for localhost or custom domain, in both conditions you need to pass localhost in the referer.

When you are in the web browser you don't need to pass the referer, it will be passed by web browser.

Hadayat Niazi
  • 1,991
  • 3
  • 16
  • 28