Just for the records, what I wanted was to call (in this case) the Authenticate middleware every time I called another one middleware that checks some permissions and stuff on the auth user. Just to be DRY and to separate the logic, I wanted to still use the basic auth middleware without duplicating code or use something complex...
Here is what I've done:
namespace App\Http\Middleware;
class YourMiddleware
{
public function handle($request, Closure $next)
{
//First you need to instantiate the middleware you want to use and call the handle method on it.
//Then you have to create a closure where you'll do all your logic.
return app(Authenticate::class)->handle($request, function ($request) use ($next) {
//Put your awesome stuff there. Like:
if ($someTestsFails) {
return redirect()->home();
}
//Then process the next request if every tests passed.
return $next($request);
});
}
}
I know this is kinda old but I was looking for this exact situation and I think I found a (as jeffery says) more laravel like way to do it.
To be fair I think this may have been implemented in a newer version of laravel than what was available when this was posted.
I looked into how the 'can' => \Illuminate\Auth\Middleware\Authorize::class, middleware worked and came up with this
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;
class YourMiddleware
{
/**
* The authentication factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
public function handle($request, Closure $next)
{
$this->auth->authenticate();
if($someTestFails) {
return redirect()->home();
}
return $next($request);
}
}
Although this does not work (as is) with the generalized question in the OP.
@geozak why go to all that trouble if you can just call Auth::authenticate();? This isn't exactly an alternative to calling any middleware surely??
/**
* Determine if the current user is authenticated.
*
* @return \Illuminate\Contracts\Auth\Authenticatable
*
* @throws \Illuminate\Auth\AuthenticationException
*/
public function authenticate()
{
if (! is_null($user = $this->user())) {
return $user;
}
throw new AuthenticationException($this);
}
@peterpan666 have you seen geozak's solution? What do you make of yours 2 years on? Would you do anything differently?
I have included Google Two Factor authentication and it worked well. We have changed product design to giving option to users to enable two factor authentication.
I need to show otp screen on successful login.
In this case I want include 2fa middleware on successful login case only.