<?php

namespace App\Http\Middleware;

use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie;

class EnsureUserNotLoggedIn
{
    public function handle(Request $request, Closure $next)
    {

        $sessionKey = Cookie::get(config('app.sessionKeyName'));

        if (User::isLoggedIn($sessionKey)) {
            return redirect(route('index'));
        }

        return $next($request);
    }
}
