DEV Community

Aisak
Aisak

Posted on

Laravel Sanctum No 'Access-Control-Allow-Origin'

Alt Text

Hello, i have this error, I dont know if my configuration is bad, or is correctly.
File config\cors.php



<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Cross-Origin Resource Sharing (CORS) Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may configure your settings for cross-origin resource sharing
    | or "CORS". This determines what cross-origin operations may execute
    | in web browsers. You are free to adjust these settings as needed.
    |
    | To learn more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
    |
    */

    'paths' => [
        'api/*', 
        '/*',
        'register',
        'sanctum/csrf-cookie', 

    ],

    'allowed_methods' => ['*'],

    'allowed_origins' => ['*'],

    'allowed_origins_patterns' => [''],

    'allowed_headers' => ['*'],

    'exposed_headers' => [],

    'max_age' => 0,

    'supports_credentials' => true,

];



Enter fullscreen mode Exit fullscreen mode

config\sanctum.php



  'stateful' => explode(',', env(
        'SANCTUM_STATEFUL_DOMAINS',
        'localhost, 127.0.0.1'
       // 'localhost,localhost:8080,127.0.0.1,127.0.0.1:8080,::1'
    )),


Enter fullscreen mode Exit fullscreen mode

Kernel.php



  protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            EnsureFrontendRequestsAreStateful::class,
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];


Enter fullscreen mode Exit fullscreen mode

Model user.php I add HasApitokens and my file .env i have:



SESSION_DOMAIN=localhost:8080 
SANCTUM_STATEFUL_DOMAINS=localhost:8080


Enter fullscreen mode Exit fullscreen mode

I use too only localhost, in my project file main of vuejs i have this:



Vue.config.productionTip = false
Vue.prototype.$http = axios
axios.defaults.withCredentials = true
axios.defaults.baseURL = 'http://localhost:8000/'

Vue.use(VueCountryCode);

new Vue({
  router,
  vuetify,
  store,
  render: h => h(App)
}).$mount('#app')


Enter fullscreen mode Exit fullscreen mode

and in the methods to send the form i have this



 sendRegister(){
         this.$http.get('sanctum/csrf-cookie').then(() => {
           this.$http.post('register', this.form).then(res=>{
              console.log(res)
           })
        });
      },


Enter fullscreen mode Exit fullscreen mode

And i have the error that show you in the images, what is my error in this configuration :/

Top comments (1)

Collapse
 
tonyrandriamanantsoa profile image
ToNyRANDRIAMANANTSOA

For those facing cors issue in laravel even though everything set up, check if there are duplicate use alias declaration in some namespaces somewhere, or any unexpected expressions, for me, duplicate use Illuminate\Database\Eloquent\Builder in controller sillily caused the issue :)