Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Fetch to Laravel API Creates New Session

My app is using React on the front end and Laravel 5.4 on the backend. I'm using fetch() to request data from the backend. The problem is that two sessions are created when the page loads. A TokenMismatchException is thrown by the CSRF Middleware when a POST request is made because the token that is sent matches the first session that is created, but it checks against the second.

I'm setting the token in app.blade.php

<meta name="_token" content="{{ csrf_token() }}">

And grabbing the token in the fetch config

fetchConfig = {
    headers:  {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
    },
    credentials: 'same-origin'
}}

Here are the decrypted sessions:

a:3:{s:6:"_token";s:40:"7obvOzPaqqJDtVdij8RaqrvmTFLjKA2qnvYMxry6";s:9:"_previous";a:1:{s:3:"url";s:24:"http://localhost/page";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}

a:3:{s:6:"_token";s:40:"5Aiws9Qy72YzlkfWX81zkhzrSeiMDYjFWiLeDAwN";s:9:"_previous";a:1:{s:3:"url";s:41:"http://localhost/api/page";}s:6:"_flash";a:2:{s:3:"old";a:0:{}s:3:"new";a:0:{}}}

Request URL: http://localhost/page

API URL: http://localhost/api/page

How can I prevent a new session from being created when the React app makes its initial GET request?

like image 451
bill Avatar asked Apr 13 '17 17:04

bill


People also ask

How fetch data from React to API?

Using the JavaScript Fetch API. The Fetch API through the fetch() method allows us to make an HTTP request to the backend. With this method, we can perform different types of operations using HTTP methods like the GET method to request data from an endpoint, POST to send data to an endpoint, and more.

Can we use session in Laravel API?

Laravel Passport is a token-based authentication package, it does not use sessions!

Can React and Laravel work together?

Q: Can I use React JS with Laravel? Laravel doesn't require you to use any specific Javascript framework. As long as you've configured Laravel Mix properly, any framework should work.


Video Answer


2 Answers

I'm not sure what is your request URL and what is your target API url. Make sure both are on same domain(including subdomain).

I think its a good idea to disable CSRF validation only for API routes as these might be used by other domains, native apps etc

You can do that by adding following class file: app/Http/Middleware/VerifyCsrfToken.php

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'api/*',
    ];
}

Make sure to edit Kernal.php to point to the new class:

protected $middleware = [
    'csrf' => 'App\Http\Middleware\VerifyCsrfToken'
];

To learn more how Laravel uses CSRF check this laracast video

like image 25
Bhavesh B Avatar answered Oct 19 '22 09:10

Bhavesh B


Laravel automatically generates a CSRF "token" for each active user session managed by the application. This token is used to verify that the authenticated user is the one actually making the requests to the application. : https://laravel.com/docs/5.4/csrf

APIs are stateless. There is nothing like session in APIs. So you shouldn't use CSRF token in API. If you check Kernel.php of laravel. You will see Tylor didn't add VerifyCsrf middleware in API group. Which suggest that CSRF is only used in the request having session i.e, stateful request. I would recommend you to use JWT based authentication system for API. For more about JWT check here.

You can use this laravel package for JWT : https://github.com/tymondesigns/jwt-auth

like image 155
Pankit Gami Avatar answered Oct 19 '22 07:10

Pankit Gami