manchenkoff/nuxt-auth-sanctum

[Question] backend not receiving other than necessary cookies from frontend

Closed this issue · 3 comments

Hi, the module is working perfectly, but I can't send other cookies generated in nuxt to laravel, for example, an referral code in registration.

I have a /ref/[ref].vue with:

<script setup lang="ts">
const route = useRoute()
const cookie = useCookie('referred_by', {
 maxAge: 60 * 60 * 24 * 30,
 sameSite: 'lax',
})

cookie.value = cookie.value || route.params.ref as string

navigateTo('/auth/register', {
 replace: true,
 open: {
   target: '_self',
 },
})
</script>

The cookie is stored in the browser with lax along laravel session and XSRF-TOKEN, but when I submit the registration form, laravel receives NULL for referral code and works properly with the session and xsrf token.

[2024-07-25 19:45:02] local.DEBUG: array (
  'XSRF-TOKEN' => 'M829Xy9JKOfi9DShN14ZbngjFdI5Zs31NrdBDaeO',
  'laravel_session' => 'CK2UCWNWvVtXnobeR5nqXSVYtJWEw6XRjTGvfcf2',
  'referred_by' => NULL,
  'i18n_redirected' => NULL,
)  

My session settings:

SESSION_DRIVER=database
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null

Maybe I missing something? Off course I'm using the useSanctumClient.

Hey @ibrunotome, I have just tried the same approach but it looks like it works for me. I added this code to the login page:

const route = useRoute()
const referralCookie = useCookie('referral', { maxAge: 60 * 60 * 24 * 30, sameSite: 'lax' })

referralCookie.value = referralCookie.value || route.query.referral as string

here is what we have in the Application tab in the browser

Screenshot 2024-07-25 at 10 25 39 PM

and once I send a request to the API, here is the list of sent cookies

Screenshot 2024-07-25 at 10 26 02 PM

as you can see, all cookies were included without any null values. On the API side I also can see the whole list in the request object.

For the tests, I used sample projects:

@ibrunotome a new update 😄

Turned out I didn't test it well since I debugged incoming requests, but not the request in the controller. Laravel middleware named EncryptCookies actually resets the value to null if it is not set by the backend.

So, to solve this, you need to configure the middleware properly:

For Laravel 11+, update your bootstrap/app.php like this

<?php

declare(strict_types=1);

use App\Http\Middleware\EnsureEmailIsVerified;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        // ... routing files
    )
    ->withMiddleware(function (Middleware $middleware) {
        // ... other middleware config
        $middleware->encryptCookies(['referral']); // REPLACE WITH YOUR COOKIE NAME
    })
    ->create();

For previous Laravel versions, go to app/Http/Middleware/EncryptCookies.php and extend protected $except = [] array with your cookies names.

It worked! Thanks for spending your time on this 🤝