Liam Arbel
Liam Arbel

Reputation: 557

How to fetch Laravel cookie in an SPA if page only loads once

I'm running a Vue3 SPA on a Laravel 8 backend. I'm setting a cookie via middleware for unauthenticated users. I can see the cookie and its value in Chrome's dev tools Sotrage > Cookies. But I can't read its value inside of the Vue app, probably since it was set after the initial page load that created the Vue SPA.

Any idea how I can access that cookie?

Upvotes: 1

Views: 984

Answers (2)

Yasoob_Saifyzadeh
Yasoob_Saifyzadeh

Reputation: 1

You can manually set false HTTP-only the it when creating a cookie

protected function authenticated(Request $request, $user)
{
Cookie::queue(Cookie::make('token','token_here',525600,null,null,false,false));
}

Upvotes: 0

tony19
tony19

Reputation: 138246

As determined from comments, you have HTTP-only cookies enabled, which prevents client-side JavaScript access to the cookies.

HTTP-only cookies are intended for security purposes, but you can disable this with session-set-cookie-params:

<?php
  $options = array("httponly" => false);
  session_set_cookie_params($options);
  session_start();
?>

Or in laravel/config/session.php:

<?php
  //...
  return [
    'http_only' => false
  ]
?>

Upvotes: 1

Related Questions