Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

princeparaste's avatar

Api auth Token saved in Session getting lost after i redirect to third party page.

I am using LARAVEL Passport for building Rest API's.

I saved the user auth token in Laravel Session during the login time. I am using payment gateway. When user make payment i redirect my website to the payment gateway website. and on success response i redirect back to my laravel website.

The problem is when i do this for the 1st time on a day it always gives me Unauthenticated Error on 1st try. On the 2nd,3rd try and so on it works fine. It only gives me this error on 1st try of the day.

I try to debug it and find out that the Session::get('token'); is always getting empty on the 1st try. So in the backend API, it checks wheather the user has token or not. on 1st try its always empty so it gives Unauthenticated Error I dont know why this happening on every 1st try only. Any ideas why is this ?

0 likes
2 replies
martinbean's avatar

@princeparaste APIs are stateless. You don’t use sessions.

It completely defeats the point of using token-based authentication like OAuth (which is what Passport is) if you then just save that token in a session.

princeparaste's avatar

I am making API Request using Guzzle Client. I need to pass token to the while hitting API for authentication. The token is generated during the login. At that time i saved the token in SESSION so i can use that token in other pages too and hit API request. I need to use the Session to save the user token. Otherwise how will i send the to token to API in other pages of web

Please or to participate in this conversation.