How can I use Inertia to make a fetch request to a route behind `auth:api` middleware?

Viewed 15

I have a Laravel app with a number of routes behind auth:api. I'd like those routes to be available to users in my Dashboard.

I've tried the following on a route that I have named api.me that is behind auth:api, but Inertia does not provide the Authorization header.

import { useForm } from "@inertiajs/inertia-react";
...
const { data, setData, errors, get } = useForm({});
...
get(route('api.me'));

I assume the route must be behind the auth middleware instead. If so, is it possible to have it use both the auth and auth:api middlewares?

1 Answers

It looks like the best way to handle this is to provide the data via the controller or route.

The route should return:

Route::get('/subscription', function() {
    return Inertia::render('some/route', [
        'me_info' => Auth::user(),
        'other_data' > ['some' => 'other data']
    ]);
});

If you are using React, the me_info and other_data will be made available to the component props.

Related