J
J
juan_chikatilo2022-04-01 17:36:17
Laravel
juan_chikatilo, 2022-04-01 17:36:17

How to redirect user before rendering laravel layer in vue?

Hello!
I have routes like this in laravel:

Route::get('/admin{any}',function(){
    return view('admin');
})->where('any','.*');

Route::get('/{any}',function(){
return view('index');
})->where('any','.*');


And I want that when a normal user tries to go to the admin page, the admin.blade.php layer is not rendered.
Those. I tried to put a navigation guard in vue-router like this:
if (to.matched.some(record => record.meta.isAdmin)) {
        if (store.getters['auth/role']!="admin") {
            return redirect({name:'Error403'});
          } 
        }


It works, but because the url was originally /admin, it rendered the layout from admin.blade.php when it should have been from index.blade.php.

As I understand it, I should put middleware on this march in laravel. I originally wrote a middleware for my api that looks like this:
if ($request->user()->role=="admin"){
        return $next($request);
        }
        else
        {
            return response(['message'=>"Недостаточно прав для посещения данного ресурса"],403);
        }


And I thought that it could be put on that route from web.php
Route::get('/admin{any}',function(){
    return view('admin');
})->where('any','.*')->middleware('isAdmin');


But here the current $request->user() is lost, i.e. throws an error that the method was not found (I have authorization via JWT). Because app.js is needed here, where the token is taken from localstorage, if any.

Help me please.
1) How can you take the current token if the back cannot take data from ls?
2) If not, what can be done?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question