2
votes

So I have my auth middleware, which is registered in the Http/Kernel.php as:

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
];

Next I made a change to the middleware handle function in the Authenticate class:

public function handle($request, Closure $next)
{
    if ($this->auth->check()) {
        $user = $this->auth->user();

        $currentDateTime     = strtotime('Y-m-d H:i:s');
        $tokenExpirationTile = strtotime($user->token_expiration);

        if ($currentDateTime <= $tokenExpirationTile) {
            return $next($request);
        } else {
            $this->auth->logout();
            redirect('home/login')->with('message', 'Your session has expired. Please login in again');
        }
    } else {
        redirect('home/login')->with('message', 'Please login before attempting to access that');
    }
}

And finally I created the route:

Route::get('home/dashboard', 'HomeController@dashboard', ['middleware' => 'auth']);

I can visit this route but as a non signed in user I should be redirected.

When I through a dd() in the handle function nothing happens.

How do I get it to run this method on this route?

Also when it comes to other controllers where you need to authenticate before each action request how do you say: "before each action, run this method." In rails I would do before_action :method_name

1
where is your dd()? what happens if you put $this->middleware('auth'); in your HomeController constructor. According to the docs "However, it is more convenient to specify middleware within your controller's constructor. "ExoticChimp
@ExoticChimp I only want it to work on a specific controller actionTheWebs
Please check the docs in my answer. You put this in your constructor: $this->middleware('auth', ['only' => ['dashboard']]);ExoticChimp

1 Answers

1
votes

For the second part of your question, please consult the docs for how to apply middleware to specific actions in a controller and on routes:

http://laravel.com/docs/master/controllers#controller-middleware http://laravel.com/docs/master/routing#route-group-middleware

For the first part, have you tried running 'composer dump-autoload' from the terminal?