<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Routing\Exceptions\InvalidSignatureException;
use Illuminate\Routing\Middleware\ValidateSignature as BaseValidateSignature;

class ValidateSignature extends BaseValidateSignature
{
    public function handle($request, Closure $next, ...$args)
    {
        try {
            return parent::handle($request, $next, ...$args);
        } catch (InvalidSignatureException $exception) {
            if ($request->expectsJson()) {
                throw $exception;
            }

            if ($request->routeIs('verification.verify')) {
                $request->session()->flash('verification', [
                    'status' => 'error',
                    'title' => __('auth.verification.expired_title'),
                    'message' => __('auth.verification.expired_message'),
                ]);

                return redirect()->route('verification.notice');
            }

            throw $exception;
        }
    }
}
