<?php

namespace App\Http\Middleware;

use App\Models\AppSession;
use Closure;

class RedirectAuthenticatedPro
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $sessionKey = $request->cookie('sessionKey');
        $appSession = AppSession::where('session_key', $sessionKey)->first();

        if($appSession && $appSession->is_active && $appSession->pro) {
            return redirect()->route('dashboard');
        }

        return $next($request);
    }
}
