1
votes

I keep some laravel routes in the middleware auth group as:

Route::group(['middleware'=>'auth'],function(){

    Route::controller('Activities', 'ActivitiesController');
    Route::get('foo','FooController@getFoo');
    .....
});

When I try to login to access these page, I am unable to login and url redirect to login page again and again. But If I use constructor as:

public function __construct()
    {
        $this->middleware('auth');
    }

In those controllers It works perfectly. What is route group problem?

3
Do you have A middleware In Activities Controller or FooController constructors? Iike guest middle ware?Emeka Mbah
Did you modify app/http/middlewares/Authenticate in anyway?Emeka Mbah
No. I keep Authenticate as it wasIftakharul Alam
This works for me , in route Route::group(['middleware'=>'auth'],function(){ Route::controller('activities', 'ActivitiesController'); }); then controller <?php namespace App\Http\Controllers; class ActivitiesController extends Controller { public function getIndex() { return 'you are in; } } on attempt to visit /activities I was redirected to login page , and on success back to \activities with 'you are in'. What does your login process looks like?Emeka Mbah
Thanks issue solved.Iftakharul Alam

3 Answers

3
votes

Route has a ::middleware class that you can use:

Routes > web.php

Route::middleware(['auth'])->group(function(){
  Route::get('/activities', 'ActivitiesController@index');
});

You can also use Route::resource(); which I prefer. If you don't know what it does, here are the docs: https://laravel.com/docs/5.8/controllers#resource-controllers

1
votes

This works for me , in route

Route::group(['middleware'=>'auth'],function(){ 
  Route::controller('activities', 'ActivitiesController'); 
}); 

then controller

<?php namespace App\Http\Controllers; 

class ActivitiesController extends Controller { 
    public function getIndex() { 
         return 'you are in; 
    } 
} 

on attempt to visit /activities I was redirected to login page , and on success back to \activities with 'you are in'.

0
votes

In web.php:

$roleGeneral = role1.'~'.role2.'~'.role3.'~'.role4;
Route::group(['middleware' => ['permission.role:'.$roleGeneral]], function() {})

In Kernel.php:

protected $routeMiddleware = [...,
    'permission.role' => \App\Http\Middleware\CheckPermission::class,
];

In CheckPermission.php:

public function handle($request, Closure $next, $role)
{
    $roleArr = explode('~', $role);
    $token = JWTAuth::getToken();
    $user = JWTAuth::toUser($token);
    $roleLogin = SysRoleModel::where('id', $user->role_id)->first();
    if (in_array($roleLogin['name'], $roleArr)){
        return $next($request);
    }else{
        return \Redirect::back()->withMessage('You are not authorized to access!');
    }
}