dimanche 20 septembre 2015

How do I set up middleware in laravel

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire