0

Let's say I have a next js application which exists in a different domain that needs to call a laravel route. This route leads to a login page.

This is what I did on react side

 const handleSubmit = async (e) => { e.preventDefault(); try { const result = await axios.get("http://localhost:5001/login", { headers: { // "content-type": "application/json", "x-api-signature": "my-secret-token", }, }); console.log(result); } catch (error) { console.log(error); } }; 

I am getting cors error on front end

// In Laravel auth.php

 Route::get('login', [AuthenticatedSessionController::class, 'create']) ->name('login'); 

This route leads to a simple login page.

1 Answer 1

1

You can use CORS Middleware for Laravel


Or by using middleware, something like (not tested)

Note that https://stackoverflow.com should be your app domain.

class Cors { public function handle($request, Closure $next) { return $next($request) ->header('Access-Control-Allow-Origin', 'https://stackoverflow.com') ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS') ->header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, X-Token-Auth, Authorization'); } } 

Read

  1. Laravel CORS Guide: What It Is and How to Enable It
Sign up to request clarification or add additional context in comments.

2 Comments

tthanks, class Cors is the middleware for the route is it ?
@desh yes call it in routes stackoverflow.com/questions/38039159/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.