'How can I make to redirect from axios code wwhen invalid access?
With Laravel 9/Inertiajs 3 I have page under adminarea with checking that user is logged as admin:
My vuejs page is container vue file and I read data using axios(that gives me possibility to run filter without all page reloading) :
axios.post(route('admin.currencies.filter'), filters)
.then(({data}) => {
currencyRows.value = data.data
})
.catch(e => {
showRTE(e) // custom error
console.error(e)
})
and :
<?php
class CurrencyController extends Controller
{
public function index()
{
if ( ! auth()->user()->can(ACCESS_APP_ADMIN_LABEL)) {
return redirect(route('admin.dashboard.index'))
->with( 'flash', 'You have no access to currencies listing page')
->with('flash_type', 'error');
}
return Inertia::render('Admins/Currencies/Index', []);
}
public function filter()
{
\Log::info( varDump(-9, ' -9 filter::') );
if ( ! auth()->user()->can(ACCESS_APP_ADMIN_LABEL)) {
\Log::info( varDump(-98, ' -98 INSIDE filter::') );
return redirect(route('admin.dashboard.index'))
->with( 'flash', 'You have no access to currencies filter method')
->with('flash_type', 'error');
}
...
$currencies = Currency
::getByName($filter_name)
->orderBy($order_by, $order_direction)
->paginate($backend_items_per_page, array('*'), 'page', $page);
return (CurrencyResource::collection($currencies)); // collection of resources
} // public function filter()
...
php>
The problem is that when in filter method invalid access code is run(I check it in file) app is not redirecred to dasboard page, but is run next and I got javascript error as filter method did not return valid data which are expected on client...
Looks like redirect can not be run from axios code... How that can be done?
Thanks!
Solution 1:[1]
You may change axios to this.$inertia : [https://inertiajs.com/manual-visits]
this.$inertia.post(this.route('...'),{},{
only:['...']
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | gouki |
