<?php

namespace Mpociot\Reauthenticate\Middleware;

use Carbon\Carbon;
use Closure;

class Reauthenticate
{
    /**
     * Number of minutes a successful Reauthentication is valid.
     *
     * @var int
     */
    protected $reauthTime = 30;

    /**
     * Validate a reauthenticated Session data.
     *
     * @param \Illuminate\Session\Store $session
     *
     * @return bool
     */
    private function validAuth($session)
    {
        $validationTime = Carbon::createFromTimestamp($session->get('reauthenticate.life', 0));

        return $session->get('reauthenticate.authenticated', false) &&
            ($validationTime->diffInMinutes() <= $this->reauthTime);
    }

    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!$this->validAuth($request->session())) {
            $request->session()->set('url.intended', $request->url());

            return redirect('auth/reauthenticate');
        }

        return $next($request);
    }
}

<?php

namespace Mpociot\Reauthenticate\Middleware;

use Carbon\Carbon;
use Closure;

class Reauthenticate
{
    /**
     * Number of minutes a successful Reauthentication is valid.
     *
     * @var int
     */
    protected $reauthTime = 30;

    /**
     * Validate a reauthenticated Session data.
     *
     * @param \Illuminate\Session\Store $session
     *
     * @return bool
     */
    private function validAuth($session)
    {
        $validationTime = Carbon::createFromTimestamp($session->get('reauthenticate.life', 0));

        return $session->get('reauthenticate.authenticated', false) &&
            ($validationTime->diffInMinutes() <= $this->reauthTime);
    }

    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!$this->validAuth($request->session())) {
            $request->session()->set('url.intended', $request->url());

            return $this->invalidated($request);
        }

        return $next($request);
    }

    /**
     * Handle invalidated auth.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return mixed
     */
    protected function invalidated($request)
    {
        return redirect('auth/reauthenticate');
    }
}

Allow user to extends and define how the redirect path/process should work.
Allow user to extends and define how the redirect path/process should work.

Signed-off-by: crynobone <e1a543840a942eb68427510a8a483282a7bfeddf@gmail.com>
