Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

duchuynguyen94's avatar

Unauthenticated api laravel subdomain

I am having ajax api laravel problem Unauthenticated. Please help me

Api: http://localhost/api/v1/verify/telephone/send_code -> error

Api: http://seller.localhost/api/v1verify/telephone/send_code -> OK

Webview: http://seller.localhost In seller.localhost

      $.ajaxSetup({
          headers: {
            'Accept': 'application/json',
            'X-CSRF-TOKEN': crsf_token,
            'X-Requested-With': 'XMLHttpRequest'
            },
      });
        
          $.ajax({
            url: 'http://localhost/api/v1/verify/telephone/send_code',
            type: 'post',
            dataType: 'json',
            data: {telephone: $('input[name="telephone"]').val()},
            xhrFields: {
                withCredentials: true
            },
            crossDomain:true,
            success: function () {
            }
         });

File cors middware

    class Cors
       {

      public function handle($request, Closure $next)
      {
        return $next($request)
            ->header('Access-Control-Allow-Origin', '*')
              ->header('Access-Control-Allow-Methods','*')
             ->header('Access-Control-Allow-Credentials', 'true')
             ->header('Access-Control-Allow-Headers', 'Accept, Content-Type, X-CSRF-Token');

      }
 }

     protected $middleware = [
          ....
           \App\Http\Middleware\Cors::class
         ];
   protected $middlewareGroups = [


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

Api route

    protected function mapApiRoutes()
      {
         Route::prefix('api')
            ->middleware('api')
               ->namespace($this->namespace)
          ->group(base_path('routes/api.php'));
     }
0 likes
1 reply

Please or to participate in this conversation.