I am developing a mobile application with Cordova. I am writing the backend with laravel 10. I am using laravel sanctum for the token. I have written the login and user information retrieval api tips. I am testing the api tips in Postman. After logging in, I get a value called cookie laravel_session and I use the token returned by laravel as a bearer token. I send a request to the api tip to retrieve user information and since my session is active, I can successfully retrieve user information with Auth::user(). However, when I do this with cordova, I notice that every time I send a request to the laravel side, the session id changes when I return Session::getId(). I see that the session id, laravel_session value, appears on the cookie side, but the session id changes every time I send a request. What is the reason?
.env:
SESSION_DRIVER=database
SESSION_LIFETIME=120
Laravel UserController:
class UserController extends Controller
{
public function index(Request $request)
{
$session = $request->post('session');
return $request->session()->getId();
$user = Auth::user();
if($user) {
return response()->json([
'status' => 'success',
'data' => $user
]);
} else {
return response()->json([
'status' => 'error',
'message' => 'Unauthorized'
], 401);
}
}
}
Cordova App.js:
$.ajax({
url: 'http://localhost:5000/api/user',
method: 'GET',
headers: {
'Authorization': 'Bearer ' + $token,
},
dataType: 'json',
success: function(response) {
console.log(response);
localStorage.setItem("user", response.data);
return response;
},
error: function(response) {
console.log(response);
app.views.main.router.navigate('/sign-in/', {reloadCurrent: true});
}
});
Login returned token & session id:
immediately afterwards, when I send a request to the api endpoint to pull user information, the session id changed:
but with the postman it's very different. We log in to the account:
and then when we send a request to the api endpoint to pull user information, we see that our session id has not changed.
I've searched all the similar threads on stackoverflow, github, laracast and tried all the solutions but my problem still persists. Can you please help me?